rust(day19): Part 2

This commit is contained in:
Sijmen 2020-12-20 00:29:01 +01:00
parent a9cee17a28
commit 788651476c
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -687,7 +687,9 @@ mod day18 {
mod day19 { mod day19 {
use std::collections::BTreeMap as HashMap; use std::collections::BTreeMap as HashMap;
use std::time::Duration; use std::fs::File;
use std::io::{BufWriter, Write};
use std::time::{Duration, Instant};
#[derive(Debug)] #[derive(Debug)]
enum Rhs { enum Rhs {
@ -733,63 +735,76 @@ mod day19 {
Some((rules, messages)) Some((rules, messages))
} }
pub fn run(_print: bool) -> Duration { fn generate(rules: &Rules, messages: &Vec<&str>, filename: &str) -> std::io::Result<()> {
let input = std::fs::read_to_string("../inputs/19").unwrap(); let write_file = File::create(filename)?;
let mut w = BufWriter::new(&write_file);
println!("from typing import Tuple"); writeln!(&mut w, "from typing import Tuple")?;
println!(); writeln!(&mut w)?;
let (rules, messages) = parse(&input).unwrap();
for (i, rule) in rules.iter() { for (i, rule) in rules.iter() {
println!("def rule_{}(s: str) -> Tuple[bool, str]:", i); writeln!(&mut w, "def rule_{}(s):", i)?;
writeln!(&mut w, " if s != '':")?;
match rule { match rule {
Rhs::Terminal(terminal) => { Rhs::Terminal(terminal) => {
println!(" if s[0] == '{}':", terminal); writeln!(&mut w, " if s[0] == '{}':", terminal)?;
println!(" return True, s[1:]"); writeln!(&mut w, " yield s[1:]")?;
println!(" return False, s");
} }
Rhs::Variables(variables) => { Rhs::Variables(variables) => {
for term in variables { for term in variables {
println!(" r = s"); for (j, rule) in term.iter().enumerate() {
for (j, variable) in term.iter().enumerate() { if j == 0 {
println!(" valid_{}, r = rule_{}(r)", j, variable); writeln!(&mut w, " v{} = rule_{}(s)", j, rule)?;
} else {
writeln!(
&mut w,
" v{} = (out for inp in v{} for out in rule_{}(inp))",
j,
j - 1,
rule,
)?;
}
} }
println!(" if all(("); writeln!(&mut w, " yield from v{}", term.len() - 1)?;
for j in 0..term.len() { writeln!(&mut w)?;
println!(" valid_{}, ", j);
}
println!(" )):");
println!(" return True, r");
println!();
} }
println!(" return False, s");
} }
} }
println!(); writeln!(&mut w)?;
println!();
} }
println!("def validate(s):"); writeln!(&mut w, "def validate(s):")?;
for i in 0..rules.len() { writeln!(&mut w, " return any(x == '' for x in rule_0(s))")?;
println!(" if rule_{}(s) == (True, ''):", i);
println!(" return True");
}
println!(" return False");
println!(); writeln!(&mut w)?;
println!("if __name__ == '__main__':"); writeln!(&mut w, "if __name__ == '__main__':")?;
println!(" part1 = 0"); writeln!(&mut w, " result = 0")?;
for message in messages { for message in messages {
println!(" if (result := validate('{}')):", message); writeln!(&mut w, " success = validate('{}')", message)?;
println!(" part1 += 1"); // writeln!(&mut w, " print('{}:', success)", message)?;
println!(" print('{}:', result)", message); writeln!(&mut w, " if success:")?;
println!(); writeln!(&mut w, " result += 1")?;
writeln!(&mut w)?;
} }
println!(" print('Part 1:', part1)"); writeln!(&mut w, " print('Result:', result)")?;
Duration::new(0, 0) Ok(())
}
pub fn run(_print: bool) -> Duration {
let input = std::fs::read_to_string("../inputs/19").unwrap();
let instant = Instant::now();
let (mut rules, messages) = parse(&input).unwrap();
generate(&rules, &messages, "/tmp/day19-1.py").unwrap();
rules.insert(8, Rhs::Variables(vec![vec![42], vec![42, 8]]));
rules.insert(11, Rhs::Variables(vec![vec![42, 31], vec![42, 11, 31]]));
generate(&rules, &messages, "/tmp/day19-2.py").unwrap();
instant.elapsed()
} }
} }