Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 4: Ceres Search #33

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Development occurs in language-specific directories:
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)||
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)|[day4.rs](rs/src/day4.rs)|
8 changes: 7 additions & 1 deletion rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::{day1, day2, day3};
use aoc2024::{day1, day2, day3, day4};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -32,6 +32,12 @@ fn aoc2024_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day3::part2(black_box(&data))));
g.finish();

let data = get_day_input(4)?;
let mut g = c.benchmark_group("day 4");
g.bench_function("part 1", |b| b.iter(|| day4::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day4::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion rs/src/day3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn part1(data: &str) -> i32 {
let b = b.parse::<i32>().ok()?;
Some(a * b)
})
.sum::<i32>()
.sum()
}

pub fn part2(data: &str) -> i32 {
Expand Down
78 changes: 78 additions & 0 deletions rs/src/day4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use itertools::izip;

pub fn part1(data: &str) -> usize {
let lines = data.lines().collect::<Vec<_>>();
let mut result = 0;
for (y, line) in lines.iter().enumerate() {
for x in 0..line.len() {
for dy in -1..=1 {
for dx in -1..=1 {
if "XMAS".bytes().enumerate().all(|(i, b)| {
y.checked_add_signed(i as isize * dy)
.and_then(|y| lines.get(y))
.and_then(|line| {
x.checked_add_signed(i as isize * dx)
.and_then(|x| line.as_bytes().get(x))
})
== Some(&b)
}) {
result += 1;
}
}
}
}
}
result
}

pub fn part2(data: &str) -> usize {
let lines = data.lines().collect::<Vec<_>>();
let mut result = 0;
for (above, line, below) in izip!(&lines[..], &lines[1..], &lines[2..]) {
for (nw, ne, b, sw, se) in izip!(
above.bytes(),
above.bytes().skip(2),
line.bytes().skip(1),
below.bytes(),
below.bytes().skip(2)
) {
if b == b'A'
&& (nw == b'M' && se == b'S' || se == b'M' && nw == b'S')
&& (ne == b'M' && sw == b'S' || sw == b'M' && ne == b'S')
{
result += 1;
}
}
}
result
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

static EXAMPLE: &str = indoc! {"
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"};

#[test]
fn part1_examples() {
assert_eq!(18, part1(EXAMPLE));
}

#[test]
fn part2_examples() {
assert_eq!(9, part2(EXAMPLE));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::{day1, day2, day3};
use aoc2024::{day1, day2, day3, day4};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -40,5 +40,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("4") {
println!("Day 4");
let data = get_day_input(4)?;
println!("{:?}", day4::part1(&data));
println!("{:?}", day4::part2(&data));
println!();
}

Ok(())
}
Loading