-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
179 lines (155 loc) · 4.21 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
extern crate core;
use crate::ActionType::{Toggle, TurnOff, TurnOn};
pub fn fn1(input: &str) -> i32 {
let lines: Vec<_> = input.lines().collect();
let actions = lines
.iter()
.map(|s| {
let action;
let start;
match s.chars().nth(6).unwrap() {
'f' => {
action = TurnOff;
start = 9;
}
'n' => {
action = TurnOn;
start = 8;
}
' ' => {
action = Toggle;
start = 7;
}
_ => panic!("unhandled"),
}
let end = s[start + 1..].find(' ').unwrap() + start + 1;
let from = Pos::new(&s[start..end]);
let th = s.find("through ").unwrap() + "through ".len();
let to = Pos::new(&s[th..]);
Action {
action_type: action,
from,
to,
}
})
.collect::<Vec<_>>();
let mut grid = Vec::with_capacity(1000);
for _ in 0..1000 {
grid.push(vec![false; 1000]);
}
for action in actions {
for row in action.from.row..=action.to.row {
for col in action.from.col..=action.to.col {
match action.action_type {
TurnOn => grid[row][col] = true,
TurnOff => grid[row][col] = false,
Toggle => grid[row][col] = !grid[row][col],
}
}
}
}
grid.iter()
.map(|row| row.iter().filter(|&&v| v).count())
.fold(0, |sum, i| sum + i as i32)
}
#[derive(Debug)]
enum ActionType {
TurnOn,
TurnOff,
Toggle,
}
struct Action {
action_type: ActionType,
from: Pos,
to: Pos,
}
#[derive(Debug)]
struct Pos {
row: usize,
col: usize,
}
impl Pos {
fn new(s: &str) -> Self {
let i = s.find(',').unwrap();
Pos {
row: s[..i].parse::<usize>().unwrap(),
col: s[i + 1..].parse::<usize>().unwrap(),
}
}
}
pub fn fn2(input: &str) -> i32 {
let lines: Vec<_> = input.lines().collect();
let actions = lines
.iter()
.map(|s| {
let action;
let start;
match s.chars().nth(6).unwrap() {
'f' => {
action = TurnOff;
start = 9;
}
'n' => {
action = TurnOn;
start = 8;
}
' ' => {
action = Toggle;
start = 7;
}
_ => panic!("unhandled"),
}
let end = s[start + 1..].find(' ').unwrap() + start + 1;
let from = Pos::new(&s[start..end]);
let th = s.find("through ").unwrap() + "through ".len();
let to = Pos::new(&s[th..]);
Action {
action_type: action,
from,
to,
}
})
.collect::<Vec<_>>();
let mut grid = Vec::with_capacity(1000);
for _ in 0..1000 {
grid.push(vec![0; 1000]);
}
for action in actions {
for row in action.from.row..=action.to.row {
for col in action.from.col..=action.to.col {
match action.action_type {
TurnOn => grid[row][col] += 1,
TurnOff => {
grid[row][col] -= 1;
if grid[row][col] < 0 {
grid[row][col] = 0;
}
}
Toggle => grid[row][col] += 2,
}
}
}
}
let mut sum = 0;
for row in 0..1000 {
for col in 0..1000 {
sum += grid[row][col];
}
}
sum
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_fn1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn1(s.as_str()), 1);
}
#[test]
fn test_fn2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn2(s.as_str()), 1);
}
}