-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.rs
73 lines (70 loc) · 1.82 KB
/
day25.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
pub fn part1<'a, I, S>(lines: I) -> Option<usize>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let mut grid: Vec<Vec<char>> = lines
.into_iter()
.map(|line| line.as_ref().chars().collect())
.collect();
let width = grid.get(0)?.len();
let height = grid.len();
if grid.iter().skip(1).any(|row| row.len() != width) {
return None;
}
let mut n = 1;
loop {
let mut a = false;
let mut b = false;
for row in &mut grid {
let indices: Vec<_> = (0..width)
.filter(|&i| row[i] == '>' && row[(i + 1) % width] == '.')
.collect();
if !indices.is_empty() {
a = true;
}
for i in indices {
row[i] = '.';
row[(i + 1) % width] = '>';
}
}
for col in 0..width {
let indices: Vec<_> = (0..height)
.filter(|&i| grid[i][col] == 'v' && grid[(i + 1) % height][col] == '.')
.collect();
if !indices.is_empty() {
b = true;
}
for i in indices {
grid[i][col] = '.';
grid[(i + 1) % height][col] = 'v';
}
}
if !a && !b {
return Some(n);
}
n += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::error::Error;
static EXAMPLE: &[&str] = &[
"v...>>.vv>",
".vv>>.vv..",
">>.>v>...v",
">>v>>.>.v.",
"v>v.vv.v..",
">.>>..v...",
".vv..>.>v.",
"v.v..>>v.v",
"....v..v.>",
];
#[test]
fn part1_examples() -> Result<(), Box<dyn Error + Send + Sync>> {
assert_eq!(Some(58), part1(EXAMPLE));
Ok(())
}
}