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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
use std::io::{self, Read};
#[derive(Debug, Clone, Copy)]
struct Layer {
range: i32,
scanner: i32,
direction: i32,
}
impl Layer {
fn new(range: i32) -> Self {
Layer { range, scanner: 0, direction: 1 }
}
fn step(&mut self) {
if self.scanner == 0 {
self.direction = 1;
} else if self.scanner == self.range - 1 {
self.direction = -1;
}
self.scanner += self.direction;
}
}
fn solve1(input: &str) -> usize {
let mut layers = vec![];
for line in input.lines() {
let mut iter = line.split(": ");
let index: usize = iter.next().unwrap().parse().unwrap();
let range = iter.next().unwrap().parse().unwrap();
layers.resize(index + 1, None);
layers[index] = Some(Layer::new(range));
}
let mut severity = 0;
for i in 0..layers.len() {
if let Some(ref layer) = layers[i] {
if layer.scanner == 0 {
severity += i * layer.range as usize;
}
}
for layer in &mut layers {
layer.as_mut().map(Layer::step);
}
}
severity
}
fn solve2(input: &str) -> usize {
let mut init_layers = vec![];
for line in input.lines() {
let mut iter = line.split(": ");
let index: usize = iter.next().unwrap().parse().unwrap();
let range = iter.next().unwrap().parse().unwrap();
init_layers.resize(index + 1, None);
init_layers[index] = Some(Layer::new(range));
}
'delay: for delay in 0.. {
if delay > 0 {
for layer in &mut init_layers {
layer.as_mut().map(Layer::step);
}
}
let mut layers = init_layers.clone();
for i in 0..layers.len() {
if let Some(ref layer) = layers[i] {
if layer.scanner == 0 {
continue 'delay;
}
}
for layer in &mut layers {
layer.as_mut().map(Layer::step);
}
}
return delay;
}
unreachable!()
}
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
println!("Part 1: {}", solve1(&input));
println!("Part 2: {}", solve2(&input));
}
#[test]
fn part1() {
assert_eq!(24, solve1(
"\
0: 3
1: 2
4: 4
6: 4
"
));
}
#[test]
fn part2() {
assert_eq!(10, solve2(
"\
0: 3
1: 2
4: 4
6: 4
"
));
}
|