generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.rs
171 lines (159 loc) · 4.45 KB
/
14.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
use std::collections::HashMap;
advent_of_code::solution!(14);
fn rotate_north(map: Vec<Vec<char>>) -> Vec<Vec<char>> {
let mut map = map;
for col in 0..map[0].len() {
let mut loc = 0;
for row in 0..map.len() {
match map[row][col] {
'O' => {
if loc != row {
map[loc][col] = 'O';
map[row][col] = '.';
}
loc += 1;
}
'#' => loc = row + 1,
_ => {}
}
}
}
map
}
fn rotate_south(map: Vec<Vec<char>>) -> Vec<Vec<char>> {
let mut map = map;
for col in 0..map[0].len() {
let mut loc = map.len() - 1;
for row in 0..map.len() {
let row = map.len() - row - 1;
match map[row][col] {
'O' => {
if loc != row {
map[loc][col] = 'O';
map[row][col] = '.';
}
loc = loc.saturating_sub(1);
}
'#' => {
if row > 0 {
loc = row - 1
}
}
_ => {}
}
}
}
map
}
fn rotate_east(map: Vec<Vec<char>>) -> Vec<Vec<char>> {
let mut map = map;
for row in 0..map.len() {
let mut loc = map[0].len() - 1;
for col in 0..map[0].len() {
let col = map[0].len() - col - 1;
match map[row][col] {
'O' => {
if loc != col {
map[row][loc] = 'O';
map[row][col] = '.';
}
loc = loc.saturating_sub(1);
}
'#' => {
if col > 0 {
loc = col - 1
}
}
_ => {}
}
}
}
map
}
fn rotate_west(map: Vec<Vec<char>>) -> Vec<Vec<char>> {
let mut map = map;
for row in 0..map.len() {
let mut loc = 0;
for col in 0..map[0].len() {
match map[row][col] {
'O' => {
if loc != col {
map[row][loc] = 'O';
map[row][col] = '.';
}
loc += 1;
}
'#' => loc = col + 1,
_ => {}
}
}
}
map
}
fn north_load(map: &Vec<Vec<char>>) -> u64 {
let mut acc = 0;
for col in 0..map[0].len() {
for row in 0..map.len() {
if map[row][col] == 'O' {
acc += map.len() - row;
}
}
}
acc as u64
}
fn run_cycle(map: Vec<Vec<char>>) -> Vec<Vec<char>> {
let mut map = map;
map = rotate_north(map);
map = rotate_west(map);
map = rotate_south(map);
map = rotate_east(map);
map
}
pub fn part_one(input: &str) -> Option<u64> {
let map: Vec<Vec<char>> = input.lines().map(|s| s.chars().collect()).collect();
let map = rotate_north(map);
let acc = north_load(&map);
Some(acc)
}
pub fn part_two(input: &str) -> Option<u64> {
let map: Vec<Vec<char>> = input.lines().map(|s| s.chars().collect()).collect();
// let printmap = |map: &Vec<Vec<char>>| {
// println!("");
// map.iter().for_each(|line| println!("{:?}", line));
// };
let mut seen: HashMap<String, usize> = HashMap::new();
let mut map = map;
let mut count = 0;
let start_cycle = loop {
map = run_cycle(map);
let as_string = map.iter().fold(String::new(), |acc, v| {
acc + &v.iter().cloned().collect::<String>()
});
if let Some(x) = seen.get(&as_string) {
break x;
}
seen.insert(as_string, count);
count += 1;
};
let rotations_needed = (1_000_000_000 - start_cycle - 1) % (count - start_cycle);
let map = (0..rotations_needed).fold(map, |mut map, _| {
map = run_cycle(map);
map
});
let acc = north_load(&map);
Some(acc)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(136));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(64));
}
}