feat: added day 19 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (19) (push) Successful in 3s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 12s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (19) (push) Successful in 3s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 12s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
parent
e46cd60df1
commit
2fad402c93
@ -11,7 +11,7 @@ jobs:
|
|||||||
name: Challenge for day
|
name: Challenge for day
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
|
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||||
runs-on: rust-bookworm
|
runs-on: rust-bookworm
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
|
6
day19/Cargo.toml
Normal file
6
day19/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day19"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
62
day19/src/main.rs
Normal file
62
day19/src/main.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::{env, fs};
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
fs::read_to_string(path).expect("Cannot read file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> (HashSet<&str>, Vec<&str>) {
|
||||||
|
let mut lines = input.lines().filter(|s| !s.is_empty());
|
||||||
|
(lines.next().unwrap().split(", ").collect(), lines.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_combinations(
|
||||||
|
pattern: &str,
|
||||||
|
rules: &HashSet<&str>,
|
||||||
|
combinations_counts: &mut HashMap<String, u64>,
|
||||||
|
) -> u64 {
|
||||||
|
if combinations_counts.contains_key(pattern) {
|
||||||
|
return *combinations_counts.get(pattern).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
for &rule in rules {
|
||||||
|
if pattern.starts_with(rule) {
|
||||||
|
if pattern == rule {
|
||||||
|
count += 1;
|
||||||
|
} else {
|
||||||
|
let sub_pattern_combinations =
|
||||||
|
get_combinations(&pattern[rule.len()..], rules, combinations_counts);
|
||||||
|
combinations_counts
|
||||||
|
.insert(pattern[rule.len()..].to_string(), sub_pattern_combinations);
|
||||||
|
count += sub_pattern_combinations;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
combinations_counts.insert(pattern.to_string(), count);
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(combinations: &Vec<u64>) -> usize {
|
||||||
|
combinations.iter().filter(|&&c| c > 0).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(combinations: &Vec<u64>) -> u64 {
|
||||||
|
combinations.iter().sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
let input = read_input(&arg);
|
||||||
|
let (rules, patterns) = parse_input(&input);
|
||||||
|
let mut valid_patterns = HashMap::new();
|
||||||
|
let combinations = patterns
|
||||||
|
.iter()
|
||||||
|
.map(|p| get_combinations(p, &rules, &mut valid_patterns))
|
||||||
|
.collect();
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", part_1(&combinations));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", part_2(&combinations));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user