Skip to content

Commit 01ad458

Browse files
committed
[2024] Day 19 initial solution
1 parent bbc6863 commit 01ad458

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
2323
- [Day 16: Reindeer Maze](aoc_2024/src/day_16.rs)
2424
- [Day 17: Chronospatial Computer](aoc_2024/src/day_17.rs)
2525
- [Day 18: RAM Run](aoc_2024/src/day_18.rs)
26+
- [Day 19: Linen Layout](aoc_2024/src/day_19.rs)
2627
<!-- MARKER -->
2728

2829
## [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_19.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
use std::collections::HashMap;
2+
3+
use common::{solution, Answer};
4+
5+
solution!("Linen Layout", 19);
6+
7+
fn part_a(input: &str) -> Answer {
8+
let problem = Problem::parse(input);
9+
let mut sum = 0;
10+
11+
for i in 0..problem.needed.len() {
12+
if problem.possible(i) {
13+
sum += 1;
14+
}
15+
}
16+
17+
sum.into()
18+
}
19+
20+
fn part_b(input: &str) -> Answer {
21+
let problem = Problem::parse(input);
22+
let mut sum = 0;
23+
24+
for i in 0..problem.needed.len() {
25+
sum += problem.ways(i);
26+
}
27+
28+
sum.into()
29+
}
30+
31+
struct Problem {
32+
sources: Vec<String>,
33+
needed: Vec<String>,
34+
}
35+
36+
impl Problem {
37+
fn parse(input: &str) -> Self {
38+
let (sources, needed) = input.split_once("\n\n").unwrap();
39+
let sources = sources.split(", ").map(|x| x.to_owned()).collect();
40+
let needed = needed.lines().map(|x| x.to_owned()).collect();
41+
42+
Self { sources, needed }
43+
}
44+
45+
fn possible(&self, design: usize) -> bool {
46+
fn _inner<'a>(
47+
memo: &mut HashMap<&'a str, bool>,
48+
expected: &'a str,
49+
sources: &[String],
50+
) -> bool {
51+
if let Some(&cache) = memo.get(expected) {
52+
return cache;
53+
}
54+
55+
if expected.len() == 0 {
56+
memo.insert(expected, true);
57+
return true;
58+
}
59+
60+
for source in sources {
61+
if expected.len() >= source.len()
62+
&& expected.starts_with(source)
63+
&& _inner(memo, &expected[source.len()..], &sources)
64+
{
65+
memo.insert(expected, true);
66+
return true;
67+
}
68+
}
69+
70+
memo.insert(expected, false);
71+
false
72+
}
73+
74+
_inner(&mut HashMap::new(), &self.needed[design], &self.sources)
75+
}
76+
77+
fn ways(&self, design: usize) -> u64 {
78+
fn _inner<'a>(
79+
memo: &mut HashMap<&'a str, u64>,
80+
expected: &'a str,
81+
sources: &[String],
82+
) -> u64 {
83+
if let Some(&cache) = memo.get(expected) {
84+
return cache;
85+
}
86+
87+
if expected.len() == 0 {
88+
return 1;
89+
}
90+
91+
let mut ways = 0;
92+
for source in sources {
93+
if expected.len() >= source.len() && expected.starts_with(source) {
94+
ways += _inner(memo, &expected[source.len()..], &sources);
95+
}
96+
}
97+
98+
memo.insert(expected, ways);
99+
ways
100+
}
101+
102+
_inner(&mut HashMap::new(), &self.needed[design], &self.sources)
103+
}
104+
}
105+
106+
#[cfg(test)]
107+
mod test {
108+
use indoc::indoc;
109+
110+
const CASE: &str = indoc! {"
111+
r, wr, b, g, bwu, rb, gb, br
112+
113+
brwrr
114+
bggr
115+
gbbr
116+
rrbgbr
117+
ubwu
118+
bwurrg
119+
brgr
120+
bbrgwb
121+
"};
122+
123+
#[test]
124+
fn part_a() {
125+
assert_eq!(super::part_a(CASE), 6.into());
126+
}
127+
128+
#[test]
129+
fn part_b() {
130+
assert_eq!(super::part_b(CASE), 16.into());
131+
}
132+
}

aoc_2024/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod day_15;
1818
mod day_16;
1919
mod day_17;
2020
mod day_18;
21+
mod day_19;
2122
// [import_marker]
2223

2324
pub const SOLUTIONS: &[Solution] = &[
@@ -39,5 +40,6 @@ pub const SOLUTIONS: &[Solution] = &[
3940
day_16::SOLUTION,
4041
day_17::SOLUTION,
4142
day_18::SOLUTION,
43+
day_19::SOLUTION,
4244
// [list_marker]
4345
];

0 commit comments

Comments
 (0)