Skip to content

Commit 9d6abea

Browse files
committed
2025-06
1 parent 4ee7f10 commit 9d6abea

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

aoc2025_wasm/inputs

aoc2025_wasm/src/grid_util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use ndarray::Array2;
33

44
pub fn make_byte_grid(raw_inp: &str) -> Result<Array2<u8>> {
55
let columns = raw_inp
6-
.trim()
76
.bytes()
87
.position(|c| c == b'\n')
98
.context("can't get column count")?;
109

1110
Array2::from_shape_vec(
12-
((raw_inp.trim().len() + 1) / (columns + 1), columns),
11+
((raw_inp.len() + 1) / (columns + 1), columns),
1312
raw_inp.bytes().filter(|&x| x != b'\n').collect(),
1413
)
1514
.context("can't make array")

aoc2025_wasm/src/impl_2025_06.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use anyhow::{Context, Result};
2+
use ndarray::s;
3+
4+
use crate::grid_util::make_byte_grid;
5+
6+
fn calculate_p1(inp: &str) -> Result<u64> {
7+
let data = inp
8+
.lines()
9+
.map(|line| {
10+
line.trim()
11+
.split(" ")
12+
.filter(|x| !x.is_empty())
13+
.collect::<Vec<_>>()
14+
})
15+
.collect::<Vec<_>>();
16+
17+
let y_size = data.len();
18+
let x_size = data.first().context("not enough data")?.len();
19+
let mut ans = 0;
20+
21+
for x in 0..x_size {
22+
let op = *data
23+
.get(y_size - 1)
24+
.context("not enough data")?
25+
.get(x)
26+
.context("not enough data")?;
27+
28+
ans += (0..(y_size - 1))
29+
.filter_map(|y| data.get(y).and_then(|v| v.get(x)))
30+
.filter_map(|itm| itm.parse::<u64>().ok())
31+
.fold(if op == "+" { 0 } else { 1 }, |a, b| {
32+
if op == "+" { a + b } else { a * b }
33+
});
34+
}
35+
36+
Ok(ans)
37+
}
38+
39+
fn calculate_p2(inp: &str) -> Result<u64> {
40+
let grid = make_byte_grid(inp)?;
41+
42+
let mut op = b'+';
43+
let mut cumulative = 0;
44+
let mut ans = 0;
45+
46+
for x in 0..grid.dim().1 {
47+
if let Some(&o) = grid.get((grid.dim().0 - 1, x))
48+
&& o != b' '
49+
{
50+
op = o;
51+
ans += cumulative;
52+
if op == b'+' {
53+
cumulative = 0;
54+
} else {
55+
cumulative = 1;
56+
}
57+
}
58+
59+
let n = grid
60+
.slice(s![..-1, x])
61+
.iter()
62+
.filter(|elem| elem.is_ascii_digit())
63+
.map(|&elem| (elem - b'0') as u64)
64+
.fold(0, |a, b| a * 10 + b);
65+
66+
if n != 0 {
67+
if op == b'+' {
68+
cumulative += n;
69+
} else {
70+
cumulative *= n;
71+
}
72+
}
73+
}
74+
75+
Ok(ans + cumulative)
76+
}
77+
78+
pub fn run_2025_06(inp: &str) -> Result<String> {
79+
let p1 = calculate_p1(inp)?;
80+
let p2 = calculate_p2(inp)?;
81+
Ok(format!("{p1}\n{p2}"))
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
const EXAMPLE_DATA: &str = include_str!("../inputs/examples/2025_06");
89+
const REAL_DATA: &str = include_str!("../inputs/real/2025_06");
90+
91+
#[test]
92+
fn test_example_p1() {
93+
assert_eq!(calculate_p1(EXAMPLE_DATA).unwrap(), 4277556);
94+
}
95+
96+
#[test]
97+
fn test_example_p2() {
98+
assert_eq!(calculate_p2(EXAMPLE_DATA).unwrap(), 3263827);
99+
}
100+
101+
#[test]
102+
fn test_real_p1() {
103+
assert_eq!(calculate_p1(REAL_DATA).unwrap(), 5524274308182);
104+
}
105+
106+
#[test]
107+
fn test_real_p2() {
108+
assert_eq!(calculate_p2(REAL_DATA).unwrap(), 8843673199391);
109+
}
110+
}

aoc2025_wasm/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
pub mod grid_util;
1+
pub(crate) mod grid_util;
22
mod impl_2025_01;
33
mod impl_2025_02;
44
mod impl_2025_03;
55
mod impl_2025_04;
66
mod impl_2025_05;
7+
mod impl_2025_06;
78

89
pub use wasm_bindgen_rayon::init_thread_pool;
910

@@ -18,6 +19,7 @@ pub fn run_day(day: u32, input: &str) -> String {
1819
3 => impl_2025_03::run_2025_03(input),
1920
4 => impl_2025_04::run_2025_04(input),
2021
5 => impl_2025_05::run_2025_05(input),
22+
6 => impl_2025_06::run_2025_06(input),
2123
_ => Err(anyhow!("No solution for that day yet")),
2224
};
2325

0 commit comments

Comments
 (0)