-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.rs
68 lines (63 loc) · 1.72 KB
/
25.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
#![feature(test)]
#[derive(Copy, Clone, Eq, PartialEq)]
enum Type {
Empty,
Right,
Down,
}
type Input = Vec<Vec<Type>>;
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'v' => Type::Down,
'>' => Type::Right,
_ => Type::Empty,
})
.collect()
})
.collect()
}
fn part1(input: &Input) -> String {
let mut grid = input.clone();
let mut cnt = 0;
loop {
let mut moved = false;
let mut new_grid = grid.clone();
for (i, line) in grid.iter().enumerate() {
for (j, &c) in line.iter().enumerate() {
if c == Type::Right {
let k = (j + 1) % line.len();
if grid[i][k] == Type::Empty {
new_grid[i][k] = Type::Right;
new_grid[i][j] = Type::Empty;
moved = true;
}
}
}
}
grid = new_grid;
let mut new_grid = grid.clone();
for (i, line) in grid.iter().enumerate() {
for (j, &c) in line.iter().enumerate() {
if c == Type::Down {
let k = (i + 1) % grid.len();
if grid[k][j] == Type::Empty {
new_grid[k][j] = Type::Down;
new_grid[i][j] = Type::Empty;
moved = true;
}
}
}
}
grid = new_grid;
cnt += 1;
if !moved {
break;
}
}
cnt.to_string()
}
aoc::main!(2021, 25, ex: 1);