diff --git a/src/bin/2024/02.rs b/src/bin/2024/02.rs index cbbd67d..fd7e1f4 100644 --- a/src/bin/2024/02.rs +++ b/src/bin/2024/02.rs @@ -1,6 +1,4 @@ -use std::fmt::Display; - -use advent_of_code::strings::{convert_to_array, parsenumber}; +use advent_of_code::{strings::{convert_to_array, parsenumber}, KD}; #[allow(unused_imports)] use advent_of_code::{include_data, include_example}; @@ -10,22 +8,6 @@ fn get_numbers(input: &str) -> Vec { input.split(char::is_whitespace).map(parsenumber).collect() } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] -enum Direction { - Up, - Down, -} - -impl Display for Direction { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let _ = f.write_str(match *self { - Direction::Down => "Down", - Direction::Up => "Up", - }); - Ok(()) - } -} - trait SafeCheck { fn safe(&self) -> bool; } @@ -74,9 +56,9 @@ fn safe(record: Vec) -> bool { } } let direction = if ups > downs { - Direction::Up + KD::Top } else { - Direction::Down + KD::Bottom }; let mut errors = 0; i = record.clone().into_iter(); @@ -89,8 +71,9 @@ fn safe(record: Vec) -> bool { continue; } match (direction, last > level) { - (Direction::Up, false) | (Direction::Down, true) => last = level, - (Direction::Up, true) | (Direction::Down, false) => errors += 1, + (KD::Top, false) | (KD::Bottom, true) => last = level, + (KD::Top, true) | (KD::Bottom, false) => errors += 1, + _=>unreachable!() } } if errors > 0 { @@ -112,9 +95,6 @@ fn main() { safe_with_dampener += 1; } } - if safe_reports != 598 || safe_with_dampener == 623 { - println!("Broken implementation") - } println!("Safe Reports: {safe_reports}"); println!("Safe Reports with dampener: {safe_with_dampener}") } diff --git a/src/bin/2024/03.rs b/src/bin/2024/03.rs index ee82bbe..134aa67 100644 --- a/src/bin/2024/03.rs +++ b/src/bin/2024/03.rs @@ -5,38 +5,41 @@ use regex::{Captures, Regex}; include_data!(DATA 2024 03); -fn main() { - let part1re = Regex::new(r"mul\((?\d{1,3}),(?\d{1,3})\)").unwrap(); - let multiplies: Vec<(u32, u32)> = part1re - .captures_iter(DATA) - .map(|capture| { - let i = capture.name("i").unwrap().as_str(); - let j = capture.name("j").unwrap().as_str(); - let a = parsenumber(i); - let b = parsenumber(j); - (a, b) - }) - .collect(); +fn parse_part1(input : Vec>) -> u32 { let mut sum = 0; - for (i, j) in multiplies { - sum += i * j; + let (mut i,mut j): (u32, u32); + for capture in input { + i = parsenumber(capture.name("i").unwrap().as_str()); + j=parsenumber(capture.name("j").unwrap().as_str()); + sum += i * j } - println!("The Cleaned output is: {}", sum); - let part2re = Regex::new(r"(mul\((?\d{1,3}),(?\d{1,3})\)|do\(\)|don't\(\))").unwrap(); - let inputs: Vec> = part2re.captures_iter(DATA).collect(); - let mut parse = true; + sum +} + +fn parse_part2(input : Vec>)->u32 { let mut sum = 0; - for capture in inputs { + let mut parse = true; + let (mut i,mut j): (u32, u32); + for capture in input { match (capture.get(1).unwrap().as_str(), parse) { ("do()", _) => parse = true, ("don't()", _) => parse = false, (_, true) => { - let a = parsenumber::(capture.name("i").unwrap().as_str()); - let b = parsenumber::(capture.name("j").unwrap().as_str()); - sum += a * b + i = parsenumber(capture.name("i").unwrap().as_str()); + j = parsenumber(capture.name("j").unwrap().as_str()); + sum += i * j }, (_, _) => {}, } } + sum +} + +fn main() { + let part1 = Regex::new(r"mul\((?\d{1,3}),(?\d{1,3})\)").unwrap(); + let part2 = Regex::new(r"(mul\((?\d{1,3}),(?\d{1,3})\)|do\(\)|don't\(\))").unwrap(); + let sum = parse_part1(part1.captures_iter(DATA).collect()); + println!("The Cleaned output is: {}", sum); + let sum = parse_part2(part2.captures_iter(DATA).collect()); println!("The Cleaned with check output is: {}", sum); }