-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
147 lines (132 loc) · 3.97 KB
/
day18.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
type Point = (isize, isize);
#[derive(Debug, Clone, Copy)]
enum Direction {
Up,
Down,
Left,
Right,
}
#[derive(Debug)]
pub struct Instruction {
dir: Direction,
steps: isize,
dec_dir: Direction,
dec_steps: isize,
}
trait Lagoon {
fn find_volume(&self, decoded: bool) -> usize;
}
#[allow(unused_assignments)]
impl Lagoon for Vec<Instruction> {
fn find_volume(&self, decoded: bool) -> usize {
let mut start = (0, 0);
let mut vertices = Vec::from([start]);
let mut boundary = 0;
let mut dir = Direction::Up;
let mut steps = 0;
for inst in self {
if decoded {
dir = inst.dec_dir;
steps = inst.dec_steps;
} else {
dir = inst.dir;
steps = inst.steps;
}
let next = match dir {
Direction::Up => (start.0, start.1 + steps),
Direction::Down => (start.0, start.1 - steps),
Direction::Left => (start.0 - steps, start.1),
Direction::Right => (start.0 + steps, start.1),
};
vertices.push(next);
start = next;
boundary += steps;
}
vertices.shoestring().picks(boundary as usize) + boundary as usize
}
}
trait Area {
fn shoestring(&self) -> usize;
}
impl Area for Vec<Point> {
fn shoestring(&self) -> usize {
(self
.windows(2)
.fold(0, |acc, matrix|
acc + (matrix[0].0 * matrix[1].1) - (matrix[1].0 * matrix[0].1)
) / 2).abs() as usize
}
}
trait Interior {
fn picks(&self, boundary: usize) -> usize;
}
impl Interior for usize {
fn picks(&self, boundary: usize) -> usize {
self + 1 - boundary/2
}
}
#[aoc_generator(day18)]
pub fn input_generator(input: &str) -> Vec<Instruction> {
input
.lines()
.map(|line| {
let (dir_str, rest) = line.trim().split_once(' ').unwrap();
let dir = match dir_str {
"U" => Direction::Up,
"D" => Direction::Down,
"L" => Direction::Left,
"R" => Direction::Right,
_ => panic!("Not a valid direction."),
};
let (steps_str, dir_hex) = rest.split_once(' ').unwrap();
let steps = steps_str.parse::<isize>().unwrap();
let digits = dir_hex.chars()
.filter(|c| c.is_digit(16))
.collect::<String>();
let dec_dir = digits.chars().last().unwrap();
let dec_dir = match dec_dir {
'0' => Direction::Right,
'1' => Direction::Down,
'2' => Direction::Left,
'3' => Direction::Up,
_ => panic!("Not a valid direction."),
};
let dec_steps = isize::from_str_radix(&digits[..digits.len() - 1], 16).unwrap();
Instruction { dir, steps, dec_dir, dec_steps }
})
.collect()
}
#[aoc(day18, part1)]
pub fn solve_part1(input: &Vec<Instruction>) -> usize {
input.find_volume(false)
}
#[aoc(day18, part2)]
pub fn solve_part2(input: &Vec<Instruction>) -> usize {
input.find_volume(true)
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "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]
fn part1_test() {
assert_eq!(solve_part1(&input_generator(TEST)), 62);
}
#[test]
fn part2_test() {
assert_eq!(solve_part2(&input_generator(TEST)), 952_408_144_115);
}
}