diff --git a/examples/day03_1_rewrite.rs b/examples/day03_1_rewrite.rs new file mode 100644 index 0000000..40c12bd --- /dev/null +++ b/examples/day03_1_rewrite.rs @@ -0,0 +1,14 @@ +fn main() { + println!("{}", std::io::stdin().lines() + .map(|x| x.unwrap()) + .map(|line| ( + line.chars().take(line.len() / 2).collect::>(), + line.chars().skip(line.len() / 2).collect::>() + )).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()); +} diff --git a/examples/day03_2_rewrite.rs b/examples/day03_2_rewrite.rs new file mode 100644 index 0000000..e620681 --- /dev/null +++ b/examples/day03_2_rewrite.rs @@ -0,0 +1,17 @@ +fn main() { + let lines = std::io::stdin().lines() + .map(|x| x.unwrap()) + .collect::>(); + 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); +} diff --git a/examples/day04_1.rs b/examples/day04_1.rs new file mode 100644 index 0000000..197f9b6 --- /dev/null +++ b/examples/day04_1.rs @@ -0,0 +1,14 @@ +use std::collections::*; + +fn main() { + let lines: Vec = 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::>>(); + 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); +} diff --git a/examples/day04_2.rs b/examples/day04_2.rs new file mode 100644 index 0000000..f257a7a --- /dev/null +++ b/examples/day04_2.rs @@ -0,0 +1,14 @@ +use std::collections::*; + +fn main() { + let lines: Vec = 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::>>(); + 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); +}