diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-12-03 22:51:44 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-12-04 09:08:47 +0800 |
| commit | 4250e02b0f08e3389fdef0500de08ab74ffa2611 (patch) | |
| tree | 063b75d1c0eca72bf7419b304df1735e8e6a11bc /2025/d3.rs | |
| parent | c8780d06a13dd08bccc1ce29e0b7cd60ff65f259 (diff) | |
refactor: structure of each day
Diffstat (limited to '2025/d3.rs')
| -rw-r--r-- | 2025/d3.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/2025/d3.rs b/2025/d3.rs new file mode 100644 index 0000000..3d56ed6 --- /dev/null +++ b/2025/d3.rs @@ -0,0 +1,120 @@ +use aoc::{fetch::fetch, joltage::Joltage, read::read}; + +struct JoltageBank { + inner: Vec<Joltage>, +} + +impl JoltageBank { + fn new(inner: Vec<Joltage>) -> Self { + Self { + inner, + } + } + + fn get_biggest_two(&mut self) -> usize { + let mut res = 0; + for i in 0..self + .inner + .len() + { + for j in (i + 1) + ..self + .inner + .len() + { + let max1 = *self.inner[i] as usize; + let max2 = *self.inner[j] as usize; + let value = max1 * 10 + max2; + if value > res { + res = value; + } + } + } + res + } + + fn get_biggest_12(&mut self) -> usize { + let digits: Vec<usize> = self + .inner + .iter() + .map(|j| **j as usize) + .collect(); + let n = digits.len(); + let k = 12; + let to_remove = n - k; + + // Use monotonic stack to find largest number by removing `to_remove` digits + let mut stack: Vec<usize> = Vec::new(); + let mut removed = 0; + + for &digit in &digits { + while removed < to_remove + && !stack.is_empty() + && *stack + .last() + .unwrap() + < digit + { + stack.pop(); + removed += 1; + } + stack.push(digit); + } + + // If we haven't removed enough, remove from the end + while stack.len() > k { + stack.pop(); + } + + let mut res = 0; + for val in stack { + res = res * 10 + val; + } + res + } +} + +pub fn part1() { + let input = fetch(3, '\n'); + let mut res = 0; + for i in input { + if i.is_empty() { + break; + } + let mut vec = Vec::new(); + for c in i.chars() { + vec.push( + Joltage::parse( + c.to_digit(10) + .unwrap() as u8, + ) + .unwrap(), + ); + } + res += JoltageBank::new(vec).get_biggest_two(); + } + println!("{res}"); +} + +pub fn part2() { + // let input = fetch(3, '\n'); + let input = read("d", '\n'); + let mut res = 0; + for i in input { + if i.is_empty() { + break; + } + let mut vec = Vec::new(); + for c in i.chars() { + vec.push( + Joltage::parse( + c.to_digit(10) + .unwrap() as u8, + ) + .unwrap(), + ); + } + res += JoltageBank::new(vec).get_biggest_12(); + } + println!("{res}"); +} |
