-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.rs
More file actions
133 lines (115 loc) · 3.23 KB
/
04.rs
File metadata and controls
133 lines (115 loc) · 3.23 KB
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
advent_of_code::solution!(4);
pub fn part_one(input: &str) -> Option<u64> {
let map = input;
let (accessible_rolls, _new_map) = move_rolls(map);
// println!("{}", new_map);
Some(accessible_rolls)
}
fn move_rolls(input: &str) -> (u64, String) {
let lines = lines(input);
let _height = lines.len();
let _width = lines[0].len();
let mut new_map = String::new();
let mut accessible_rolls = 0;
for h in 0..lines.len() {
for w in 0..lines[0].len() {
if !is_roll(h, w, &lines) {
new_map.push('.');
continue;
}
let neighbors = num_neighbors(h, w, &lines);
if neighbors < 4 {
accessible_rolls += 1;
new_map.push('x');
} else {
new_map.push('@');
}
}
new_map.push('\n');
}
(accessible_rolls, new_map)
}
fn lines(input: &str) -> Vec<&str> {
input.lines().filter(|line| !line.is_empty()).collect()
}
fn is_roll(h: usize, w: usize, lines: &[&str]) -> bool {
lines[h].as_bytes()[w] == b'@'
}
fn num_neighbors(h: usize, w: usize, lines: &[&str]) -> u32 {
let mut tests = Vec::new();
if h > 0 {
// is not top row.
if w > 0 {
tests.push((h - 1, w - 1));
}
tests.push((h - 1, w));
if w < lines[0].len() - 1 {
tests.push((h - 1, w + 1));
}
}
if w > 0 {
tests.push((h, w - 1));
}
if w < lines[0].len() - 1 {
tests.push((h, w + 1));
}
if h < lines.len() - 1 {
// is not bottom row.
if w > 0 {
tests.push((h + 1, w - 1));
}
tests.push((h + 1, w));
if w < lines[0].len() - 1 {
tests.push((h + 1, w + 1));
}
}
tests
.iter()
.map(|(nh, nw)| is_roll(*nh, *nw, lines))
.filter(|&y| y)
.count() as u32
}
pub fn part_two(input: &str) -> Option<u64> {
let mut map: String = input.to_string();
let mut net_rolls = 0;
loop {
let (accessible_rolls, new_map) = move_rolls(&map);
if accessible_rolls == 0 {
break;
}
net_rolls += accessible_rolls;
map = new_map.to_string();
}
// println!("{}", new_map);
Some(net_rolls)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_roll() {
let input = &advent_of_code::template::read_file("examples", DAY);
let lines = lines(input);
assert!(!is_roll(0, 0, &lines));
assert!(!is_roll(0, 1, &lines));
assert!(is_roll(1, 0, &lines));
assert!(is_roll(0, 2, &lines));
}
#[test]
fn test_num_neighbors() {
let _input = &advent_of_code::template::read_file("examples", DAY);
let test = "@..\n.@.\n...\n";
assert!(is_roll(0, 0, &lines(test)));
assert_eq!(num_neighbors(1, 1, &lines(test)), 1);
}
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(13));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(43));
}
}