Skip to content

Commit 7ba8213

Browse files
committed
AoC 2024 Day 19 - rust
1 parent 0b0f5f4 commit 7ba8213

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | [](src/main/python/AoC2024_08.py) | [](src/main/python/AoC2024_09.py) | [](src/main/python/AoC2024_10.py) | [](src/main/python/AoC2024_11.py) | [](src/main/python/AoC2024_12.py) | [](src/main/python/AoC2024_13.py) | [](src/main/python/AoC2024_14.py) | [](src/main/python/AoC2024_15.py) | [](src/main/python/AoC2024_16.py) | [](src/main/python/AoC2024_17.py) | [](src/main/python/AoC2024_18.py) | [](src/main/python/AoC2024_19.py) | | | | | | |
1212
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | [](src/main/java/AoC2024_14.java) | [](src/main/java/AoC2024_15.java) | | | | | | | | | | |
13-
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | [](src/main/rust/AoC2024_10/src/main.rs) | [](src/main/rust/AoC2024_11/src/main.rs) | [](src/main/rust/AoC2024_12/src/main.rs) | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | [](src/main/rust/AoC2024_15/src/main.rs) | [](src/main/rust/AoC2024_16/src/main.rs) | [](src/main/rust/AoC2024_17/src/main.rs) | [](src/main/rust/AoC2024_18/src/main.rs) | | | | | | | |
13+
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | [](src/main/rust/AoC2024_10/src/main.rs) | [](src/main/rust/AoC2024_11/src/main.rs) | [](src/main/rust/AoC2024_12/src/main.rs) | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | [](src/main/rust/AoC2024_15/src/main.rs) | [](src/main/rust/AoC2024_16/src/main.rs) | [](src/main/rust/AoC2024_17/src/main.rs) | [](src/main/rust/AoC2024_18/src/main.rs) | [](src/main/rust/AoC2024_19/src/main.rs) | | | | | | |
1414
<!-- @END:ImplementationsTable:2024@ -->
1515

1616
## 2023
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "AoC2024_19"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../aoc" }
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#![allow(non_snake_case)]
2+
3+
use aoc::Puzzle;
4+
use std::collections::HashMap;
5+
6+
struct AoC2024_19 {}
7+
8+
impl AoC2024_19 {
9+
#[allow(clippy::only_used_in_recursion)]
10+
fn count(
11+
&self,
12+
cache: &mut HashMap<String, usize>,
13+
design: String,
14+
towels: &[String],
15+
) -> usize {
16+
if let Some(ans) = cache.get(&design) {
17+
return *ans;
18+
}
19+
if design.is_empty() {
20+
return 1;
21+
}
22+
let ans = towels
23+
.iter()
24+
.filter(|towel| design.starts_with(*towel))
25+
.map(|towel| {
26+
self.count(cache, String::from(&design[towel.len()..]), towels)
27+
})
28+
.sum();
29+
cache.insert(design, ans);
30+
ans
31+
}
32+
}
33+
34+
impl aoc::Puzzle for AoC2024_19 {
35+
type Input = (Vec<String>, Vec<String>);
36+
type Output1 = usize;
37+
type Output2 = usize;
38+
39+
aoc::puzzle_year_day!(2024, 19);
40+
41+
fn parse_input(&self, lines: Vec<String>) -> Self::Input {
42+
let towels = lines[0].split(", ").map(String::from).collect();
43+
let designs = lines[2..].to_vec();
44+
(towels, designs)
45+
}
46+
47+
fn part_1(&self, input: &Self::Input) -> Self::Output1 {
48+
let (towels, designs) = input;
49+
let mut cache: HashMap<String, usize> = HashMap::new();
50+
designs
51+
.iter()
52+
.filter(|design| {
53+
self.count(&mut cache, String::from(*design), towels) > 0
54+
})
55+
.count()
56+
}
57+
58+
fn part_2(&self, input: &Self::Input) -> Self::Output2 {
59+
let (towels, designs) = input;
60+
let mut cache: HashMap<String, usize> = HashMap::new();
61+
designs
62+
.iter()
63+
.map(|design| self.count(&mut cache, String::from(design), towels))
64+
.sum()
65+
}
66+
67+
fn samples(&self) {
68+
aoc::puzzle_samples! {
69+
self, part_1, TEST, 6,
70+
self, part_2, TEST, 16
71+
};
72+
}
73+
}
74+
75+
fn main() {
76+
AoC2024_19 {}.run(std::env::args());
77+
}
78+
79+
const TEST: &str = "\
80+
r, wr, b, g, bwu, rb, gb, br
81+
82+
brwrr
83+
bggr
84+
gbbr
85+
rrbgbr
86+
ubwu
87+
bwurrg
88+
brgr
89+
bbrgwb
90+
";
91+
92+
#[cfg(test)]
93+
mod tests {
94+
use super::*;
95+
96+
#[test]
97+
pub fn samples() {
98+
AoC2024_19 {}.samples();
99+
}
100+
}

src/main/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)