Skip to content

Commit 04550ee

Browse files
committed
[2024] Day 25 initial solution
WOO LAST DAY
1 parent 1a90776 commit 04550ee

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
2929
- [Day 22: Monkey Market](aoc_2024/src/day_22.rs)
3030
- [Day 23: LAN Party](aoc_2024/src/day_23.rs)
3131
- [Day 24: Crossed Wires](aoc_2024/src/day_24.rs)
32+
- [Day 25: Code Chronicle](aoc_2024/src/day_25.rs)
3233
<!-- MARKER -->
3334

3435
## [2023](https://adventofcode.com/2023) [![aoc_2023](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2023.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2023.yml)

aoc_2024/src/day_25.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use std::convert::identity;
2+
3+
use aoc_lib::matrix::Grid;
4+
use common::{solution, Answer};
5+
use nd_vec::vector;
6+
7+
solution!("Code Chronicle", 25);
8+
9+
fn part_a(input: &str) -> Answer {
10+
let mut keys = Vec::new();
11+
let mut locks = Vec::new();
12+
13+
for item in input.split("\n\n") {
14+
let (item, lock) = Key::parse(item);
15+
if lock {
16+
locks.push(item);
17+
} else {
18+
keys.push(item);
19+
}
20+
}
21+
22+
let mut out = 0;
23+
24+
for key in keys {
25+
for lock in locks.iter() {
26+
if key.fits(&lock) {
27+
out += 1;
28+
}
29+
}
30+
}
31+
32+
out.into()
33+
}
34+
35+
fn part_b(input: &str) -> Answer {
36+
Answer::Unimplemented
37+
}
38+
39+
#[derive(Debug)]
40+
struct Key {
41+
heights: [u8; 5],
42+
}
43+
44+
impl Key {
45+
fn parse(input: &str) -> (Self, bool) {
46+
let grid = Grid::parse(input, identity);
47+
48+
let mut heights = [0; 5];
49+
let mut is_lock = true;
50+
51+
for x in 0..5 {
52+
let mut height = 0;
53+
for y in 0..7 {
54+
if y == 0 && grid[vector!(x, y)] != '#' {
55+
is_lock = false;
56+
}
57+
58+
if grid[vector!(x, y)] == '#' {
59+
height += 1;
60+
}
61+
}
62+
63+
heights[x] = height - 1;
64+
}
65+
66+
(Self { heights }, is_lock)
67+
}
68+
69+
fn fits(&self, other: &Self) -> bool {
70+
for (a, b) in self.heights.iter().zip(other.heights.iter()) {
71+
if a + b > 5 {
72+
return false;
73+
}
74+
}
75+
76+
true
77+
}
78+
}
79+
80+
#[cfg(test)]
81+
mod test {
82+
use indoc::indoc;
83+
84+
const CASE: &str = indoc! {"
85+
#####
86+
.####
87+
.####
88+
.####
89+
.#.#.
90+
.#...
91+
.....
92+
93+
#####
94+
##.##
95+
.#.##
96+
...##
97+
...#.
98+
...#.
99+
.....
100+
101+
.....
102+
#....
103+
#....
104+
#...#
105+
#.#.#
106+
#.###
107+
#####
108+
109+
.....
110+
.....
111+
#.#..
112+
###..
113+
###.#
114+
###.#
115+
#####
116+
117+
.....
118+
.....
119+
.....
120+
#....
121+
#.#..
122+
#.#.#
123+
#####
124+
"};
125+
126+
#[test]
127+
fn part_a() {
128+
assert_eq!(super::part_a(CASE), 3.into());
129+
}
130+
131+
#[test]
132+
fn part_b() {
133+
assert_eq!(super::part_b(CASE), ().into());
134+
}
135+
}

aoc_2024/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod day_21;
2424
mod day_22;
2525
mod day_23;
2626
mod day_24;
27+
mod day_25;
2728
// [import_marker]
2829

2930
pub const SOLUTIONS: &[Solution] = &[
@@ -51,5 +52,6 @@ pub const SOLUTIONS: &[Solution] = &[
5152
day_22::SOLUTION,
5253
day_23::SOLUTION,
5354
day_24::SOLUTION,
55+
day_25::SOLUTION,
5456
// [list_marker]
5557
];

0 commit comments

Comments
 (0)