Add day19
This commit is contained in:
parent
c72a5f9c6c
commit
cee0f7c935
119
day19/Cargo.lock
generated
Normal file
119
day19/Cargo.lock
generated
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ahash"
|
||||||
|
version = "0.8.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"getrandom",
|
||||||
|
"once_cell",
|
||||||
|
"version_check",
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day19"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.169"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.20.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.92"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.37"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.90"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
7
day19/Cargo.toml
Normal file
7
day19/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "day19"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
ahash = "0.8.11"
|
||||||
99
day19/src/main.rs
Normal file
99
day19/src/main.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||||
|
enum Color {
|
||||||
|
White,
|
||||||
|
Blue,
|
||||||
|
Black,
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
}
|
||||||
|
use ahash::AHashMap;
|
||||||
|
use Color::*;
|
||||||
|
|
||||||
|
impl From<char> for Color {
|
||||||
|
fn from(value: char) -> Self {
|
||||||
|
match value {
|
||||||
|
'w' => White,
|
||||||
|
'u' => Blue,
|
||||||
|
'b' => Black,
|
||||||
|
'r' => Red,
|
||||||
|
'g' => Green,
|
||||||
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||||
|
struct ParsedInput {
|
||||||
|
available_patterns: Vec<Vec<Color>>,
|
||||||
|
designs: Vec<Vec<Color>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &str) -> ParsedInput {
|
||||||
|
let (patterns, designs) = input.split_once("\n\n").unwrap();
|
||||||
|
ParsedInput {
|
||||||
|
available_patterns: patterns
|
||||||
|
.trim_end()
|
||||||
|
.split(", ")
|
||||||
|
.map(|p| p.chars().map(|c| c.into()).collect())
|
||||||
|
.collect(),
|
||||||
|
designs: designs
|
||||||
|
.lines()
|
||||||
|
.map(|d| d.chars().map(|c| c.into()).collect())
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_possible(
|
||||||
|
design: &[Color],
|
||||||
|
available_patterns: &[Vec<Color>],
|
||||||
|
cache: &mut AHashMap<Vec<Color>, usize>,
|
||||||
|
) -> usize {
|
||||||
|
if design.is_empty() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if let Some(res) = cache.get(design) {
|
||||||
|
return *res;
|
||||||
|
}
|
||||||
|
let mut count = 0;
|
||||||
|
for pattern in available_patterns {
|
||||||
|
if pattern.len() > design.len() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let mut matches = true;
|
||||||
|
for i in 0..pattern.len() {
|
||||||
|
if design[i] != pattern[i] {
|
||||||
|
matches = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if matches {
|
||||||
|
count += count_possible(&design[pattern.len()..], available_patterns, cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cache.insert(design.to_vec(), count);
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(parsed_input: &ParsedInput) -> usize {
|
||||||
|
let mut cache = AHashMap::new();
|
||||||
|
parsed_input
|
||||||
|
.designs
|
||||||
|
.iter()
|
||||||
|
.filter(|d| count_possible(d, &parsed_input.available_patterns, &mut cache) > 0)
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(parsed_input: &ParsedInput) -> usize {
|
||||||
|
let mut cache = AHashMap::new();
|
||||||
|
parsed_input
|
||||||
|
.designs
|
||||||
|
.iter()
|
||||||
|
.map(|d| count_possible(d, &parsed_input.available_patterns, &mut cache))
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = parse(include_str!("../input.txt"));
|
||||||
|
println!("{}", part1(&input));
|
||||||
|
println!("{}", part2(&input));
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user