Skip to content

Commit 40a22cb

Browse files
committed
[2025] Day 6 draft solutions
1 parent 171c12a commit 40a22cb

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
1010
- [Day 03: Lobby](aoc_2025/src/day_03.rs)
1111
- [Day 04: Printing Department](aoc_2025/src/day_04.rs)
1212
- [Day 05: Cafeteria](aoc_2025/src/day_05.rs)
13+
- [Day 06: Trash Compactor](aoc_2025/src/day_06.rs)
1314
<!-- MARKER -->
1415

1516
## [2024](https://adventofcode.com/2024) [![aoc_2024](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml)

aoc_2025/src/day_06.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use std::{convert::identity, mem};
2+
3+
use aoc_lib::matrix::Grid;
4+
use common::{Answer, solution};
5+
use nd_vec::vector;
6+
7+
solution!("Trash Compactor", 6);
8+
9+
fn part_a(input: &str) -> Answer {
10+
let (nums, ops) = input.trim().rsplit_once('\n').unwrap();
11+
12+
let mut problems = Vec::new();
13+
for line in nums.lines() {
14+
let nums = line
15+
.split_whitespace()
16+
.map(|x| x.parse::<u64>().unwrap())
17+
.collect::<Vec<_>>();
18+
if problems.is_empty() {
19+
problems.resize(nums.len(), Vec::new());
20+
}
21+
22+
for (i, num) in nums.iter().enumerate() {
23+
problems[i].push(*num);
24+
}
25+
}
26+
27+
let ops = ops.split_whitespace().collect::<Vec<_>>();
28+
29+
let mut out = 0;
30+
for (nums, op) in problems.iter().zip(ops) {
31+
match op {
32+
"*" => out += nums.iter().product::<u64>(),
33+
"+" => out += nums.iter().sum::<u64>(),
34+
_ => panic!(),
35+
}
36+
}
37+
38+
out.into()
39+
}
40+
41+
fn part_b(input: &str) -> Answer {
42+
let grid = Grid::parse(input, identity);
43+
let size = grid.size();
44+
45+
let mut pos = vector!(size.x() - 1, 0);
46+
47+
let mut out = 0;
48+
let mut nums = Vec::new();
49+
'outer: loop {
50+
let mut num = 0;
51+
while pos.y() < size.y() {
52+
let chr = grid.get(pos).unwrap();
53+
54+
if matches!(chr, '+' | '*') {
55+
nums.push(mem::take(&mut num));
56+
dbg!(&nums);
57+
match chr {
58+
'+' => out += mem::take(&mut nums).iter().sum::<u64>(),
59+
'*' => out += mem::take(&mut nums).iter().product::<u64>(),
60+
_ => {}
61+
}
62+
63+
if pos.x() == 0 {
64+
break 'outer;
65+
}
66+
67+
pos = vector!(pos.x() - 2, 0);
68+
continue 'outer;
69+
}
70+
71+
if let Some(digit) = chr.to_digit(10) {
72+
num = num * 10 + digit as u64;
73+
}
74+
pos += vector!(0, 1);
75+
}
76+
nums.push(mem::take(&mut num));
77+
78+
pos = vector!(pos.x() - 1, 0);
79+
}
80+
81+
out.into()
82+
}
83+
84+
#[cfg(test)]
85+
mod test {
86+
const CASE: &str = "123 328 51 64 \n 45 64 387 23 \n 6 98 215 314\n* + * + ";
87+
88+
#[test]
89+
fn part_a() {
90+
assert_eq!(super::part_a(CASE), 4277556.into());
91+
}
92+
93+
#[test]
94+
fn part_b() {
95+
assert_eq!(super::part_b(CASE), 3263827.into());
96+
}
97+
}

aoc_2025/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod day_02;
55
mod day_03;
66
mod day_04;
77
mod day_05;
8+
mod day_06;
89
// [import_marker]
910

1011
pub const SOLUTIONS: &[Solution] = &[
@@ -13,5 +14,6 @@ pub const SOLUTIONS: &[Solution] = &[
1314
day_03::SOLUTION,
1415
day_04::SOLUTION,
1516
day_05::SOLUTION,
17+
day_06::SOLUTION,
1618
// [list_marker]
1719
];

src/commands/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn run(cmd: &RunArgs) -> Result<()> {
2929
}
3030
};
3131

32-
let input = fs::read_to_string(&*path)?.trim().replace('\r', "");
32+
let input = fs::read_to_string(&*path)?.replace('\r', "");
3333

3434
let start = Instant::now();
3535
let out = match cmd.part {

0 commit comments

Comments
 (0)