From b2dcf20a27a10e5b1e080dcdc70fd2455cd1c5f8 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 4 Dec 2024 22:14:14 -0500 Subject: [PATCH] Day 4 solution! --- 2024/data/day4.txt | 140 ++++++++++++++++++++++++++++ 2024/src/day3.rs | 17 +++- 2024/src/day4.rs | 226 +++++++++++++++++++++++++++++++++++++++++++++ 2024/src/main.rs | 3 + 4 files changed, 381 insertions(+), 5 deletions(-) create mode 100644 2024/data/day4.txt create mode 100644 2024/src/day4.rs diff --git a/2024/data/day4.txt b/2024/data/day4.txt new file mode 100644 index 0000000..779643e --- /dev/null +++ b/2024/data/day4.txt @@ -0,0 +1,140 @@ +SAXMASMMMASAMMMMXXMMXMMMSSMMMXSAAXMSSMSSSSSSSSXSASXMSMMMSASXSXMASXMSSSMXSSMXSMSSXMXXSXMXAMXMASXASXMSAMSSSMMAAXAMMXXSAMMSXSSSSMSMMSMSAXSAMXMX +MXXAAMAAMAMASXSSSMMSAMMSAMASMMSXSAMXAAMAAAAAAXSXAMMMAMAAXAMMAXXAXAAAXMAMMAMAMMAMXMAXSASXSAMXAXMSMAMMAAXSAMXSSXMMMAMMASAMXAAAXAAAXAAMAMXAMXAM +XSXMASXMMXSAMXAAXMASASMMXXAMAXXAMXXSMMMMMMMMMMXMXMAMASASMMMAMMMMXMMMMSAMSAMAXMAMAMMXSAMXMMXMASXAMXMSMMMSMXMXAXSAMSXSAMASMMMMMSMSMMSMAMSXMASA +SMAMASMAMAMASMMMMMASMMXAAMMSSMMMMMXAXXXAMAMASXAASXXSASAXAAASAAAMASAXMSXXSMMMMSASXSMAMXMAMXAMXMXMMAMXAAAMXSMMMXMAMMAMSSXMMMSMAMXXXAAMAAXASAMS +XMAMAXXAMSSXSMSAXMXSXSMSMMXAXAXMASAMXMMMSMSASXMXSAMMMMXSMMMMSSXSXSASAMXMMMSMMMAMAAMXMMSXSAXXAMAXASASXMSSXXAAXMXMMMMMASXAXAAMSSXXMXSSXSMMM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MSMSMSAMAAAMSSSMSXAMMMSMASAMMASMSS +XSMXAXAAXXMAMAMXAMAMXMMXAAXMAMXMXSMSAAAXXMXXAAXMMSXMASXSMSMASMMMSMMMMSMSAASAMXSSMMSSSXMAXASXMMAMXMMMMMSMAAXXAMAMMSSMXXAXXXXMMXSAAMAMAXSMSMAA +SXXSAMMXXXMAMAMMAMXMSXMXAMXXAMMXASXSMSMXXSAMSSMMXMXMAMXMAMMMMXMAAXXAAXMMXMXAMAMAMAXAXMASMMMAASAMSMMSSMAMMSASAMXMAXMMSMMMSSXAXAMMMMXMMXMAXXMX +AXAAAMAASMSASXSMAMAMAASXSMMSASAMXSAXAMMSMMMMMAXMAMAMMMAMAMMMASMSMSSMSSMAMSSSMASXMSMSMMAXXMMMMMAXSAAAAXMSXXASXMAXMMMAASASAMXMMMSMSSMMSSMAMMXM +MMMMSMXXSAAAAAXXASASXXMAMAASXMASMMMMSMAAAASASMMSASASMSSMASXMAMAAXMASAXMAMXAASAMXAXAAAMSSSMAXXSXMSMMSSMXAAMAMXSXSXAMSSSSMASAXAMAXAAAAAAAMSAAA +XXXXMAMXMAMSMAMSXSMSXMMAMMMMXXMAMAXXXMSSSMSMSXMSASMSMXMAXSAMMSSMSSMMSSSMSMSMMSAMXMSMSMMAASXSAMXMXXMAXXSMSMSMMXXSMSXXAMXMASASMSSSSSMMSSMXAXSS diff --git a/2024/src/day3.rs b/2024/src/day3.rs index 497a3d5..a4c0e00 100644 --- a/2024/src/day3.rs +++ b/2024/src/day3.rs @@ -1,5 +1,9 @@ use nom::{ - branch::alt, bytes::complete::{tag, take_while}, character::{complete::anychar, is_digit}, combinator::eof, multi::{many1, many_till}, Err, Finish, IResult + branch::alt, + bytes::complete::{tag, take_while}, + character::{complete::anychar, is_digit}, + multi::{many1, many_till}, + Finish, IResult, }; use regex::Regex; @@ -119,10 +123,13 @@ fn match_mult(input: &str) -> IResult<&str, Operation> { let (rest, y) = take_while(|c| is_digit(c as u8))(rest)?; let (rest, _) = tag(")")(rest)?; - Ok((rest, Operation::Mult(Multiplication { - x: x.parse::().unwrap(), - y: y.parse::().unwrap(), - }))) + Ok(( + rest, + Operation::Mult(Multiplication { + x: x.parse::().unwrap(), + y: y.parse::().unwrap(), + }), + )) } #[cfg(test)] diff --git a/2024/src/day4.rs b/2024/src/day4.rs new file mode 100644 index 0000000..2009f5c --- /dev/null +++ b/2024/src/day4.rs @@ -0,0 +1,226 @@ +const INPUT: &'static str = include_str!("../data/day4.txt"); + +pub fn day4a() -> String { + let grid = Grid::from(INPUT); + format!("{}", find_xmas(&grid)) +} + +pub fn day4b() -> String { + let grid = Grid::from(INPUT); + format!("{}", find_x_mas(&grid)) +} + +struct Grid { + width: usize, + height: usize, + data: Vec, +} + +impl Grid { + fn elem(&self, x: usize, y: usize) -> char { + if y >= self.height || x >= self.width { + panic!("out of range") + } + self.data[y * self.width + x] + } +} + +impl From<&str> for Grid { + fn from(input: &str) -> Self { + let height = input.lines().count(); + + let first_row = input.lines().next().unwrap(); + let width = first_row.len(); + + let data = input.chars().filter(|c| *c != '\n').collect::>(); + + Self { + width, + height, + data, + } + } +} + +#[derive(Copy, Clone)] +enum Direction { + East, + SouthEast, + South, + SouthWest, + West, + NorthWest, + North, + NorthEast, +} + +fn find_xmas(grid: &Grid) -> usize { + let mut acc = 0; + for x in 0..grid.width { + for y in 0..grid.height { + if check_xmas(grid, x, y, Direction::East) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::SouthEast) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::South) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::SouthWest) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::West) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::NorthWest) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::North) { + acc += 1; + } + if check_xmas(grid, x, y, Direction::NorthEast) { + acc += 1; + } + } + } + + acc +} + +fn check_xmas(grid: &Grid, x: usize, y: usize, direction: Direction) -> bool { + let mut addr: Option<(usize, usize)> = Some((x, y)); + + for c in "XMAS".chars() { + if let Some((cx, cy)) = addr { + if grid.elem(cx, cy) == c { + addr = move_pointer(grid, cx, cy, direction); + } else { + return false; + } + } else { + return false; + } + } + + return true; +} + +fn find_x_mas(grid: &Grid) -> usize { + let mut acc = 0; + for x in 1..grid.width - 1 { + for y in 1..grid.width - 1 { + if check_x_mas(grid, x, y) { + acc += 1; + } + } + } + + acc +} + +fn check_x_mas(grid: &Grid, x: usize, y: usize) -> bool { + if grid.elem(x, y) != 'A' { + return false; + } + + let nw = move_pointer(&grid, x, y, Direction::NorthWest).map(|(cx, cy)| grid.elem(cx, cy)); + let ne = move_pointer(&grid, x, y, Direction::NorthEast).map(|(cx, cy)| grid.elem(cx, cy)); + let se = move_pointer(&grid, x, y, Direction::SouthEast).map(|(cx, cy)| grid.elem(cx, cy)); + let sw = move_pointer(&grid, x, y, Direction::SouthWest).map(|(cx, cy)| grid.elem(cx, cy)); + + match (nw, ne, se, sw) { + (Some(nw), Some(ne), Some(se), Some(sw)) => { + // M . M + // . A . + // S . S + if nw == 'M' && se == 'S' && ne == 'M' && sw == 'S' { + return true; + }; + + // M . S + // . A . + // M . S + if nw == 'M' && se == 'S' && sw == 'M' && ne == 'S' { + return true; + }; + + // S . S + // . A . + // M . M + if sw == 'M' && ne == 'S' && se == 'M' && nw == 'S' { + return true; + }; + + // S . M + // . A . + // S . M + if se == 'M' && nw == 'S' && ne == 'M' && sw == 'S' { + return true; + }; + + false + } + _ => false, + } +} + +fn move_pointer(grid: &Grid, x: usize, y: usize, direction: Direction) -> Option<(usize, usize)> { + match direction { + Direction::East if x < grid.width - 1 => Some((x + 1, y)), + Direction::SouthEast if x < grid.width - 1 && y < grid.height - 1 => Some((x + 1, y + 1)), + Direction::South if y < grid.height - 1 => Some((x, y + 1)), + Direction::SouthWest if x > 0 && y < grid.height - 1 => Some((x - 1, y + 1)), + Direction::West if x > 0 => Some((x - 1, y)), + Direction::NorthWest if x > 0 && y > 0 => Some((x - 1, y - 1)), + Direction::North if y > 0 => Some((x, y - 1)), + Direction::NorthEast if x < grid.width - 1 && y > 0 => Some((x + 1, y - 1)), + _ => None, + } +} + +#[cfg(test)] +mod test { + use super::*; + + const TEST_INPUT: &'static str = "MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX"; + + #[test] + fn it_finds_xmas() { + let grid = Grid::from(TEST_INPUT); + assert_eq!(find_xmas(&grid), 18); + + let grid = Grid::from(INPUT); + assert_eq!(find_xmas(&grid), 2633); + } + + #[test] + fn it_finds_x_mas() { + let grid = Grid::from(TEST_INPUT); + assert_eq!(find_x_mas(&grid), 9); + + let grid = Grid::from(INPUT); + assert_eq!(find_x_mas(&grid), 1936); + } + + #[test] + fn it_parses_the_grid() { + let grid = Grid::from(TEST_INPUT); + assert_eq!(grid.width, 10); + assert_eq!(grid.height, 10); + assert_eq!(grid.elem(9, 0), 'M'); + assert_eq!(grid.elem(9, 1), 'A'); + assert_eq!(grid.elem(9, 2), 'M'); + assert_eq!(grid.elem(9, 3), 'X'); + assert_eq!(grid.elem(9, 9), 'X'); + } +} diff --git a/2024/src/main.rs b/2024/src/main.rs index 4067764..a020215 100644 --- a/2024/src/main.rs +++ b/2024/src/main.rs @@ -1,6 +1,7 @@ mod day1; mod day2; mod day3; +mod day4; fn main() { let day = std::env::args().skip(1).next(); @@ -12,6 +13,8 @@ fn main() { Some("2b") => day2::day2b(), Some("3a") => day3::day3a(), Some("3b") => day3::day3b(), + Some("4a") => day4::day4a(), + Some("4b") => day4::day4b(), _ => panic!("unrecognized day"), };