From 122d56170d6cdeff928442b996cfac9c0fbdb31f Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Wed, 4 Dec 2024 13:56:04 +0100 Subject: [PATCH] Add day4 --- day4/Cargo.lock | 7 +++ day4/Cargo.toml | 6 +++ day4/src/main.rs | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 day4/Cargo.lock create mode 100644 day4/Cargo.toml create mode 100644 day4/src/main.rs diff --git a/day4/Cargo.lock b/day4/Cargo.lock new file mode 100644 index 0000000..a08bce7 --- /dev/null +++ b/day4/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day4" +version = "0.1.0" diff --git a/day4/Cargo.toml b/day4/Cargo.toml new file mode 100644 index 0000000..0ebbbc8 --- /dev/null +++ b/day4/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day4" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day4/src/main.rs b/day4/src/main.rs new file mode 100644 index 0000000..cf9971f --- /dev/null +++ b/day4/src/main.rs @@ -0,0 +1,109 @@ +fn parse(input: &str) -> Vec> { + input.lines().map(|line| line.chars().collect()).collect() +} + +fn part1(input: &[Vec]) -> u32 { + let xmas = ['X', 'M', 'A', 'S']; + let directions: [(i32, i32); 8] = [ + (0, 1), + (0, -1), + (1, 0), + (-1, 0), + (1, 1), + (-1, -1), + (1, -1), + (-1, 1), + ]; + input + .iter() + .enumerate() + .map(|(line_idx, line)| { + line.iter() + .enumerate() + .map(|(col_idx, c)| { + if c != &xmas[0] { + return 0; + } + directions + .iter() + .map(|(dir_x, dir_y)| { + xmas.iter() + .enumerate() + .skip(1) + .fold(1, |acc, (shift, xmas_char)| { + let Some(Some(cc)) = input + .get((line_idx as i32 + (dir_y * (shift as i32))) as usize) + .map(|ll| { + ll.get( + (col_idx as i32 + dir_x * (shift as i32)) as usize, + ) + }) + else { + return 0; + }; + if cc != xmas_char { + return 0; + } + acc + }) + }) + .sum::() + }) + .sum::() + }) + .sum() +} + +fn part2(input: &[Vec]) -> u32 { + input + .iter() + .enumerate() + .map(|(line_idx, line)| { + line.iter() + .enumerate() + .map(|(col_idx, c)| { + if c != &'A' { + return 0; + } + let Some(Some(up_left)) = input + .get(line_idx.overflowing_sub(1).0) + .map(|l| l.get(col_idx.overflowing_sub(1).0)) + else { + return 0; + }; + let Some(Some(up_right)) = input + .get(line_idx.overflowing_sub(1).0) + .map(|l| l.get(col_idx + 1)) + else { + return 0; + }; + let Some(Some(down_left)) = input + .get(line_idx + 1) + .map(|l| l.get(col_idx.overflowing_sub(1).0)) + else { + return 0; + }; + let Some(Some(down_right)) = + input.get(line_idx + 1).map(|l| l.get(col_idx + 1)) + else { + return 0; + }; + if ((up_left == &'M' && down_right == &'S') + || (up_left == &'S' && down_right == &'M')) + && ((up_right == &'M' && down_left == &'S') + || (up_right == &'S' && down_left == &'M')) + { + 1 + } else { + 0 + } + }) + .sum::() + }) + .sum() +} +fn main() { + let input = parse(include_str!("../input.txt")); + println!("{}", part1(&input)); + println!("{}", part2(&input)); +}