Skip to content

Commit

Permalink
[2024] Day 25 initial solution
Browse files Browse the repository at this point in the history
WOO LAST DAY
  • Loading branch information
connorslade committed Dec 25, 2024
1 parent 1a90776 commit 04550ee
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 22: Monkey Market](aoc_2024/src/day_22.rs)
- [Day 23: LAN Party](aoc_2024/src/day_23.rs)
- [Day 24: Crossed Wires](aoc_2024/src/day_24.rs)
- [Day 25: Code Chronicle](aoc_2024/src/day_25.rs)
<!-- MARKER -->

## [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)
Expand Down
135 changes: 135 additions & 0 deletions aoc_2024/src/day_25.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use std::convert::identity;

use aoc_lib::matrix::Grid;
use common::{solution, Answer};
use nd_vec::vector;

solution!("Code Chronicle", 25);

fn part_a(input: &str) -> Answer {
let mut keys = Vec::new();
let mut locks = Vec::new();

for item in input.split("\n\n") {
let (item, lock) = Key::parse(item);
if lock {
locks.push(item);
} else {
keys.push(item);
}
}

let mut out = 0;

for key in keys {
for lock in locks.iter() {
if key.fits(&lock) {
out += 1;
}
}
}

out.into()
}

fn part_b(input: &str) -> Answer {
Answer::Unimplemented
}

#[derive(Debug)]
struct Key {
heights: [u8; 5],
}

impl Key {
fn parse(input: &str) -> (Self, bool) {
let grid = Grid::parse(input, identity);

let mut heights = [0; 5];
let mut is_lock = true;

for x in 0..5 {
let mut height = 0;
for y in 0..7 {
if y == 0 && grid[vector!(x, y)] != '#' {
is_lock = false;
}

if grid[vector!(x, y)] == '#' {
height += 1;
}
}

heights[x] = height - 1;
}

(Self { heights }, is_lock)
}

fn fits(&self, other: &Self) -> bool {
for (a, b) in self.heights.iter().zip(other.heights.iter()) {
if a + b > 5 {
return false;
}
}

true
}
}

#[cfg(test)]
mod test {
use indoc::indoc;

const CASE: &str = indoc! {"
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####
"};

#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 3.into());
}

#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), ().into());
}
}
2 changes: 2 additions & 0 deletions aoc_2024/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod day_21;
mod day_22;
mod day_23;
mod day_24;
mod day_25;
// [import_marker]

pub const SOLUTIONS: &[Solution] = &[
Expand Down Expand Up @@ -51,5 +52,6 @@ pub const SOLUTIONS: &[Solution] = &[
day_22::SOLUTION,
day_23::SOLUTION,
day_24::SOLUTION,
day_25::SOLUTION,
// [list_marker]
];

0 comments on commit 04550ee

Please sign in to comment.