-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.rs
111 lines (88 loc) · 2.91 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
use crate::direction::Direction;
use crate::point::Point;
use crate::shoelace_formula::shoelace_formula;
use crate::solutions::Solution;
use itertools::Itertools;
pub struct Day18;
impl Solution for Day18 {
fn part_one(&self, input: &str) -> String {
let instructions: Vec<Instruction> = Self::parse_input_part_one(input);
Self::solve(instructions)
}
fn part_two(&self, input: &str) -> String {
let instructions: Vec<Instruction> = Self::parse_input_part_two(input);
Self::solve(instructions)
}
}
impl Day18 {
fn parse_input_part_one(input: &str) -> Vec<Instruction> {
input
.lines()
.map(|line| {
let (dir, length, _) = line.split_whitespace().collect_tuple().unwrap();
let direction = match dir {
"R" => Direction::East,
"L" => Direction::West,
"U" => Direction::North,
"D" => Direction::South,
_ => unreachable!(),
};
Instruction::new(direction, length.parse().unwrap())
})
.collect()
}
fn parse_input_part_two(input: &str) -> Vec<Instruction> {
input
.lines()
.map(|line| {
let (_, _, color) = line.split_whitespace().collect_tuple().unwrap();
let dir: u8 = color[7..8].parse().unwrap();
let length = usize::from_str_radix(&color[2..7], 16).unwrap();
let direction = match dir {
0 => Direction::East,
1 => Direction::South,
2 => Direction::West,
3 => Direction::North,
_ => unreachable!(),
};
Instruction::new(direction, length)
})
.collect()
}
fn solve(instructions: Vec<Instruction>) -> String {
let mut last = Point::new(0, 0);
let mut trenches: Vec<Point> = vec![last];
for instruction in instructions {
let new = last.move_in_with_length(instruction.direction, instruction.length as isize);
trenches.push(new);
last = new;
}
shoelace_formula(&trenches).to_string()
}
}
#[derive(Debug)]
struct Instruction {
direction: Direction,
length: usize,
}
impl Instruction {
fn new(direction: Direction, length: usize) -> Self {
Self { direction, length }
}
}
#[cfg(test)]
mod tests {
use crate::file_system::read_example;
use crate::solutions::day18::Day18;
use crate::solutions::Solution;
#[test]
fn part_one_example_test() {
let input = read_example("18");
assert_eq!("62", Day18.part_one(input.as_str()));
}
#[test]
fn part_two_example_test() {
let input = read_example("18");
assert_eq!("952408144115", Day18.part_two(input.as_str()));
}
}