feat: day 4

This commit is contained in:
Lucy 2024-12-04 08:59:07 +01:00
parent 3fd2674cb9
commit 7f811ce999
3 changed files with 142 additions and 0 deletions

10
inputs/day04/example.txt Normal file
View file

@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX

130
src/day04.rs Normal file
View file

@ -0,0 +1,130 @@
pub fn pt1(input: &str) -> usize {
let lines = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut count = 0;
for (i, line) in lines.iter().enumerate() {
for (j, c) in line.iter().enumerate() {
if *c == 'X' {
if j >= 3
&& line.get(j.saturating_sub(1)) == Some(&'M')
&& line.get(j.saturating_sub(2)) == Some(&'A')
&& line.get(j.saturating_sub(3)) == Some(&'S')
{
count += 1;
}
if j + 3 <= line.len()
&& line.get(j + 1) == Some(&'M')
&& line.get(j + 2) == Some(&'A')
&& line.get(j + 3) == Some(&'S')
{
count += 1;
}
if i >= 3
&& lines.get(i.saturating_sub(1)).and_then(|l| l.get(j)) == Some(&'M')
&& lines.get(i.saturating_sub(2)).and_then(|l| l.get(j)) == Some(&'A')
&& lines.get(i.saturating_sub(3)).and_then(|l| l.get(j)) == Some(&'S')
{
count += 1;
}
if i + 3 <= lines.len()
&& lines.get(i + 1).and_then(|l| l.get(j)) == Some(&'M')
&& lines.get(i + 2).and_then(|l| l.get(j)) == Some(&'A')
&& lines.get(i + 3).and_then(|l| l.get(j)) == Some(&'S')
{
count += 1;
}
if i >= 3
&& j >= 3
&& lines
.get(i.saturating_sub(1))
.and_then(|l| l.get(j.saturating_sub(1)))
== Some(&'M')
&& lines
.get(i.saturating_sub(2))
.and_then(|l| l.get(j.saturating_sub(2)))
== Some(&'A')
&& lines
.get(i.saturating_sub(3))
.and_then(|l| l.get(j.saturating_sub(3)))
== Some(&'S')
{
count += 1;
}
if i >= 3
&& j + 3 <= line.len()
&& lines.get(i.saturating_sub(1)).and_then(|l| l.get(j + 1)) == Some(&'M')
&& lines.get(i.saturating_sub(2)).and_then(|l| l.get(j + 2)) == Some(&'A')
&& lines.get(i.saturating_sub(3)).and_then(|l| l.get(j + 3)) == Some(&'S')
{
count += 1;
}
if j >= 3
&& i + 3 <= lines.len()
&& lines.get(i + 1).and_then(|l| l.get(j.saturating_sub(1))) == Some(&'M')
&& lines.get(i + 2).and_then(|l| l.get(j.saturating_sub(2))) == Some(&'A')
&& lines.get(i + 3).and_then(|l| l.get(j.saturating_sub(3))) == Some(&'S')
{
count += 1;
}
if j + 3 <= line.len()
&& i + 3 <= lines.len()
&& lines.get(i + 1).and_then(|l| l.get(j + 1)) == Some(&'M')
&& lines.get(i + 2).and_then(|l| l.get(j + 2)) == Some(&'A')
&& lines.get(i + 3).and_then(|l| l.get(j + 3)) == Some(&'S')
{
count += 1;
}
}
}
}
count
}
pub fn pt2(input: &str) -> usize {
let lines = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut count = 0;
for (i, line) in lines.iter().enumerate() {
for (j, c) in line.iter().enumerate() {
if *c == 'A' && (j >= 1 && i >= 1 && j + 1 < line.len() && i + 1 < lines.len()) {
let top_left = lines.get(i - 1).and_then(|l| l.get(j - 1));
let bottom_right = lines.get(i + 1).and_then(|l| l.get(j + 1));
let bottom_left = lines.get(i + 1).and_then(|l| l.get(j - 1));
let top_right = lines.get(i - 1).and_then(|l| l.get(j + 1));
if ((top_left == Some(&'M') && bottom_right == Some(&'S'))
|| (top_left == Some(&'S') && bottom_right == Some(&'M')))
&& ((bottom_left == Some(&'M') && top_right == Some(&'S'))
|| (bottom_left == Some(&'S') && top_right == Some(&'M')))
{
count += 1;
}
}
}
}
count
}
#[cfg(test)]
mod tests {
use std::fs::read_to_string;
use super::{pt1, pt2};
#[test]
pub fn test_pt1() {
let input =
read_to_string("./inputs/day04/example.txt").expect("Missing example.txt for day04");
assert_eq!(pt1(&input), 18)
}
#[test]
pub fn test_pt2() {
let input =
read_to_string("./inputs/day04/example.txt").expect("Missing example_2.txt for day04");
assert_eq!(pt2(&input), 9)
}
}

View file

@ -3,12 +3,14 @@ use std::{fmt::Display, fs::read_to_string, time::Instant};
mod day01; mod day01;
mod day02; mod day02;
mod day03; mod day03;
mod day04;
fn main() { fn main() {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
solve_day(1, day01::pt1, day01::pt2); solve_day(1, day01::pt1, day01::pt2);
solve_day(2, day02::pt1, day02::pt2); solve_day(2, day02::pt1, day02::pt2);
solve_day(3, day03::pt1, day03::pt2); solve_day(3, day03::pt1, day03::pt2);
solve_day(4, day04::pt1, day04::pt2);
} }
fn solve_day<Fa, Fb, Ta, Tb>(day: u8, part_a: Fa, part_b: Fb) fn solve_day<Fa, Fb, Ta, Tb>(day: u8, part_a: Fa, part_b: Fb)