Skip to content

Commit 38f087d

Browse files
committed
chore: y2025::day_06
1 parent 5881f06 commit 38f087d

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

aoclp_solutions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub mod y2025;
1111
build_solvers! {
1212
{ 2017, [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] },
1313
{ 2024, [01, 02, 03, 04, 05, 06, 07, 08] },
14-
{ 2025, [01, 02, 03, 04, 05] }
14+
{ 2025, [01, 02, 03, 04, 05, 06] }
1515
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use std::str::FromStr;
2+
3+
use aoclp::anyhow::anyhow;
4+
use aoclp::functional::ByRefPredHelper;
5+
use aoclp::solvers_impl::input::safe_get_input;
6+
use itertools::Itertools;
7+
8+
pub fn part_1() -> usize {
9+
problems()
10+
.into_iter()
11+
.map(Problem::answer.without_ref())
12+
.sum()
13+
}
14+
15+
pub fn part_2() -> usize {
16+
cephalopod_problems()
17+
.into_iter()
18+
.map(Problem::answer.without_ref())
19+
.sum()
20+
}
21+
22+
#[derive(Debug, Copy, Clone)]
23+
enum Operator {
24+
Plus,
25+
Times,
26+
}
27+
28+
impl Operator {
29+
fn initial(self) -> usize {
30+
match self {
31+
Operator::Plus => 0,
32+
Operator::Times => 1,
33+
}
34+
}
35+
36+
fn apply(self, a: usize, b: usize) -> usize {
37+
match self {
38+
Operator::Plus => a + b,
39+
Operator::Times => a * b,
40+
}
41+
}
42+
}
43+
44+
impl FromStr for Operator {
45+
type Err = aoclp::Error;
46+
47+
fn from_str(s: &str) -> Result<Self, Self::Err> {
48+
match s {
49+
"+" => Ok(Operator::Plus),
50+
"*" => Ok(Operator::Times),
51+
op => Err(anyhow!("Unknown operator: {op}")),
52+
}
53+
}
54+
}
55+
56+
#[derive(Debug)]
57+
struct Problem {
58+
operands: Vec<usize>,
59+
operator: Operator,
60+
}
61+
62+
impl Problem {
63+
fn answer(&self) -> usize {
64+
self.operands
65+
.iter()
66+
.fold(self.operator.initial(), |acc, i| self.operator.apply(acc, *i))
67+
}
68+
}
69+
70+
fn problems() -> Vec<Problem> {
71+
let input = input();
72+
let operands_list = input
73+
.lines()
74+
.dropping_back(1)
75+
.map(|line| {
76+
line.split_ascii_whitespace()
77+
.map(|n| n.parse::<usize>().unwrap())
78+
.collect_vec()
79+
})
80+
.collect_vec();
81+
82+
parse_operators(&input)
83+
.into_iter()
84+
.enumerate()
85+
.map(|(i, operator)| {
86+
let operands = operands_list
87+
.iter()
88+
.map(|operands| operands[i])
89+
.collect_vec();
90+
Problem { operands, operator }
91+
})
92+
.collect_vec()
93+
}
94+
95+
fn cephalopod_problems() -> Vec<Problem> {
96+
let input = input();
97+
let operators = parse_operators(&input);
98+
let mut operands = Vec::new();
99+
100+
let operand_lines = input.lines().dropping_back(1).collect_vec();
101+
let max_len = operand_lines.iter().map(|line| line.len()).max().unwrap();
102+
let mut add_problem_operands = |first_col, col| {
103+
let mut problem_operands = Vec::new();
104+
for problem_col in first_col..col {
105+
let n: usize = operand_lines
106+
.iter()
107+
.map(|line| line.chars().nth(problem_col).unwrap())
108+
.join("")
109+
.trim_ascii()
110+
.parse()
111+
.unwrap();
112+
problem_operands.push(n);
113+
}
114+
operands.push(problem_operands);
115+
};
116+
117+
let mut first_col = 0;
118+
for col in 0..max_len {
119+
if operand_lines
120+
.iter()
121+
.map(|line| line.chars().nth(col).unwrap())
122+
.all(|c| c == ' ')
123+
{
124+
add_problem_operands(first_col, col);
125+
first_col = col + 1;
126+
}
127+
}
128+
if first_col != max_len {
129+
add_problem_operands(first_col, max_len);
130+
}
131+
132+
operands
133+
.into_iter()
134+
.zip(operators)
135+
.map(|(operands, operator)| Problem { operands, operator })
136+
.collect_vec()
137+
}
138+
139+
fn parse_operators(input: &str) -> Vec<Operator> {
140+
input
141+
.lines()
142+
.last()
143+
.unwrap()
144+
.split_ascii_whitespace()
145+
.map(|op| op.parse::<Operator>().unwrap())
146+
.collect_vec()
147+
}
148+
149+
fn input() -> String {
150+
safe_get_input(2025, 6)
151+
}

aoclp_solutions/src/y2025/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pub mod day_02;
33
pub mod day_03;
44
pub mod day_04;
55
pub mod day_05;
6+
pub mod day_06;

0 commit comments

Comments
 (0)