-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
163 lines (130 loc) · 4.41 KB
/
lib.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use common::{Answer, Coordinates, Grid};
use regex::Regex;
use std::sync::LazyLock;
type IntType = u16;
type Lights = Grid<IntType, bool>;
type DimmableLights = Grid<IntType, i32>;
type Coords = Coordinates<IntType>;
enum Instruction {
On(Coords, Coords),
Off(Coords, Coords),
Toggle(Coords, Coords),
}
fn parse(s: &str) -> impl Iterator<Item = Instruction> + '_ {
static EXPRESSION: &str =
r"(turn on|turn off|toggle) (\d{1,3}),(\d{1,3}) through (\d{1,3}),(\d{1,3})";
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(EXPRESSION).unwrap());
RE.captures_iter(s).map(|caps| {
let (_, [action, x1, y1, x2, y2]) = caps.extract();
let (x1, y1, x2, y2) = (
x1.parse().unwrap(),
y1.parse().unwrap(),
x2.parse().unwrap(),
y2.parse().unwrap(),
);
match action {
"turn on" => Instruction::On(Coordinates::new(x1, y1), Coordinates::new(x2, y2)),
"turn off" => Instruction::Off(Coordinates::new(x1, y1), Coordinates::new(x2, y2)),
"toggle" => Instruction::Toggle(Coordinates::new(x1, y1), Coordinates::new(x2, y2)),
_ => unreachable!(),
}
})
}
#[inline]
fn turn_on(lights: &mut Lights, pos: Coords) {
lights.store(pos, true);
}
#[inline]
fn turn_off(lights: &mut Lights, pos: Coords) {
lights.store(pos, false);
}
#[inline]
fn toggle(lights: &mut Lights, pos: Coords) {
let value = *lights.get(&pos).unwrap_or(&false);
lights.store(pos, !value);
}
#[inline]
fn count_lights(lights: &Lights) -> usize {
lights.iter().filter(|(_, &b)| b).count()
}
pub fn step1(s: &str) -> Answer {
let mut lights = Lights::new();
parse(s).for_each(|i| match i {
Instruction::On(a, b) => a.range_inclusive(b).for_each(|p| turn_on(&mut lights, p)),
Instruction::Off(a, b) => a.range_inclusive(b).for_each(|p| turn_off(&mut lights, p)),
Instruction::Toggle(a, b) => a.range_inclusive(b).for_each(|p| toggle(&mut lights, p)),
});
count_lights(&lights).into()
}
#[inline]
fn increase(lights: &mut DimmableLights, pos: Coords) {
*lights.get_mut(pos) += 1;
}
#[inline]
fn decrease(lights: &mut DimmableLights, pos: Coords) {
let value = lights.get_mut(pos);
if *value > 0 {
*value -= 1;
}
}
#[inline]
fn really_increase(lights: &mut DimmableLights, pos: Coords) {
*lights.get_mut(pos) += 2;
}
#[inline]
fn sum_brightness(lights: &DimmableLights) -> i32 {
lights.iter().map(|(_, l)| l).sum()
}
pub fn step2(s: &str) -> Answer {
let mut lights = DimmableLights::new();
parse(s).for_each(|i| match i {
Instruction::On(a, b) => a.range_inclusive(b).for_each(|p| increase(&mut lights, p)),
Instruction::Off(a, b) => a.range_inclusive(b).for_each(|p| decrease(&mut lights, p)),
Instruction::Toggle(a, b) => a
.range_inclusive(b)
.for_each(|p| really_increase(&mut lights, p)),
});
sum_brightness(&lights).into()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn dimmable_lights_store_correct_value_after_increase() {
let mut lights = DimmableLights::new();
increase(&mut lights, (1, 1).into());
assert_eq!(sum_brightness(&lights), 1);
}
#[test]
fn dimmable_lights_store_correct_value_after_decrease() {
let mut lights = DimmableLights::new();
increase(&mut lights, (1, 1).into());
decrease(&mut lights, (1, 1).into());
assert_eq!(sum_brightness(&lights), 0);
}
#[test]
fn dimmable_lights_store_correct_value_after_real_increase() {
let mut lights = DimmableLights::new();
really_increase(&mut lights, (1, 1).into());
assert_eq!(sum_brightness(&lights), 2);
}
#[test]
fn dimmable_lights_store_correct_value_after_multiple_changes() {
let mut lights = DimmableLights::new();
really_increase(&mut lights, (1, 1).into());
increase(&mut lights, (1, 1).into());
decrease(&mut lights, (1, 1).into());
really_increase(&mut lights, (1, 1).into());
assert_eq!(sum_brightness(&lights), 4);
}
#[test]
fn step2_handles_first_example() {
let answer = step2("turn on 0,0 through 0,0");
assert_eq!(answer, Answer::Signed(1));
}
#[test]
fn step2_handles_second_example() {
let answer = step2("toggle 0,0 through 999,999");
assert_eq!(answer, Answer::Signed(2_000_000));
}
}