This commit is contained in:
TriMill 2022-12-04 00:07:16 -05:00
parent 40f684fa6e
commit e0fa04d075
4 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,14 @@
fn main() {
println!("{}", std::io::stdin().lines()
.map(|x| x.unwrap())
.map(|line| (
line.chars().take(line.len() / 2).collect::<Vec<char>>(),
line.chars().skip(line.len() / 2).collect::<Vec<char>>()
)).map(|(a, b)| *a.iter().filter(|c| b.contains(c)).next().unwrap())
.map(|c| match c {
'a'..='z' => (c as u32) - ('a' as u32) + 1,
'A'..='Z' => (c as u32) - ('A' as u32) + 27,
_ => unreachable!()
}).reduce(|a, b| a + b)
.unwrap());
}

View file

@ -0,0 +1,17 @@
fn main() {
let lines = std::io::stdin().lines()
.map(|x| x.unwrap())
.collect::<Vec<String>>();
let result = lines.chunks(3)
.map(|chunk|
chunk[0].chars()
.filter(|c| chunk[1].contains(*c) && chunk[2].contains(*c))
.next().unwrap()
).map(|c| match c {
'a'..='z' => (c as u32) - ('a' as u32) + 1,
'A'..='Z' => (c as u32) - ('A' as u32) + 27,
_ => unreachable!()
}).reduce(|a, b| a + b)
.unwrap();
println!("{}", result);
}

14
examples/day04_1.rs Normal file
View file

@ -0,0 +1,14 @@
use std::collections::*;
fn main() {
let lines: Vec<String> = std::io::stdin().lines().map(|x| x.unwrap()).collect();
let mut result = 0;
for line in lines {
let elves = line.split(",").map(|x| x.split("-").map(|s| s.parse().unwrap()).collect()).collect::<Vec<Vec<i32>>>();
if elves[0][0] <= elves[1][0] && elves[0][1] >= elves[1][1]
|| elves[0][0] >= elves[1][0] && elves[0][1] <= elves[1][1] {
result += 1;
}
}
println!("{}", result);
}

14
examples/day04_2.rs Normal file
View file

@ -0,0 +1,14 @@
use std::collections::*;
fn main() {
let lines: Vec<String> = std::io::stdin().lines().map(|x| x.unwrap()).collect();
let mut result = 0;
for line in lines {
let elves = line.split(",").map(|x| x.split("-").map(|s| s.parse().unwrap()).collect()).collect::<Vec<Vec<i32>>>();
if elves[0][1] >= elves[1][0] && elves[1][1] >= elves[0][0]
|| elves[0][1] <= elves[1][0] && elves[1][1] <= elves[0][0] {
result += 1;
}
}
println!("{}", result);
}