-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.rs
133 lines (110 loc) · 3.34 KB
/
day21.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
use ahash::AHashSet;
fn parse(input: &str) -> Vec<Vec<char>> {
input.lines().map(|l| l.chars().collect()).collect()
}
fn part1(map: &[Vec<char>], steps: usize) -> usize {
let mut positions = AHashSet::new();
positions.insert((map.len() / 2, map.len() / 2));
for _ in 0..steps {
let mut new_positions = AHashSet::new();
for (x, y) in positions {
let n = (x, y.wrapping_sub(1));
let w = (x.wrapping_sub(1), y);
let s = (x, y + 1);
let e = (x + 1, y);
for d in [n, w, s, e] {
if let Some(t) = map.get(d.1).and_then(|r| r.get(d.0)) {
if *t != '#' {
new_positions.insert(d);
}
}
}
}
positions = new_positions;
}
positions.len()
}
fn part2(map: &[Vec<char>], steps: usize) -> usize {
let start = (map.len() / 2, map.len() / 2);
let mut positions = AHashSet::new();
let height = map.len();
let width = map[0].len();
positions.insert((start.0 as i64, start.1 as i64));
let mut prev = 0;
let mut prev_diff = 0;
let mut prev_diff_diff = 0;
let mut next_step = 0;
for step in 0..steps {
let mut new_positions = AHashSet::new();
for (x, y) in positions.iter().copied() {
let n = (x, y - 1);
let w = (x - 1, y);
let s = (x, y + 1);
let e = (x + 1, y);
for d in [n, w, s, e] {
let t = map[d.1.rem_euclid(height as i64) as usize]
[d.0.rem_euclid(width as i64) as usize];
{
if t != '#' {
new_positions.insert(d);
}
}
}
}
if (step + 1) % width == steps % width {
let diff = new_positions.len() - prev;
let diff_diff = diff - prev_diff;
prev_diff = diff;
prev = new_positions.len();
if prev_diff_diff == diff_diff {
next_step = step + width;
positions = new_positions;
break;
}
prev_diff_diff = diff_diff;
}
positions = new_positions;
}
let mut plots = positions.len();
if next_step == 0 {
return plots;
}
// The number of plots grows quadratically after some steps.
// diff_diff is the change of diffs between width steps.
for _ in (next_step..steps).step_by(width) {
let diff = prev_diff + prev_diff_diff;
prev_diff = diff;
plots += diff;
}
plots
}
fn main() {
let input = include_str!("../../input/input21.txt");
let input = parse(input);
println!("part1 = {}", part1(&input, 64));
println!("part2 = {}", part2(&input, 26501365));
}
#[test]
fn test_day21() {
let input = "\
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........";
let input = parse(input);
assert_eq!(part1(&input, 6), 16);
assert_eq!(part2(&input, 6), 16);
assert_eq!(part2(&input, 10), 50);
assert_eq!(part2(&input, 50), 1594);
assert_eq!(part2(&input, 100), 6536);
assert_eq!(part2(&input, 500), 167004);
assert_eq!(part2(&input, 1000), 668697);
assert_eq!(part2(&input, 5000), 16733044);
}