-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
118 lines (104 loc) · 3.14 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
use aoc_lib::{
answer::Answer,
directions::{Cardinal, Direction},
solution::Solution,
vec2::Vec2,
};
pub struct Day18;
impl Solution for Day18 {
fn part_a(&self, input: &[String]) -> Answer {
let plan = parse_a(input);
solve(plan.instructions).into()
}
fn part_b(&self, input: &[String]) -> Answer {
let plan = parse_b(input);
solve(plan.instructions).into()
}
}
fn solve(instructions: Vec<Instruction>) -> isize {
let mut pos = Vec2::new(0_isize, 0_isize);
let mut perimeter = 0;
let mut area = 0;
for instruction in instructions {
let step = instruction.len as isize;
perimeter += step;
let after = instruction.direction.to_offset() * step;
pos += after;
area += pos.x * after.y;
}
// number of lava cubes in the trench = inside points + outside points
// 1) inside points = pick's theorem (I = A - P/2 + 1)
// 2) outside points = P
// <=> number of lava cubes = A + P/2 + 1
area + perimeter / 2 + 1
}
#[derive(Debug)]
struct DigPlan {
instructions: Vec<Instruction>,
}
fn parse_a(input: &[String]) -> DigPlan {
let mut instructions = vec![];
for line in input {
let content = line.split_whitespace().collect::<Vec<_>>();
let instruction = Instruction {
direction: to_direction_a(content[0].chars().next().unwrap()),
len: content[1].parse::<usize>().unwrap(),
};
instructions.push(instruction);
}
DigPlan { instructions }
}
fn parse_b(input: &[String]) -> DigPlan {
let mut instructions = vec![];
for line in input {
let content = line.split_whitespace().collect::<Vec<_>>()[2]
.trim_start_matches("(#")
.trim_end_matches(')');
let len = usize::from_str_radix(&content[0..5], 16).unwrap();
let direction = to_direction_b(content.chars().nth(5).unwrap());
instructions.push(Instruction { direction, len })
}
DigPlan { instructions }
}
#[derive(Debug, Copy, Clone)]
struct Instruction {
direction: Cardinal,
len: usize,
}
fn to_direction_a(ch: char) -> Cardinal {
match ch {
'R' => Cardinal::East,
'D' => Cardinal::South,
'U' => Cardinal::North,
'L' => Cardinal::West,
_ => unreachable!(),
}
}
fn to_direction_b(ch: char) -> Cardinal {
match ch {
'0' => Cardinal::East,
'1' => Cardinal::South,
'2' => Cardinal::West,
'3' => Cardinal::North,
_ => unreachable!(),
}
}
#[cfg(test)]
mod test {
use aoc_lib::{self, answer::Answer, input, solution::Solution};
use super::Day18;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_18_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day18.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(62), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_18_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day18.part_b(&input);
assert_eq!(<i64 as Into<Answer>>::into(952408144115), answer);
}
}