-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.rs
138 lines (121 loc) · 3.48 KB
/
day9.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
//! [Day 9: Rope Bridge](https://adventofcode.com/2022/day/9)
use std::collections::HashSet;
struct Puzzle {
moves: Vec<(char, i32)>,
}
impl Puzzle {
fn new() -> Self {
Self { moves: vec![] }
}
/// Loads data from input (one line)
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
for line in data.split('\n').collect::<Vec<_>>() {
if !line.is_empty() {
let mut split = line.split(' ');
let direction = split.next().unwrap().chars().next().unwrap();
let n = split.next().unwrap().parse().unwrap();
self.moves.push((direction, n));
}
}
}
// Solves part one
fn part1(&self) -> usize {
let mut tails = HashSet::new();
let mut head = (0, 0);
let mut tail = (0, 0);
for m in &self.moves {
let direction = m.0;
let n = m.1;
for _ in 0..n {
Puzzle::move_segment(direction, &mut head, &mut tail);
tails.insert(tail);
}
}
tails.len()
}
// Solve part two
fn part2(&self) -> usize {
let mut tails = HashSet::new();
let mut rope = [
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
(0, 0),
];
for m in &self.moves {
let direction = m.0;
let n = m.1;
for _ in 0..n {
Puzzle::move_rope(direction, &mut rope);
tails.insert(rope.last().copied().unwrap());
}
}
tails.len()
}
/// Move a multi-segment rope
fn move_rope(direction: char, rope: &mut [(i32, i32)]) {
for k in 0..rope.len() - 1 {
let mut head = rope[k];
let mut tail = rope[k + 1];
Puzzle::move_segment(if k == 0 { direction } else { ' ' }, &mut head, &mut tail);
rope[k] = head;
rope[k + 1] = tail;
}
}
/// Move a segment
fn move_segment(direction: char, head: &mut (i32, i32), tail: &mut (i32, i32)) {
// head movement
let (dx, dy) = match direction {
'R' => (1, 0),
'L' => (-1, 0),
'U' => (0, 1),
'D' => (0, -1),
_ => (0, 0),
};
head.0 += dx;
head.1 += dy;
// tail movement
let dx = if head.0 - tail.0 > 0 { 1 } else { -1 };
let dy = if head.1 - tail.1 > 0 { 1 } else { -1 };
if tail.0 == head.0 {
if (tail.1 - head.1).abs() > 1 {
tail.1 += dy;
}
} else if tail.1 == head.1 {
if (tail.0 - head.0).abs() > 1 {
tail.0 += dx;
}
} else if (tail.0 - head.0).abs() + (tail.1 - head.1).abs() > 2 {
tail.0 += dx;
tail.1 += dy;
}
}
}
/// main function
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(&args.path);
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 13);
assert_eq!(puzzle.part2(), 1);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test02.txt");
assert_eq!(puzzle.part2(), 36);
}