-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
87 lines (72 loc) · 1.87 KB
/
day15.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! [Day 15: Rambunctious Recitation](https://adventofcode.com/2020/day/15)
use rustc_hash::FxHashMap;
struct Puzzle {
nums: Vec<usize>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
nums: data
.trim()
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect(),
}
}
fn solve(&self, number_spoken: usize) -> usize {
let mut last_spoken = FxHashMap::default();
let mut last_last_spoken = FxHashMap::default();
let mut first_spoken = false;
let mut last = 0;
let mut n = 0;
for turn in 1.. {
if turn <= self.nums.len() {
n = self.nums[turn - 1];
} else if first_spoken {
n = 0;
} else {
n = last_spoken[&last] - last_last_spoken[&n];
};
if turn == number_spoken {
break;
}
if last_spoken.contains_key(&n) {
first_spoken = false;
last_last_spoken.insert(n, last_spoken[&n]);
} else {
first_spoken = true;
}
last_spoken.insert(n, turn);
last = n;
}
n
}
/// Solve part one.
fn part1(&self) -> usize {
self.solve(2020)
}
/// Solve part two.
fn part2(&self) -> usize {
self.solve(30_000_000)
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (usize, usize) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test_part1() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 436);
}
}