diff --git a/aoc-01/src/main.rs b/aoc-01/src/main.rs index a66b61c..fc32e04 100644 --- a/aoc-01/src/main.rs +++ b/aoc-01/src/main.rs @@ -23,18 +23,37 @@ impl LocationList { } } - fn check_dupes(&mut self) { - for (outer_pos, entry_outer) in self.left.iter().enumerate() { - for (inner_pos, entry_inner) in self.right.iter().enumerate() { - if entry_inner == entry_outer { - println!( - "Outer: {}, Inner: {}, O_Pos: {}, I_POS: {}", - entry_outer, entry_inner, outer_pos, inner_pos - ) - } + fn solve(&mut self) { + self.left.sort(); + self.right.sort(); + + let mut nums = Vec::::new(); + for (pos, entry) in self.left.iter().enumerate() { + if entry > &self.right[pos] { + nums.push(entry - self.right[pos]) + } else { + nums.push(self.right[pos] - entry) } } + let mut total = 0; + for num in nums { + total += num; + } + println!("{}", total); } + + // fn check_dupes(&mut self) { + // for (outer_pos, entry_outer) in self.left.iter().enumerate() { + // for (inner_pos, entry_inner) in self.right.iter().enumerate() { + // if entry_inner == entry_outer { + // println!( + // "Outer: {}, Inner: {}, O_Pos: {}, I_POS: {}", + // entry_outer, entry_inner, outer_pos, inner_pos + // ) + // } + // } + // } + // } } impl fmt::Debug for LocationList { @@ -66,5 +85,5 @@ fn main() { location_list.parse_data(data); - println!("{:?}", location_list.check_dupes()); + location_list.solve(); }