-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-15.rs
48 lines (40 loc) · 970 Bytes
/
day-15.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::collections::HashMap;
const INPUT: &str = "10,16,6,0,1,17";
fn main() {
let mut mem = Memory::new();
println!("part 1: {}", mem.step_until(2020));
println!("part 2: {}", mem.step_until(30000000));
}
struct Memory {
spoken: HashMap<usize, usize>,
turn: usize,
next: usize,
}
impl Memory {
fn new() -> Self {
let mut mem = Self {
spoken: HashMap::new(),
turn: 1,
next: 0,
};
for n in INPUT.split(",").map(|s| s.parse().unwrap()) {
mem.next = n;
mem.step();
}
mem
}
fn step(&mut self) {
self.next = if let Some(prev) = self.spoken.insert(self.next, self.turn) {
self.turn - prev
} else {
0
};
self.turn += 1;
}
fn step_until(&mut self, until: usize) -> usize {
while self.turn < until {
self.step();
}
self.next
}
}