Skip to content

Commit 188955f

Browse files
committed
🧻
1 parent 5c67471 commit 188955f

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

‎aoc2025_wasm/inputs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use anyhow::{Context, Result};
2+
use ndarray::Array2;
3+
4+
pub fn make_byte_grid(raw_inp: &str) -> Result<Array2<u8>> {
5+
let columns = raw_inp
6+
.trim()
7+
.bytes()
8+
.position(|c| c == b'\n')
9+
.context("can't get column count")?;
10+
11+
Array2::from_shape_vec(
12+
((raw_inp.trim().len() + 1) / (columns + 1), columns),
13+
raw_inp.bytes().filter(|&x| x != b'\n').collect(),
14+
)
15+
.context("can't make array")
16+
}
17+
18+
pub fn make_bool_grid<const TRUE_CHAR: u8>(raw_inp: &str) -> Result<Array2<bool>> {
19+
let columns = raw_inp
20+
.trim()
21+
.bytes()
22+
.position(|c| c == b'\n')
23+
.context("can't get column count")?;
24+
25+
Array2::from_shape_vec(
26+
((raw_inp.trim().len() + 1) / (columns + 1), columns),
27+
raw_inp
28+
.bytes()
29+
.filter(|&x| x != b'\n')
30+
.map(|b| b == TRUE_CHAR)
31+
.collect(),
32+
)
33+
.context("can't make array")
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use anyhow::Result;
2+
use ndarray::Array2;
3+
4+
fn neighbours(grid: &Array2<bool>, y: usize, x: usize) -> usize {
5+
[
6+
(y.wrapping_sub(1), x.wrapping_sub(1)),
7+
(y.wrapping_sub(1), x),
8+
(y.wrapping_sub(1), x.wrapping_add(1)),
9+
(y, x.wrapping_sub(1)),
10+
(y, x.wrapping_add(1)),
11+
(y.wrapping_add(1), x.wrapping_sub(1)),
12+
(y.wrapping_add(1), x),
13+
(y.wrapping_add(1), x.wrapping_add(1)),
14+
]
15+
.into_iter()
16+
.filter(|&(y, x)| *grid.get((y, x)).unwrap_or(&false))
17+
.count()
18+
}
19+
20+
fn calculate_p1(grid: &Array2<bool>) -> usize {
21+
grid.indexed_iter()
22+
.filter(|&((y, x), itm)| *itm && neighbours(grid, y, x) < 4)
23+
.count()
24+
}
25+
26+
fn calculate_p2(mut grid: Array2<bool>) -> u32 {
27+
let mut p2 = 0;
28+
let mut changed = true;
29+
30+
while changed {
31+
changed = false;
32+
33+
for y in 0..grid.dim().0 {
34+
for x in 0..grid.dim().1 {
35+
if *grid.get((y, x)).expect("indexes are valid") && neighbours(&grid, y, x) < 4 {
36+
*grid.get_mut((y, x)).expect("indexes are valid") = false;
37+
p2 += 1;
38+
changed = true;
39+
}
40+
}
41+
}
42+
}
43+
44+
p2
45+
}
46+
47+
pub fn run_2025_04(inp: &str) -> Result<String> {
48+
let grid = crate::grid_util::make_bool_grid::<b'@'>(inp)?;
49+
let p1 = calculate_p1(&grid);
50+
let p2 = calculate_p2(grid);
51+
Ok(format!("{p1}\n{p2}"))
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
const EXAMPLE_DATA: &str = include_str!("../inputs/examples/2025_04");
59+
const REAL_DATA: &str = include_str!("../inputs/real/2025_04");
60+
61+
#[test]
62+
fn test_example_p1() {
63+
let grid = crate::grid_util::make_bool_grid::<b'@'>(EXAMPLE_DATA).unwrap();
64+
assert_eq!(calculate_p1(&grid), 13);
65+
}
66+
67+
#[test]
68+
fn test_example_p2() {
69+
let grid = crate::grid_util::make_bool_grid::<b'@'>(EXAMPLE_DATA).unwrap();
70+
assert_eq!(calculate_p2(grid), 43);
71+
}
72+
73+
#[test]
74+
fn test_real_p1() {
75+
let grid = crate::grid_util::make_bool_grid::<b'@'>(REAL_DATA).unwrap();
76+
assert_eq!(calculate_p1(&grid), 1349);
77+
}
78+
79+
#[test]
80+
fn test_real_p2() {
81+
let grid = crate::grid_util::make_bool_grid::<b'@'>(REAL_DATA).unwrap();
82+
assert_eq!(calculate_p2(grid), 8277);
83+
}
84+
}

‎aoc2025_wasm/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
pub mod grid_util;
12
mod impl_2025_01;
23
mod impl_2025_02;
34
mod impl_2025_03;
5+
mod impl_2025_04;
46

57
pub use wasm_bindgen_rayon::init_thread_pool;
68

@@ -13,6 +15,7 @@ pub fn run_day(day: u32, input: &str) -> String {
1315
1 => impl_2025_01::run_2025_01(input),
1416
2 => impl_2025_02::run_2025_02(input),
1517
3 => impl_2025_03::run_2025_03(input),
18+
4 => impl_2025_04::run_2025_04(input),
1619
_ => Err(anyhow!("No solution for that day yet")),
1720
};
1821

0 commit comments

Comments
 (0)