This commit is contained in:
Stefan Bühler 2020-12-15 20:06:25 +01:00
parent 8ff8c4570b
commit d2d9750403

53
src/bin/day15.rs Normal file
View File

@ -0,0 +1,53 @@
use std::collections::HashMap;
const INPUT: &str = "5,2,8,16,18,0,1";
struct Memory {
turn: usize,
mem: HashMap<usize, usize>,
previous: usize,
}
impl Memory {
fn new() -> Self {
Self {
turn: 0,
mem: HashMap::new(),
previous: 0,
}
}
fn remember(&mut self, value: usize) {
if self.turn > 0 {
self.mem.insert(self.previous, self.turn);
}
self.turn += 1;
self.previous = value;
}
}
fn main() {
let input: Vec<usize> = INPUT.split(",").map(|s| s.parse().unwrap()).collect();
let mut mem = Memory::new();
for &value in &input {
mem.remember(value);
}
while mem.turn < 2020 {
let value = mem.previous;
if let Some(&turn) = mem.mem.get(&value) {
mem.remember(mem.turn - turn);
} else {
mem.remember(0);
}
}
println!("{}", mem.previous);
while mem.turn < 30000000 {
let value = mem.previous;
if let Some(&turn) = mem.mem.get(&value) {
mem.remember(mem.turn - turn);
} else {
mem.remember(0);
}
}
println!("{}", mem.previous);
}