-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.rs
78 lines (65 loc) · 1.76 KB
/
day13.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
//! [Day 13: Packet Scanners](https://adventofcode.com/2017/day/13)
use std::collections::HashMap;
struct Puzzle {
heights: HashMap<u32, u32>,
}
impl Puzzle {
fn new() -> Self {
Self {
heights: HashMap::new(),
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
for line in data.lines() {
let mut line = line.split(": ");
let pos: u32 = line.next().unwrap().parse().unwrap();
let height: u32 = line.next().unwrap().parse().unwrap();
self.heights.insert(pos, height);
}
}
/// Solve part one.
fn part1(&self) -> u32 {
self.heights
.iter()
.filter(|&(&pos, &height)| pos % (2 * (height - 1)) == 0)
.map(|(&pos, &height)| pos * height)
.sum()
}
/// Solve part two.
fn part2(&self) -> u32 {
(0..10_000_000)
.find(|wait| {
!self
.heights
.iter()
.any(|(&pos, &height)| (wait + pos) % (2 * (height - 1)) == 0)
})
.unwrap()
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 24);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part2(), 10);
}
}