-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.rs
157 lines (138 loc) · 3.49 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
#![feature(test)]
use std::{
hash::Hash,
ops::{Index, IndexMut},
};
use itertools::Itertools;
use rustc_hash::FxHashMap;
type Input = Grid;
#[derive(Debug, Clone, Copy)]
enum Cell {
Empty,
Cube,
Rounded,
}
#[derive(Debug, Clone)]
struct Grid {
grid: Vec<Vec<Cell>>,
rotation: u8,
}
fn setup(input: &str) -> Input {
let grid = input
.lines()
.map(|line| {
line.bytes()
.map(|b| match b {
b'.' => Cell::Empty,
b'#' => Cell::Cube,
b'O' => Cell::Rounded,
_ => panic!(),
})
.collect()
})
.collect();
Grid { grid, rotation: 0 }
}
impl Grid {
fn width(&self) -> usize {
if self.rotation % 2 == 0 {
self.grid[0].len()
} else {
self.grid.len()
}
}
fn height(&self) -> usize {
if self.rotation % 2 == 0 {
self.grid.len()
} else {
self.grid[0].len()
}
}
fn index(&self, i: usize, j: usize) -> (usize, usize) {
let w = self.grid[0].len();
let h = self.grid.len();
match self.rotation % 4 {
0 => (i, j),
1 => (w - j - 1, i),
2 => (h - i - 1, w - j - 1),
3 => (j, h - i - 1),
_ => unreachable!(),
}
}
fn tilt_north(&mut self) {
for i in 1..self.height() {
for j in 0..self.width() {
if !matches!(self[(i, j)], Cell::Rounded) {
continue;
}
let mut k = i;
while k > 0 && matches!(self[(k - 1, j)], Cell::Empty) {
self[(k, j)] = Cell::Empty;
self[(k - 1, j)] = Cell::Rounded;
k -= 1;
}
}
}
}
fn total_load(&self) -> usize {
self.grid
.iter()
.rev()
.enumerate()
.map(|(i, row)| row.iter().filter(|x| matches!(x, Cell::Rounded)).count() * (i + 1))
.sum()
}
fn cycle(&mut self) {
for i in 0..4 {
self.rotation = i;
self.tilt_north();
}
}
fn hash(&self) -> impl Hash + Eq {
type Chunk = u128;
self.grid
.iter()
.flatten()
.chunks(Chunk::BITS as _)
.into_iter()
.map(|c| c.fold(0, |acc, x| acc << 1 | matches!(x, Cell::Rounded) as Chunk))
.fold(0, |a, b| a ^ b)
}
}
impl Index<(usize, usize)> for Grid {
type Output = Cell;
fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
let (i, j) = self.index(i, j);
&self.grid[i][j]
}
}
impl IndexMut<(usize, usize)> for Grid {
fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut Self::Output {
let (i, j) = self.index(i, j);
&mut self.grid[i][j]
}
}
fn part1(input: &Input) -> usize {
let mut grid = input.clone();
grid.tilt_north();
grid.total_load()
}
fn part2(input: &Input) -> usize {
let mut grid = input.clone();
let mut n = 1000000000u32;
let mut hist = FxHashMap::default();
while n > 0 {
if let Some(k) = hist.insert(grid.hash(), n) {
let cycle_length = k - n;
n %= cycle_length;
break;
}
grid.cycle();
n -= 1;
}
for _ in 0..n {
grid.cycle();
}
grid.total_load()
}
aoc::main!(2023, 14, ex: 1);