day 15
This commit is contained in:
parent
8ff8c4570b
commit
d2d9750403
53
src/bin/day15.rs
Normal file
53
src/bin/day15.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user