-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
208 lines (185 loc) · 5.42 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Problem: https://adventofcode.com/2022/day/23
use itertools::Itertools;
use std::{
collections::{HashMap, HashSet, VecDeque},
ops::Add,
};
use Direction::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point(i32, i32);
impl Add<Point> for Point {
type Output = Point;
fn add(self, rhs: Point) -> Self::Output {
Point(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl Point {
fn row(&self) -> i32 {
self.0
}
fn col(&self) -> i32 {
self.1
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
N,
NE,
E,
SE,
S,
SW,
W,
NW,
}
impl Direction {
fn relative(&self) -> Point {
match self {
Direction::N => Point(-1, 0),
Direction::NE => Point(-1, 1),
Direction::E => Point(0, 1),
Direction::SE => Point(1, 1),
Direction::S => Point(1, 0),
Direction::SW => Point(1, -1),
Direction::W => Point(0, -1),
Direction::NW => Point(-1, -1),
}
}
}
fn parse_locations(input: &str) -> HashSet<Point> {
let mut result = HashSet::new();
for (row, line) in input.lines().enumerate() {
for (col, ch) in line.chars().enumerate() {
if ch == '#' {
result.insert(Point(row as i32, col as i32));
}
}
}
result
}
fn check_target(p: Point, dir: Direction, points: &HashSet<Point>) -> bool {
let dirs: [Direction; 3] = match dir {
N => [NW, N, NE],
S => [SW, S, SE],
W => [NW, W, SW],
E => [NE, E, SE],
_ => panic!(),
};
dirs.iter().all(|d| !points.contains(&(p + d.relative())))
}
fn bounds(elves: &HashSet<Point>) -> ((i32, i32), (i32, i32)) {
let (min_row, max_row) = elves
.iter()
.map(|p| p.row())
.minmax()
.into_option()
.unwrap();
let (min_col, max_col) = elves
.iter()
.map(|p| p.col())
.minmax()
.into_option()
.unwrap();
((min_row, max_row), (min_col, max_col))
}
#[allow(dead_code)]
fn print_elves(elves: &HashSet<Point>) -> String {
let ((min_row, max_row), (min_col, max_col)) = bounds(elves);
let mut result: String = String::new();
for row in min_row..=max_row {
for col in min_col..=max_col {
if elves.contains(&Point(row, col)) {
result += "#";
} else {
result += ".";
}
}
result += "\n";
}
result
}
fn move_elves(elves: &HashSet<Point>, target_order: &VecDeque<Direction>) -> HashSet<Point> {
let mut move_targets: HashMap<Point, usize> = HashMap::new();
let mut moves = vec![];
let mut next_elves = HashSet::new();
for p in elves.iter() {
if target_order.iter().all(|d| check_target(*p, *d, elves)) {
next_elves.insert(*p);
} else {
let mut found = false;
for t in target_order.clone() {
if check_target(*p, t, elves) {
*move_targets.entry(*p + t.relative()).or_default() += 1;
moves.push((*p, t));
found = true;
break;
}
}
if !found {
next_elves.insert(*p);
}
}
}
for (p, t) in moves {
let d = p + t.relative();
if move_targets.get(&d).unwrap() == &1 {
assert!(!next_elves.contains(&d));
next_elves.insert(d);
} else {
assert!(!next_elves.contains(&p));
next_elves.insert(p);
}
}
next_elves
}
fn part1(input: &str) -> usize {
let mut elves = parse_locations(input);
let mut target_order: VecDeque<Direction> = vec![N, S, W, E].into();
for _ in 0..10 {
elves = move_elves(&elves, &target_order);
let tmp = target_order.pop_front().unwrap();
target_order.push_back(tmp);
}
let b = bounds(&elves);
let ((min_row, max_row), (min_col, max_col)) = b;
((max_row - min_row + 1) as usize * (max_col - min_col + 1) as usize) - elves.len()
}
#[test]
fn test_small() {
let small = parse_locations(include_str!("small.txt"));
let small1 = parse_locations(include_str!("small1.txt"));
let small2 = parse_locations(include_str!("small2.txt"));
let small3 = parse_locations(include_str!("small3.txt"));
let calc1 = move_elves(&small, &[N, S, W, E].into());
assert_eq!(print_elves(&calc1), print_elves(&small1));
let calc2 = move_elves(&small1, &[S, W, E, N].into());
assert_eq!(print_elves(&calc2), print_elves(&small2));
let calc3 = move_elves(&small2, &[W, E, N, S].into());
assert_eq!(print_elves(&calc3), print_elves(&small3));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 110);
}
fn part2(input: &str) -> usize {
let mut elves = parse_locations(input);
let mut target_order: VecDeque<Direction> = vec![N, S, W, E].into();
for i in 0.. {
let next = move_elves(&elves, &target_order);
if next == elves {
return i + 1;
}
elves = next;
let tmp = target_order.pop_front().unwrap();
target_order.push_back(tmp);
}
unreachable!()
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test.txt")), 20);
}
fn main() {
println!("Part 1: {:?}", part1(include_str!("input.txt")));
println!("Part 2: {:?}", part2(include_str!("input.txt")));
}