/
day_18.rs
164 lines (146 loc) · 3.88 KB
/
day_18.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
use std::fmt::Display;
use aoc_core::AoCError;
use itertools::Itertools;
use crate::{AoCData, AoCResult};
struct Coord {
x: i64,
y: i64,
}
impl Coord {
pub fn advance(&self, direction: &Dir, amount: i64) -> Self {
match direction {
Dir::Up => Self {
x: self.x + amount,
y: self.y,
},
Dir::Down => Self {
x: self.x - amount,
y: self.y,
},
Dir::Left => Self {
x: self.x,
y: self.y - amount,
},
Dir::Right => Self {
x: self.x,
y: self.y + amount,
},
}
}
}
struct Instr {
dir: Dir,
amount: i64,
}
enum Dir {
Up,
Down,
Left,
Right,
}
fn calc_area(mut instructions: impl Iterator<Item = AoCResult<Instr>>) -> AoCResult<i64> {
let (area, perimeter, _) = instructions.fold_ok(
(0, 0, Coord { x: 0, y: 0 }),
|(area, perimeter, pos), Instr { dir, amount }| {
let new_pos = pos.advance(&dir, amount);
let new_area = area + (pos.x * new_pos.y - new_pos.x * pos.y);
let new_perimeter = (new_pos.x - pos.x).abs() + (new_pos.y - pos.y).abs() + perimeter;
(new_area, new_perimeter, new_pos)
},
)?;
Ok((area.abs() + perimeter) / 2 + 1)
}
#[derive(Debug, Clone)]
pub struct Data<'a>(&'a str);
impl<'a> AoCData<'a> for Data<'a> {
fn try_new(input: &'a str) -> AoCResult<Self> {
Ok(Self(input))
}
fn part_1(&self) -> AoCResult<impl Display> {
let instructions = self.0.lines().map(|line| {
let (instr, _) = line
.split_once(" (")
.ok_or(AoCError::Parsing)?;
let (dir, amount) = instr
.split_once(" ")
.ok_or(AoCError::Parsing)?;
let dir = match dir {
"U" => Dir::Up,
"D" => Dir::Down,
"L" => Dir::Left,
"R" => Dir::Right,
_ => return Err(AoCError::Parsing),
};
let amount = amount
.parse()
.map_err(|_| AoCError::Parsing)?;
Ok(Instr { dir, amount })
});
calc_area(instructions)
}
fn part_2(&self) -> AoCResult<impl Display> {
let instructions = self.0.lines().map(|line| {
let line = line
.strip_suffix(")")
.ok_or(AoCError::Parsing)?;
let (_, hex) = line
.split_once("(#")
.ok_or(AoCError::Parsing)?;
let (amount, dir) = hex.split_at(5);
let amount = i64::from_str_radix(amount, 16).map_err(|_| AoCError::Parsing)?;
let dir = match dir {
"3" => Dir::Up,
"1" => Dir::Down,
"2" => Dir::Left,
"0" => Dir::Right,
_ => return Err(AoCError::Parsing),
};
Ok(Instr { dir, amount })
});
calc_area(instructions)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn part_1() {
let input = "R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)";
let data = Data::try_new(input).unwrap();
let result = data.part_1().unwrap().to_string();
assert_eq!(result, "62");
}
#[test]
fn part_2() {
let input = "R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)";
let data = Data::try_new(input).unwrap();
let result = data.part_2().unwrap().to_string();
assert_eq!(result, "952408144115");
}
}