-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.rs
More file actions
129 lines (116 loc) · 3.37 KB
/
Copy pathday18.rs
File metadata and controls
129 lines (116 loc) · 3.37 KB
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
use aoc_runner_derive::aoc;
fn parse1(input: &str) -> Vec<(utils::Direction, u32)> {
use itertools::Itertools;
input
.lines()
.map(|line| {
let (direction, distance, _color) =
line.split_ascii_whitespace().collect_tuple().unwrap();
let direction = utils::Direction::try_from(direction.chars().next().unwrap()).unwrap();
let distance = distance.parse().unwrap();
(direction, distance)
})
.collect()
}
fn parse2(input: &str) -> Vec<(utils::Direction, u32)> {
use itertools::Itertools;
input
.lines()
.map(|line| {
let (_, _, instruction) = line.split_ascii_whitespace().collect_tuple().unwrap();
let instruction = instruction
.strip_prefix("(#")
.unwrap()
.strip_suffix(')')
.unwrap();
let direction =
utils::Direction::try_from(instruction.chars().last().unwrap()).unwrap();
let distance = u32::from_str_radix(&instruction[..instruction.len() - 1], 16).unwrap();
(direction, distance)
})
.collect()
}
#[aoc(day18, part1)]
#[must_use]
pub fn part1(input: &str) -> u64 {
let input = parse1(input);
utils::compute_area(&input)
}
#[aoc(day18, part2)]
#[must_use]
pub fn part2(input: &str) -> u64 {
let input = parse2(input);
utils::compute_area(&input)
}
mod utils {
pub enum Direction {
Right,
Down,
Left,
Up,
}
impl TryFrom<char> for Direction {
type Error = &'static str;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'R' | '0' => Ok(Direction::Right),
'D' | '1' => Ok(Direction::Down),
'L' | '2' => Ok(Direction::Left),
'U' | '3' => Ok(Direction::Up),
_ => Err("Invalid direction"),
}
}
}
pub fn compute_area(instruction: &[(Direction, u32)]) -> u64 {
let mut area = 0;
let mut vertical_offset = 0;
let mut perimeter = 0;
for (direction, distance) in instruction {
match direction {
Direction::Right => {
area += vertical_offset * i64::from(*distance);
}
Direction::Down => {
vertical_offset += i64::from(*distance);
}
Direction::Left => {
area -= vertical_offset * i64::from(*distance);
}
Direction::Up => {
vertical_offset -= i64::from(*distance);
}
}
perimeter += u64::from(*distance);
}
area.unsigned_abs() + (perimeter / 2) + 1
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
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)
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 62);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 952_408_144_115);
}
}