-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.rs
174 lines (149 loc) · 4.17 KB
/
day14.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
use ahash::AHashMap;
fn parse(input: &str) -> Vec<Vec<char>> {
input.lines().map(|l| l.chars().collect()).collect()
}
fn tilt_north(dish: &mut Vec<Vec<char>>) {
for y in 0..dish.len() {
for x in 0..dish[y].len() {
if dish[y][x] == 'O' {
dish[y][x] = '.';
let mut curr_y = y;
loop {
let next_y = curr_y.wrapping_sub(1);
match dish.get(next_y).and_then(|r| r.get(x)) {
Some('.') => curr_y = next_y,
Some(_) | None => break,
};
}
dish[curr_y][x] = 'O';
}
}
}
}
fn tilt_west(dish: &mut Vec<Vec<char>>) {
for y in 0..dish.len() {
for x in 0..dish[y].len() {
if dish[y][x] == 'O' {
dish[y][x] = '.';
let mut curr_x = x;
loop {
let next_x = curr_x.wrapping_sub(1);
match dish.get(y).and_then(|r| r.get(next_x)) {
Some('.') => curr_x = next_x,
Some(_) | None => break,
};
}
dish[y][curr_x] = 'O';
}
}
}
}
fn tilt_south(dish: &mut Vec<Vec<char>>) {
for y in (0..dish.len()).rev() {
for x in 0..dish[y].len() {
if dish[y][x] == 'O' {
dish[y][x] = '.';
let mut curr_y = y;
loop {
let next_y = curr_y + 1;
match dish.get(next_y).and_then(|r| r.get(x)) {
Some('.') => curr_y = next_y,
Some(_) | None => break,
};
}
dish[curr_y][x] = 'O';
}
}
}
}
fn tilt_east(dish: &mut Vec<Vec<char>>) {
for y in 0..dish.len() {
for x in (0..dish[y].len()).rev() {
if dish[y][x] == 'O' {
dish[y][x] = '.';
let mut curr_x = x;
loop {
let next_x = curr_x + 1;
match dish.get(y).and_then(|r| r.get(next_x)) {
Some('.') => curr_x = next_x,
Some(_) | None => break,
};
}
dish[y][curr_x] = 'O';
}
}
}
}
fn total_load(dish: &Vec<Vec<char>>) -> i64 {
let mut sum = 0;
for (y, row) in dish.iter().enumerate() {
for tile in row.iter() {
if *tile == 'O' {
sum += (dish.len() - y) as i64;
}
}
}
sum
}
fn part1(puzzle: &[Vec<char>]) -> i64 {
let mut dish = puzzle.to_vec();
tilt_north(&mut dish);
total_load(&dish)
}
fn part2(puzzle: &[Vec<char>]) -> i64 {
let mut dish = puzzle.to_vec();
let mut cache = AHashMap::new();
let mut loop_start = None;
for cycle in 1.. {
tilt_north(&mut dish);
tilt_west(&mut dish);
tilt_south(&mut dish);
tilt_east(&mut dish);
let key = dish.clone();
if let Some((start, _)) = cache.get(&key) {
loop_start = Some(*start);
break;
} else {
cache.insert(key, (cycle, total_load(&dish)));
}
}
let loop_start = loop_start.unwrap();
let mut cycle = cache
.into_iter()
.filter_map(
|(_, elem @ (n, _))| {
if n >= loop_start {
Some(elem)
} else {
None
}
},
)
.collect::<Vec<_>>();
cycle.sort_by(|l, r| l.0.cmp(&r.0));
let solution_idx = (1000000000 - loop_start) % cycle.len();
cycle[solution_idx].1
}
fn main() {
let input = include_str!("../../input/input14.txt");
let input = parse(input);
println!("part1 = {}", part1(&input));
println!("part2 = {}", part2(&input));
}
#[test]
fn test_day14() {
let input = "\
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....";
let input = parse(input);
assert_eq!(part1(&input), 136);
assert_eq!(part2(&input), 64);
}