You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

use std::collections::HashMap;
const INPUT: &str = include_str!("../../data/day15");
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.trim().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);
}