|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fs::File; |
| 3 | +use std::io::BufRead; |
| 4 | +use std::io::BufReader; |
| 5 | + |
| 6 | +use crate::days::common::generic_day; |
| 7 | + |
| 8 | +pub struct Day05 { |
| 9 | + input_file: String, |
| 10 | + page_ordering_rules: HashMap<i64, Vec<i64>>, |
| 11 | + updates: Vec<Vec<i64>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Day05 { |
| 15 | + pub fn new(input_folder: String) -> Day05 { |
| 16 | + let mut day05: Day05 = Day05 { |
| 17 | + input_file: format!("{}/day05.txt", input_folder), |
| 18 | + page_ordering_rules: HashMap::new(), |
| 19 | + updates: Vec::new(), |
| 20 | + }; |
| 21 | + day05.parse_input(); |
| 22 | + day05 |
| 23 | + } |
| 24 | + |
| 25 | + fn process_page_ordering_rules(&mut self, line: String) { |
| 26 | + let pages = line |
| 27 | + .split("|") |
| 28 | + .map(|x| x.parse::<i64>().unwrap()) |
| 29 | + .collect::<Vec<_>>(); |
| 30 | + self.page_ordering_rules |
| 31 | + .entry(pages[0]) |
| 32 | + .and_modify(|x| x.push(pages[1])) |
| 33 | + .or_insert(vec![pages[1]]); |
| 34 | + } |
| 35 | + |
| 36 | + fn process_update(&mut self, line: String) { |
| 37 | + self.updates.push( |
| 38 | + line.split(",") |
| 39 | + .map(|x| x.parse::<i64>().unwrap()) |
| 40 | + .collect::<Vec<_>>(), |
| 41 | + ); |
| 42 | + } |
| 43 | + fn parse_input(&mut self) { |
| 44 | + let lines = BufReader::new(File::open(&self.input_file).unwrap()) |
| 45 | + .lines() |
| 46 | + .map(|x| x.unwrap()) |
| 47 | + .collect::<Vec<_>>(); |
| 48 | + |
| 49 | + let mut all_page_ordering_rules_processed: bool = false; |
| 50 | + for line in lines { |
| 51 | + if line == "" { |
| 52 | + all_page_ordering_rules_processed = true; |
| 53 | + } else if !all_page_ordering_rules_processed { |
| 54 | + self.process_page_ordering_rules(line); |
| 55 | + } else { |
| 56 | + self.process_update(line); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + fn is_update_correctly_ordered(&self, update: &Vec<i64>) -> bool { |
| 61 | + let mut result = true; |
| 62 | + |
| 63 | + for (position, page) in update.iter().enumerate() { |
| 64 | + if self.page_ordering_rules.contains_key(page) { |
| 65 | + for not_allowed_previous_page in &self.page_ordering_rules[page] { |
| 66 | + if update[0..position].contains(¬_allowed_previous_page) { |
| 67 | + result = false; |
| 68 | + break; |
| 69 | + } |
| 70 | + if !result { |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + result |
| 77 | + } |
| 78 | + |
| 79 | + fn correct_update(&self, update: &Vec<i64>) -> Vec<i64> { |
| 80 | + let mut corrected_update = update.clone(); |
| 81 | + |
| 82 | + while !self.is_update_correctly_ordered(&corrected_update) { |
| 83 | + let mut positions_to_swap = vec![0, 0]; |
| 84 | + for (position, page) in corrected_update.iter().enumerate() { |
| 85 | + if self.page_ordering_rules.contains_key(page) { |
| 86 | + for not_allowed_previous_page in &self.page_ordering_rules[page] { |
| 87 | + if corrected_update[0..position].contains(¬_allowed_previous_page) { |
| 88 | + let position_to_swap_with = corrected_update |
| 89 | + .iter() |
| 90 | + .position(|p| p == not_allowed_previous_page) |
| 91 | + .unwrap(); |
| 92 | + positions_to_swap[0]=position; |
| 93 | + positions_to_swap[1]=position_to_swap_with; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + corrected_update.swap(positions_to_swap[0], positions_to_swap[1]); |
| 100 | + } |
| 101 | + corrected_update |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl generic_day::GenericDay for Day05 { |
| 106 | + fn part1(&self) -> i64 { |
| 107 | + let mut result = 0; |
| 108 | + for update in &self.updates { |
| 109 | + if self.is_update_correctly_ordered(update) { |
| 110 | + result += update[update.len() / 2]; |
| 111 | + } |
| 112 | + } |
| 113 | + result |
| 114 | + } |
| 115 | + |
| 116 | + fn part2(&self) -> i64 { |
| 117 | + let mut result = 0; |
| 118 | + for update in &self.updates { |
| 119 | + if !self.is_update_correctly_ordered(update) { |
| 120 | + let corrected_update = self.correct_update(update); |
| 121 | + result += corrected_update[corrected_update.len() / 2]; |
| 122 | + } |
| 123 | + } |
| 124 | + result |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod tests { |
| 130 | + use super::*; |
| 131 | + use crate::days::GenericDay; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn result_part1() { |
| 135 | + let day05: Day05 = Day05::new(String::from("input_examples")); |
| 136 | + assert_eq!(day05.part1(), 143); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn result_part2() { |
| 141 | + let day05: Day05 = Day05::new(String::from("input_examples")); |
| 142 | + assert_eq!(day05.part2(), 123); |
| 143 | + } |
| 144 | +} |
0 commit comments