-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.rs
155 lines (141 loc) · 3.25 KB
/
09.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
/*
* Day 9: Rope Bridge
* See [https://adventofcode.com/2022/day/9]
*/
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Pt {
x: i32,
y: i32,
}
macro_rules! pt {
[$x: expr, $y: expr] => { Pt { x: $x, y: $y } };
[] => { Pt { x: 0, y: 0 } };
}
enum Side {
X, Y
}
fn parse_step(l: &str) -> (Side, i32) {
use Side::*;
let n: i32 = l[2..].parse().unwrap();
match l.as_bytes()[0] {
b'U' => (Y, n),
b'D' => (Y, -n),
b'L' => (X, -n),
b'R' => (X, n),
_ => panic!("Unknown direction"),
}
}
pub fn part_1(input: &str) -> Option<usize> {
let mut hx = 0;
let mut hy = 0;
let mut tx = 0;
let mut ty = 0;
let mut paths: HashSet<Pt> = HashSet::new();
paths.insert(pt![]);
for l in input.lines() {
let (side, st) = parse_step(l);
match side {
Side::Y => {
hy += st;
let d = hy.abs_diff(ty);
if d > 1 {
tx = hx;
let p = st.signum();
for _ in 1..d {
ty += p;
paths.insert(pt![tx, ty]);
}
}
}
Side::X => {
hx += st;
let d = hx.abs_diff(tx);
if d > 1 {
ty = hy;
let p = st.signum();
for _ in 1..d {
tx += p;
paths.insert(pt![tx, ty]);
}
}
}
}
}
Some(paths.len())
}
#[inline]
fn abs_d(p1: &Pt, p2: &Pt) -> u8 {
if p1.x.abs_diff(p2.x) > 1 {
if p1.y != p2.y {
2
} else {
1
}
} else if p1.y.abs_diff(p2.y) > 1 {
if p1.x != p2.x {
2
} else {
1
}
} else {
0
}
}
macro_rules! pt_cmp {
($xp: expr, $cp: expr) => {
if $xp > $cp {
$cp + 1
} else {
$cp - 1
}
};
}
#[inline]
fn adv(pos: &mut [Pt]) {
for i in 1..pos.len() {
let cp = &pos[i].clone();
let xp = &pos[i - 1];
let d = abs_d(cp, xp);
if d == 2 {
pos[i] = pt![pt_cmp!(xp.x, cp.x), pt_cmp!(xp.y, cp.y)];
} else if d == 1 {
if cp.x == xp.x {
pos[i].y = pt_cmp!(xp.y, cp.y)
} else {
pos[i].x = pt_cmp!(xp.x, cp.x)
}
}
}
}
pub fn part_2(input: &str) -> Option<u32> {
let pp: &mut [Pt; 10] = &mut [pt![]; 10];
let mut paths: HashSet<Pt> = HashSet::new();
for l in input.lines() {
let (side, st) = parse_step(l);
let one = st.signum();
for _ in 0..st.abs() {
match side {
Side::X => pp[0].x += one,
Side::Y => pp[0].y += one
}
adv(pp);
paths.insert(pp[9]);
}
}
Some(paths.len() as u32)
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use super::*;
use aoc2022::assert_ex;
#[test]
fn test_part_1() {
assert_ex!(part_1, 13);
}
#[test]
fn test_part_2() {
assert_ex!(part_2, 1);
}
}