-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
151 lines (127 loc) · 3.56 KB
/
mod.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
use std::collections::HashMap;
const DEBUG: bool = false;
fn load(matrix: &mut [Vec<u8>], max: (usize, usize)) -> usize{
let mut sum = 0;
for (y, row) in matrix.iter().enumerate() {
for c in row {
if *c == b'O' {
sum += max.1 - y;
}
}
}
sum
}
fn tilt_north(matrix: &mut [Vec<u8>], max: (usize, usize)) {
let mut off_list = vec![0; max.0];
for y in 0..max.1 {
for x in 0..max.0 {
if matrix[y][x] == b'O' {
// swap
matrix[y][x] = b'.';
matrix[off_list[x]][x] = b'O';
off_list[x] += 1;
}
if matrix[y][x] == b'#' {
off_list[x] = y + 1;
}
}
}
}
fn tilt_west(matrix: &mut [Vec<u8>], max: (usize, usize)) {
let mut off_list = vec![0; max.1];
for y in 0..max.1 {
for x in 0..max.0 {
if matrix[y][x] == b'O' {
// swap
matrix[y][x] = b'.';
matrix[y][off_list[y]] = b'O';
off_list[y] += 1;
}
if matrix[y][x] == b'#' {
off_list[y] = x + 1;
}
}
}
}
fn tilt_south(matrix: &mut [Vec<u8>], max: (usize, usize)) {
let mut off_list = vec![max.1 - 1; max.0];
for y in (0..max.1).rev() {
for x in 0..max.0 {
if matrix[y][x] == b'O' {
// swap
matrix[y][x] = b'.';
matrix[off_list[x]][x] = b'O';
if off_list[x] > 0 {
off_list[x] -= 1;
}
}
if matrix[y][x] == b'#' && y > 0 {
off_list[x] = y - 1;
}
}
}
}
fn tilt_east(matrix: &mut [Vec<u8>], max: (usize, usize)) {
let mut off_list = vec![max.0 - 1; max.1];
for y in 0..max.1 {
for x in (0..max.0).rev() {
if matrix[y][x] == b'O' {
// swap
matrix[y][x] = b'.';
matrix[y][off_list[y]] = b'O';
if off_list[y] > 0 {
off_list[y] -= 1;
}
}
if matrix[y][x] == b'#' && x > 0 {
off_list[y] = x - 1;
}
}
}
}
fn pretty(matrix: &[Vec<u8>]) {
if !DEBUG {
return;
}
println!();
for row in matrix {
for c in row {
print!("{}", *c as char);
}
println!()
}
}
pub fn solve(input: String) {
let mut matrix = aoc::parse_matrix(input);
let max = (matrix[0].len(), matrix.len());
let mut load_one = 0;
let mut memo = HashMap::new();
let mut cycle = 0;
let max_cycle = 1000000000;
while cycle < max_cycle {
tilt_north(&mut matrix, max);
if cycle == 0 {
load_one = load(&mut matrix, max);
}
pretty(&matrix);
tilt_west(&mut matrix, max);
pretty(&matrix);
tilt_south(&mut matrix, max);
pretty(&matrix);
tilt_east(&mut matrix, max);
pretty(&matrix);
if let Some(last_seen) = memo.insert(matrix.clone(), cycle) {
// fast forward (if possible)
let cycle_len = cycle - last_seen;
// 9 += 7 * (( 100 - 1 - 9 ) / 7 )
// 9 += 7 * 12 => 93
// not the perfect solution, but easy enough
cycle += cycle_len * ((max_cycle - 1 - cycle) / cycle_len);
}
cycle += 1;
}
aoc::print_solution(&[
load_one,
load(&mut matrix, max)
]);
}