Skip to content

Commit

Permalink
Year 2024 Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Dec 19, 2024
1 parent 9f684be commit 9d97614
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 390 |
| 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [Source](src/year2024/day17.rs) | 2 |
| 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [Source](src/year2024/day18.rs) | 42 |
| 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [Source](src/year2024/day19.rs) | 425 |

## 2023

Expand Down
2 changes: 1 addition & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ benchmark!(year2023

benchmark!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17, day18
day14, day15, day16, day17, day18, day19
);
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ library!(year2023 "Restore global snow production."

library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17, day18
day14, day15, day16, day17, day18, day19
);
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ run!(year2023

run!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17, day18
day14, day15, day16, day17, day18, day19
);
80 changes: 80 additions & 0 deletions src/year2024/day19.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! # Linen Layout
use crate::util::hash::*;

pub fn parse(input: &str) -> Vec<u64> {
let (prefix, suffix) = input.split_once("\n\n").unwrap();

let mut trie = Vec::with_capacity(1_000);
trie.push(Node::new());

for towel in prefix.split(", ") {
let mut i = 0;

for j in towel.bytes().map(to_index) {
if trie[i].next[j] == 0 {
trie[i].next[j] = trie.len();
i = trie.len();
trie.push(Node::new());
} else {
i = trie[i].next[j];
}
}

trie[i].towel = true;
}

let seen = &mut FastMap::with_capacity(20_000);
suffix.lines().map(|design| helper(&trie, seen, design.as_bytes())).collect()
}

pub fn part1(input: &[u64]) -> usize {
input.iter().filter(|&&n| n > 0).count()
}

pub fn part2(input: &[u64]) -> u64 {
input.iter().sum()
}

fn helper<'a>(trie: &[Node], seen: &mut FastMap<&'a [u8], u64>, design: &'a [u8]) -> u64 {
if design.is_empty() {
return 1;
}
if let Some(&previous) = seen.get(design) {
return previous;
}

let mut i = 0;
let mut ways = 0;

for depth in 0..design.len() {
let j = to_index(design[depth]);
i = trie[i].next[j];

if i == 0 {
break;
}

if trie[i].towel {
ways += helper(trie, seen, &design[depth + 1..]);
}
}

seen.insert(design, ways);
ways
}

#[inline]
fn to_index(b: u8) -> usize {
(b - b'a') as usize
}

struct Node {
towel: bool,
next: [usize; 26],
}

impl Node {
fn new() -> Self {
Node { towel: false, next: [0; 26] }
}
}
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ test!(year2023

test!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17, day18
day14, day15, day16, day17, day18, day19
);
25 changes: 25 additions & 0 deletions tests/year2024/day19.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use aoc::year2024::day19::*;

const EXAMPLE: &str = "\
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb";

#[test]
fn part1_test() {
let input = parse(EXAMPLE);
assert_eq!(part1(&input), 6);
}

#[test]
fn part2_test() {
let input = parse(EXAMPLE);
assert_eq!(part2(&input), 16);
}

0 comments on commit 9d97614

Please sign in to comment.