|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::utils; |
| 4 | + |
| 5 | +pub struct _5 { |
| 6 | + section_1: HashMap<u16, Vec<u16>>, |
| 7 | + section_2: Vec<Vec<u16>>, |
| 8 | +} |
| 9 | + |
| 10 | +pub fn init() -> Result<_5, ()> { |
| 11 | + let mut _5 = _5 { |
| 12 | + section_1: HashMap::new(), |
| 13 | + section_2: vec![], |
| 14 | + }; |
| 15 | + |
| 16 | + match utils::http::request(5) { |
| 17 | + Ok(input_string) => _5.parse_input(&input_string), |
| 18 | + Err(()) => Err(()), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl _5 { |
| 23 | + fn parse_input(mut self, string: &String) -> Result<_5, ()> { |
| 24 | + let sections = string.split("\n\n").collect::<Vec<&str>>(); |
| 25 | + |
| 26 | + for section_line in sections[0].split("\n") { |
| 27 | + let mut numbers = vec![]; |
| 28 | + |
| 29 | + for number_str in section_line.split("|") { |
| 30 | + if number_str.len() == 0 { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + match number_str.parse::<u16>() { |
| 35 | + Ok(number) => numbers.push(number), |
| 36 | + Err(err) => { |
| 37 | + eprintln!("An error occurred while parsing the input: {}", &err); |
| 38 | + return Err(()); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + match self.section_1.get(&numbers[0]) { |
| 44 | + Some(entries) => { |
| 45 | + let mut new_entries = entries.clone(); |
| 46 | + new_entries.push(numbers[1]); |
| 47 | + |
| 48 | + self.section_1.insert(numbers[0], new_entries); |
| 49 | + } |
| 50 | + None => { |
| 51 | + self.section_1.insert(numbers[0], vec![numbers[1]]); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + for section_line in sections[1].split("\n") { |
| 57 | + let mut values = vec![]; |
| 58 | + |
| 59 | + for value in section_line.split(",") { |
| 60 | + if value.len() == 0 { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + match value.parse::<u16>() { |
| 65 | + Ok(number) => values.push(number), |
| 66 | + Err(err) => { |
| 67 | + eprintln!("An error occurred while parsing the input: {}", &err); |
| 68 | + return Err(()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + self.section_2.push(values); |
| 74 | + } |
| 75 | + |
| 76 | + Ok(self) |
| 77 | + } |
| 78 | + |
| 79 | + fn part1(&self) -> String { |
| 80 | + let mut result = 0; |
| 81 | + |
| 82 | + for array in self.section_2.iter() { |
| 83 | + if array.len() == 0 { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + if self.is_valid(&array).0 { |
| 88 | + result += array.get(array.len() / 2).unwrap(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + result.to_string() |
| 93 | + } |
| 94 | + |
| 95 | + fn part2(&mut self) -> String { |
| 96 | + let mut result = 0; |
| 97 | + |
| 98 | + for array in self.section_2.iter() { |
| 99 | + if array.len() == 0 { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + let (is_valid, p_bad_index, p_number_index, p_number) = self.is_valid(&array); |
| 104 | + |
| 105 | + if !is_valid { |
| 106 | + let mut c_array = array.clone(); |
| 107 | + |
| 108 | + c_array.remove(p_bad_index.unwrap()); |
| 109 | + c_array.insert(p_number_index.unwrap(), p_number.unwrap()); |
| 110 | + |
| 111 | + loop { |
| 112 | + let (c_is_valid, c_p_bad_index, c_p_number_index, c_p_number) = |
| 113 | + self.is_valid(&c_array); |
| 114 | + |
| 115 | + if c_is_valid { |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + c_array.remove(c_p_bad_index.unwrap()); |
| 120 | + c_array.insert(c_p_number_index.unwrap(), c_p_number.unwrap()); |
| 121 | + } |
| 122 | + |
| 123 | + result += c_array.get(c_array.len() / 2).unwrap(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + result.to_string() |
| 128 | + } |
| 129 | + |
| 130 | + fn is_valid(&self, array: &Vec<u16>) -> (bool, Option<usize>, Option<usize>, Option<u16>) { |
| 131 | + let mut i_array = array.clone(); |
| 132 | + |
| 133 | + for (index, number) in array.iter().enumerate().rev() { |
| 134 | + i_array.remove(index); |
| 135 | + |
| 136 | + for compared_number in self.section_1.get(number).unwrap_or(&vec![]).iter() { |
| 137 | + for (i_index, i_number) in i_array.iter().enumerate() { |
| 138 | + if i_number == compared_number { |
| 139 | + return (false, Some(i_index), Some(index), Some(*compared_number)); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + (true, None, None, None) |
| 146 | + } |
| 147 | + |
| 148 | + pub fn results(mut self) -> Vec<String> { |
| 149 | + vec![self.part1(), self.part2()] |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#[cfg(test)] |
| 154 | +mod tests { |
| 155 | + use std::{collections::HashMap, fs}; |
| 156 | + |
| 157 | + use super::_5; |
| 158 | + |
| 159 | + pub fn test_init() -> Result<_5, ()> { |
| 160 | + let mut _5 = _5 { |
| 161 | + section_1: HashMap::new(), |
| 162 | + section_2: vec![], |
| 163 | + }; |
| 164 | + |
| 165 | + match fs::read_to_string("input_examples/day5.txt") { |
| 166 | + Ok(input_string) => _5.parse_input(&input_string), |
| 167 | + Err(err) => { |
| 168 | + eprintln!( |
| 169 | + "An error occurred while loading the example input: {}", |
| 170 | + &err |
| 171 | + ); |
| 172 | + Err(()) |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn part1_test() { |
| 179 | + let _5 = test_init().unwrap(); |
| 180 | + assert_eq!(143.to_string(), _5.part1()) |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn part2_test() { |
| 185 | + let mut _5 = test_init().unwrap(); |
| 186 | + assert_eq!(123.to_string(), _5.part2()) |
| 187 | + } |
| 188 | +} |
0 commit comments