Skip to content

Commit

Permalink
AoC 2024 Day 19 - rust
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 20, 2024
1 parent 0b0f5f4 commit 7ba8213
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 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) | | | | | | |
| 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) | | | | | | | | | | |
| 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) | | | | | | | |
| 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) | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

## 2023
Expand Down
7 changes: 7 additions & 0 deletions src/main/rust/AoC2024_19/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "AoC2024_19"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
100 changes: 100 additions & 0 deletions src/main/rust/AoC2024_19/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#![allow(non_snake_case)]

use aoc::Puzzle;
use std::collections::HashMap;

struct AoC2024_19 {}

impl AoC2024_19 {
#[allow(clippy::only_used_in_recursion)]
fn count(
&self,
cache: &mut HashMap<String, usize>,
design: String,
towels: &[String],
) -> usize {
if let Some(ans) = cache.get(&design) {
return *ans;
}
if design.is_empty() {
return 1;
}
let ans = towels
.iter()
.filter(|towel| design.starts_with(*towel))
.map(|towel| {
self.count(cache, String::from(&design[towel.len()..]), towels)
})
.sum();
cache.insert(design, ans);
ans
}
}

impl aoc::Puzzle for AoC2024_19 {
type Input = (Vec<String>, Vec<String>);
type Output1 = usize;
type Output2 = usize;

aoc::puzzle_year_day!(2024, 19);

fn parse_input(&self, lines: Vec<String>) -> Self::Input {
let towels = lines[0].split(", ").map(String::from).collect();
let designs = lines[2..].to_vec();
(towels, designs)
}

fn part_1(&self, input: &Self::Input) -> Self::Output1 {
let (towels, designs) = input;
let mut cache: HashMap<String, usize> = HashMap::new();
designs
.iter()
.filter(|design| {
self.count(&mut cache, String::from(*design), towels) > 0
})
.count()
}

fn part_2(&self, input: &Self::Input) -> Self::Output2 {
let (towels, designs) = input;
let mut cache: HashMap<String, usize> = HashMap::new();
designs
.iter()
.map(|design| self.count(&mut cache, String::from(design), towels))
.sum()
}

fn samples(&self) {
aoc::puzzle_samples! {
self, part_1, TEST, 6,
self, part_2, TEST, 16
};
}
}

fn main() {
AoC2024_19 {}.run(std::env::args());
}

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

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn samples() {
AoC2024_19 {}.samples();
}
}
7 changes: 7 additions & 0 deletions src/main/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ba8213

Please sign in to comment.