Skip to content

Commit 33b6d73

Browse files
committed
2025 Day 6 (super dirty and hacky)
1 parent 92fccd9 commit 33b6d73

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/bin/2025_06/main.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use aoc::{common, grid::Grid, io};
2+
3+
fn solve2(input: &str) -> usize {
4+
let mut grid = Grid::from_str_no_trim(input, |c| c);
5+
//grid.print();
6+
let max_l = grid.values.iter().map(|v| v.len()).max().unwrap_or(0);
7+
//dbg!(max_l);
8+
for val in grid.values.iter_mut() {
9+
val.resize(max_l, ' ');
10+
}
11+
grid.cols = max_l;
12+
//grid.print();
13+
let grid2 = grid.rotate_clockwise();
14+
//grid2.print();
15+
16+
let mut ans = 0;
17+
let mut op_mul = false;
18+
let mut ret = 0;
19+
for val in grid2.values.iter() {
20+
let str = val.iter().collect::<String>();
21+
//println!("{}\n", str);
22+
if str.trim().is_empty() {
23+
//dbg!(ret);
24+
ans += ret;
25+
ret = 0;
26+
op_mul = false;
27+
continue;
28+
}
29+
let x = if str.contains("*") {
30+
op_mul = true;
31+
ret = 1;
32+
str[1..]
33+
.trim()
34+
.chars()
35+
.rev()
36+
.collect::<String>()
37+
.parse::<usize>()
38+
.unwrap()
39+
} else if str.contains("+") {
40+
op_mul = false;
41+
ret = 0;
42+
str[1..]
43+
.trim()
44+
.chars()
45+
.rev()
46+
.collect::<String>()
47+
.parse::<usize>()
48+
.unwrap()
49+
} else {
50+
str.trim()
51+
.chars()
52+
.rev()
53+
.collect::<String>()
54+
.parse::<usize>()
55+
.unwrap()
56+
};
57+
58+
//dbg!(x);
59+
if op_mul {
60+
ret *= x;
61+
} else {
62+
ret += x;
63+
}
64+
}
65+
ans += ret;
66+
ans
67+
}
68+
69+
fn solve<const PART: usize>(input: &str) -> usize {
70+
let mut ans = 0;
71+
let mut nums_tot: Vec<Vec<usize>> = Vec::new();
72+
for line in input.lines() {
73+
if line.is_empty() {
74+
continue;
75+
}
76+
//dbg!(line);
77+
if line.contains("*") || line.contains("+") {
78+
let ops = io::tokenize(line, " ");
79+
for (j, op) in ops.iter().enumerate() {
80+
match *op {
81+
"+" => {
82+
let mut ret = 0;
83+
for i in 0..nums_tot.len() {
84+
ret += nums_tot[i][j];
85+
}
86+
ans += ret;
87+
}
88+
"*" => {
89+
let mut ret = 1;
90+
for i in 0..nums_tot.len() {
91+
ret *= nums_tot[i][j];
92+
}
93+
ans += ret;
94+
}
95+
_ => {}
96+
}
97+
}
98+
} else {
99+
let nums = io::tokenize_nums(line, " ");
100+
nums_tot.push(nums);
101+
}
102+
}
103+
104+
ans
105+
}
106+
107+
fn main() {
108+
if let Some(input) = common::get_input() {
109+
common::timed(&input, solve::<1>, true);
110+
common::timed(&input, solve2, false);
111+
}
112+
}
113+
114+
#[cfg(test)]
115+
mod tests {
116+
use super::*;
117+
118+
#[test]
119+
fn test_samples() {
120+
let sample_input = "123 328 51 64\n 45 64 387 23\n 6 98 215 314\n* + * + ";
121+
assert_eq!(solve::<1>(sample_input), 4277556);
122+
assert_eq!(solve2(sample_input), 3263827);
123+
}
124+
}

src/grid.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ impl<T: std::fmt::Debug + Clone + Default + PartialEq + Hash> Grid<T> {
8989
grid
9090
}
9191

92+
#[must_use]
93+
pub fn from_str_no_trim(input: &str, f: fn(char) -> T) -> Self {
94+
let lines = input
95+
.split('\n')
96+
.filter(|l| !l.is_empty())
97+
.collect::<Vec<_>>();
98+
let mut grid = Grid::<T>::new(lines.len(), lines[0].len(), T::default());
99+
for (i, line) in lines.iter().enumerate() {
100+
let row = line.chars().map(f).collect::<Vec<_>>();
101+
grid.set_row(i, row);
102+
}
103+
grid
104+
}
105+
92106
#[must_use]
93107
pub fn rotate_clockwise(&self) -> Self {
94108
let mut ret = Grid::new(self.cols, self.rows, T::default());

0 commit comments

Comments
 (0)