Skip to content

Commit 2f63d49

Browse files
committed
solve: day6
1 parent e4483cc commit 2f63d49

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils", "day1", "day2", "day3", "day4", "day5",
5+
"aoc_lib/utils", "day1", "day2", "day3", "day4", "day5", "day6",
66
]
77

88
[workspace.dependencies]

aoc_lib

day6/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day6"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day6/src/main.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use aoc_derive::aoc_main;
2+
use itertools::Itertools;
3+
use utils::*;
4+
5+
#[aoc_main]
6+
fn solve(input: Input) -> impl Into<Solution> {
7+
(
8+
input
9+
.str_grid()
10+
.cols()
11+
.map(|col| {
12+
let mut col = col.rev();
13+
let (_, op) = col.next().unwrap();
14+
col.map(|(_, n)| n.parse_usize())
15+
.reduce(|acc, n| if *op == "+" { acc + n } else { acc * n })
16+
.unwrap()
17+
})
18+
.sum_usize(),
19+
input
20+
.char_grid()
21+
.cols()
22+
.map(|col| col.values().copied().collect_vec())
23+
.chunk_by(|col| col.iter().all(|&c| c == ' '))
24+
.into_iter()
25+
.filter_map(|(is_whitespace, cols)| (!is_whitespace).then_some(cols.collect_vec()))
26+
.map(|cols| {
27+
let op = *cols.first().unwrap().last().unwrap();
28+
cols.into_iter()
29+
.map(|col| col.iter().copied().fold_chars_to_number() as usize)
30+
.reduce(|acc, n| if op == '+' { acc + n } else { acc * n })
31+
.unwrap()
32+
})
33+
.sum_usize(),
34+
)
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
#[test]
41+
fn test_examples() {
42+
pretty_assertions::assert_eq!(
43+
solve(Input::from_str_no_trim(
44+
r#"123 328 51 64
45+
45 64 387 23
46+
6 98 215 314
47+
* + * + "#,
48+
))
49+
.into(),
50+
(4277556, 3263827).into(),
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)