diff --git a/2018/benches/days.rs b/2018/benches/days.rs index e594a74..a361cbb 100644 --- a/2018/benches/days.rs +++ b/2018/benches/days.rs @@ -72,8 +72,10 @@ day_bench!(day17, 17); day_bench!(day18, 18); day_bench!(day19, 19); day_bench!(day20, 20); +day_bench!(day21, 21); benchmark_main!(day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13, day14, day15, - day16, day17, day18, day19, day20); + day16, day17, day18, day19, day20, + day21); diff --git a/2018/loc.svg b/2018/loc.svg index 067d45f..ce64a3e 100644 --- a/2018/loc.svg +++ b/2018/loc.svg @@ -32,10 +32,10 @@ z +" id="mec9193305a" style="stroke:#000000;stroke-width:0.8;"/> - + @@ -71,7 +71,7 @@ z - + @@ -110,7 +110,7 @@ z - + @@ -139,7 +139,7 @@ z - + @@ -153,7 +153,7 @@ z - + @@ -193,7 +193,7 @@ z - + @@ -286,10 +286,10 @@ z +" id="mcf4dca2140" style="stroke:#000000;stroke-width:0.8;"/> - + @@ -302,7 +302,7 @@ L -3.5 0 - + @@ -316,7 +316,7 @@ L -3.5 0 - + @@ -331,7 +331,7 @@ L -3.5 0 - + @@ -346,7 +346,7 @@ L -3.5 0 - + @@ -361,7 +361,7 @@ L -3.5 0 - + @@ -577,7 +577,7 @@ z - - - + diff --git a/2018/other/elf2c.py b/2018/other/elf2c.py index d1bcb50..ac02428 100755 --- a/2018/other/elf2c.py +++ b/2018/other/elf2c.py @@ -19,7 +19,7 @@ def preamble(): int c = 0; int d = 0; int e = 0; - int f = 0;''') + int f = 0;\n''') def footer(): diff --git a/2018/src/day21.rs b/2018/src/day21.rs index 401cd91..76d9c27 100644 --- a/2018/src/day21.rs +++ b/2018/src/day21.rs @@ -1,9 +1,48 @@ +use std::collections::HashSet; use std::io::Read; use common::Solution; #[derive(Default)] -pub struct Day21 {} +pub struct Day21 { +} + +struct ValidInputs { + f: i64 +} + +impl ValidInputs { + pub fn new(start: i64) -> Self { + ValidInputs { + f: start + } + } +} + +impl Iterator for ValidInputs { + type Item = i64; + + fn next(&mut self) -> Option { + let mut f = self.f; + let mut e = f | 0x10000; + f = 13284195; + + loop { + let d = e & 0xff; + f += d; + f &= 0xffffff; + f *= 65899; + f &= 0xffffff; + + if 0x100 > e { + self.f = f; + return Some(f); + } + + e >>= 8; + } + } +} impl Day21 { pub fn new() -> Self { @@ -13,13 +52,22 @@ impl Day21 { impl Solution for Day21 { fn part1(&mut self, _input: &mut Read) -> String { - unimplemented!() + format!("{}", ValidInputs::new(0).next().unwrap()) } fn part2(&mut self, _input: &mut Read) -> String { - unimplemented!() + let inputs = ValidInputs::new(0); + let mut seen = HashSet::new(); + let mut last = None; + + for input in inputs { + if seen.contains(&input) { + return format!("{}", last.unwrap()); + } else { + last = Some(input); + seen.insert(input); + } + } + unreachable!(); } } - -#[cfg(test)] -mod tests {}