diff --git a/2022/rust/src/day3.rs b/2022/rust/src/day3.rs index 02222e5..c922e8e 100644 --- a/2022/rust/src/day3.rs +++ b/2022/rust/src/day3.rs @@ -1,6 +1,7 @@ mod common; use std::collections::HashSet; +use std::iter::FromIterator; fn main() { let input = common::day_input(3); @@ -33,11 +34,25 @@ fn part1(input: &str) -> i32 { n += 1 } } - return r; + r } fn part2(input: &str) -> i32 { - 0 + let mut r = 0_i32; + for t in input.lines().collect::>().chunks_exact(3) { + let a: HashSet = HashSet::from_iter(t[0].bytes()); + let b: HashSet = HashSet::from_iter(t[1].bytes()); + let c: HashSet = HashSet::from_iter(t[2].bytes()); + let ab = HashSet::from_iter(a.intersection(&b).cloned()); + let b = ab.intersection(&c).next().unwrap(); + if *b >= 97 && *b <= 122 { + r += (*b as i32) - 96; + } else { + r += (*b as i32) - 65 + 27; + } + println!("+{} = {}", b, r); + } + r } #[cfg(test)] @@ -58,6 +73,6 @@ CrZsJsPPZsGzwwsLwLmpwMDw"; #[test] fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) + assert_eq!(part2(TEST_INPUT), 70) } }