mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 21.
This commit is contained in:
@@ -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<i64> {
|
||||
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 {}
|
||||
|
||||
Reference in New Issue
Block a user