use std::iter::zip; #[path = "../lib.rs"] mod lib; fn part1(list1: &Vec, list2: &Vec) -> u32 { return zip(list1.iter(), list2.iter()).map(|(a, b)| a.abs_diff(*b)).sum(); } fn part2(list1: &Vec, list2: &Vec) -> u32 { let n = list1.len(); let mut v = 0; let mut i1 = 0; let mut i2 = 0; let mut k1 = 0; let mut k2 = 0; let mut sum = 0; while i1 < n && i2 < n { if v == 0 { if list1[i1] == list2[i2] { v = list1[i1]; k1 = 1; k2 = 1; i1 += 1; i2 += 1; } else if list1[i1] < list2[i2] { i1 += 1; } else { i2 += 1; } } else { if v == list1[i1] { i1 += 1; k1 += 1; } else if v == list2[i2] { i2 += 1; k2 += 1; } else { sum += v * k1 * k2; v = 0; } } } sum += v * k1 * k2; return sum; } fn main() { let mut list1 = vec![]; let mut list2 = vec![]; for line in lib::iter_input() { let (left, right) = line.split_once(" ").unwrap(); list1.push(left.parse::().unwrap()); list2.push(right.parse::().unwrap()); } list1.sort(); list2.sort(); println!("part1: {}", part1(&list1, &list2)); println!("part2: {}", part2(&list1, &list2)); }