-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.rs
68 lines (63 loc) · 1.83 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
use itertools::Itertools;
fn main() {
let input = include_str!("../../input/25.txt");
println!("Part 1: {}", part_1(input));
}
fn part_1(input: &str) -> i64 {
let mut map = input.lines().map(|l| l.chars().collect()).collect();
let mut round = 0;
loop {
let move_1 = step(&mut map, true);
let move_2 = step(&mut map, false);
round += 1;
if !move_1 && !move_2 {
break round;
}
}
}
fn step(map: &mut Vec<Vec<char>>, go_east: bool) -> bool {
let (direction_south, direction_east) = if go_east { (0, 1) } else { (1, 0) };
let (height, width) = (map.len(), map[0].len());
let (moved, new_map) = (0..height).cartesian_product(0..width).fold(
(false, vec![vec!['.'; width]; height]),
|(moved, mut new_map), (south, east)| match map[south][east] {
'>' if map[south][(east + direction_east) % width] == '.' => {
new_map[south][(east + direction_east) % width] = '>';
(true, new_map)
}
'v' if map[(south + direction_south) % height][east] == '.' => {
new_map[(south + direction_south) % height][east] = 'v';
(true, new_map)
}
'>' => {
new_map[south][east] = '>';
(moved, new_map)
}
'v' => {
new_map[south][east] = 'v';
(moved, new_map)
}
_ => (moved, new_map),
},
);
*map = new_map;
moved
}
#[cfg(test)]
mod day_25_tests {
use super::*;
static INPUT: &str = "v...>>.vv>
.vv>>.vv..
>>.>v>...v
>>v>>.>.v.
v>v.vv.v..
>.>>..v...
.vv..>.>v.
v.v..>>v.v
....v..v.>";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT), 58);
assert_eq!(part_1(include_str!("../../input/25.txt")), 321);
}
}