diff --git a/rust/src/main.rs b/rust/src/main.rs index 28fe4d0..a6f65b6 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -669,6 +669,130 @@ mod day16 { } } +mod day17 { + use std::time::Duration; + + pub fn run(_print: bool) -> Duration { + Duration::new(0, 0) + } +} + +mod day18 { + use std::time::Duration; + + pub fn run(_print: bool) -> Duration { + Duration::new(0, 0) + } +} + +mod day19 { + use std::collections::BTreeMap as HashMap; + use std::time::Duration; + + #[derive(Debug)] + enum Rhs { + Terminal(char), + Variables(Vec>), + } + + type Rules = HashMap; + + fn parse(input: &str) -> Option<(Rules, Vec<&str>)> { + let mut rules = HashMap::new(); + let mut messages = Vec::new(); + let mut parsing_rules = true; + for line in input.lines() { + let trim = line.trim(); + if trim.is_empty() { + parsing_rules = false; + continue; + } + + if parsing_rules { + let mut split = trim.split(": "); + let lhs: usize = split.next()?.parse().ok()?; + + let terms = split.next()?; + let rhs = if terms.starts_with('"') { + Rhs::Terminal(terms.chars().nth(1)?) + } else { + Rhs::Variables( + terms + .split(" | ") + .map(|t| t.split(" ").flat_map(str::parse).collect()) + .collect(), + ) + }; + + rules.insert(lhs, rhs); + } else { + messages.push(trim); + } + } + + Some((rules, messages)) + } + + pub fn run(_print: bool) -> Duration { + let input = std::fs::read_to_string("../inputs/19").unwrap(); + + println!("from typing import Tuple"); + println!(); + + let (rules, messages) = parse(&input).unwrap(); + + for (i, rule) in rules.iter() { + println!("def rule_{}(s: str) -> Tuple[bool, str]:", i); + match rule { + Rhs::Terminal(terminal) => { + println!(" if s[0] == '{}':", terminal); + println!(" return True, s[1:]"); + println!(" return False, s"); + } + + Rhs::Variables(variables) => { + for term in variables { + println!(" r = s"); + for (j, variable) in term.iter().enumerate() { + println!(" valid_{}, r = rule_{}(r)", j, variable); + } + println!(" if all(("); + for j in 0..term.len() { + println!(" valid_{}, ", j); + } + println!(" )):"); + println!(" return True, r"); + println!(); + } + println!(" return False, s"); + } + } + + println!(); + println!(); + } + println!("def validate(s):"); + for i in 0..rules.len() { + println!(" if rule_{}(s) == (True, ''):", i); + println!(" return True"); + } + println!(" return False"); + + println!(); + println!("if __name__ == '__main__':"); + println!(" part1 = 0"); + for message in messages { + println!(" if (result := validate('{}')):", message); + println!(" part1 += 1"); + println!(" print('{}:', result)", message); + println!(); + } + println!(" print('Part 1:', part1)"); + + Duration::new(0, 0) + } +} + fn main() { let days = [ day01::run, @@ -687,6 +811,9 @@ fn main() { day14::run, day15::run, day16::run, + day17::run, + day18::run, + day19::run, ]; if let Some(day) = std::env::args().nth(1) {