From 3ccc1603262c155fb7462bff58a593c2a81c7a27 Mon Sep 17 00:00:00 2001 From: Pascal Phelipot Date: Sun, 3 Dec 2023 13:11:32 +0100 Subject: [PATCH] Day 3 completed ! --- src/day3/mod.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 8 +++---- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/day3/mod.rs b/src/day3/mod.rs index 91687b4..7925145 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -7,7 +7,20 @@ struct Match { value: u32, line: usize, range: Range, - is_valid: bool + is_valid: bool, + gears: Vec +} + +#[allow(dead_code)] +pub enum Part { + One, + Two +} + +#[derive(Debug, PartialEq)] +struct Point { + x: u32, + y: u32 } fn is_symbol(line: &[char], range: Range) -> bool { @@ -20,8 +33,21 @@ fn is_symbol(line: &[char], range: Range) -> bool { false } +fn find_gears(line: &[char], range: Range, line_offset: usize) -> Vec { + let mut results: Vec = Vec::new(); + let x_offset = range.start; + + for (i, c) in line[range].iter().enumerate() { + if *c == '*' { + results.push(Point { x: (x_offset + i) as u32, y: line_offset as u32 }) + } + } + + results +} + #[allow(dead_code)] -pub fn solve(input: Vec) -> u32 { +pub fn solve(input: Vec, part: Part) -> u32 { let regex = Regex::new(r"\d+").unwrap(); let mut matches: Vec = Vec::new(); @@ -31,7 +57,7 @@ pub fn solve(input: Vec) -> u32 { for (i, line) in input.iter().enumerate() { for capture in regex.captures_iter(line) { if let Some(capture) = capture.get(0) { - matches.push(Match { value: capture.as_str().parse::().unwrap(), line: i, range: capture.range(), is_valid: false }); + matches.push(Match { value: capture.as_str().parse::().unwrap(), line: i, range: capture.range(), is_valid: false, gears: Vec::new() }); } } dataset.push(line.chars().collect()); @@ -47,14 +73,19 @@ pub fn solve(input: Vec) -> u32 { if item.line >= 1 { b1 = is_symbol(dataset.get(item.line-1).unwrap(), range.to_owned()); + item.gears.append(&mut find_gears(dataset.get(item.line-1).unwrap(), range.to_owned(), item.line-1)); + println!("\tChecking line #{} - {}\t'{:?}'", item.line-1, b1, &dataset.get(item.line-1).unwrap()[range.to_owned()]); } let b2 = is_symbol(dataset.get(item.line).unwrap(), range.to_owned()); + item.gears.append(&mut find_gears(dataset.get(item.line).unwrap(), range.to_owned(), item.line)); println!("\tChecking line #{} - {}\t'{:?}'", item.line, b2, &dataset.get(item.line).unwrap()[range.to_owned()]); if item.line < dataset.len() - 1 { b3 = is_symbol(dataset.get(item.line+1).unwrap(), range.to_owned()); + item.gears.append(&mut find_gears(dataset.get(item.line+1).unwrap(), range.to_owned(), item.line+1)); + println!("\tChecking line #{} - {}\t'{:?}'", item.line+1, b1, &dataset.get(item.line+1).unwrap()[range.to_owned()]); } @@ -68,7 +99,22 @@ pub fn solve(input: Vec) -> u32 { } } - sum + let mut sum_gears = 0; + for (i, item) in matches.iter().enumerate() { + for gears_point in &item.gears { + for other_match in &matches[(i+1)..] { + if other_match.gears.contains(gears_point) { + println!("Match gear at {:?} -> {} * {}", gears_point, item.value, other_match.value); + sum_gears += item.value * other_match.value; + } + } + } + } + + match part { + Part::One => sum, + Part::Two => sum_gears + } } @@ -80,6 +126,13 @@ mod tests { fn parse() { let input: Vec = "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598..".lines().map(|v| v.to_owned()).collect(); - assert_eq!(solve(input), 4361); + assert_eq!(solve(input, Part::One), 4361); + } + + #[test] + fn parse_part2() { + let input: Vec = "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598..".lines().map(|v| v.to_owned()).collect(); + + assert_eq!(solve(input, Part::Two), 467835); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2ba712a..20b97d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,6 @@ mod utils; fn main() { - let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt"; - let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3")); - - println!("result: {}", result); } @@ -44,8 +40,10 @@ mod tests { #[test] fn day3() { let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt"; - let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3")); + let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::One); + let result2 = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::Two); assert_eq!(result, 522726); + assert_eq!(result2, 81721933); } } \ No newline at end of file