generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.rs
119 lines (100 loc) · 3.24 KB
/
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
use nom::character::complete::{hex_digit1, newline, space1};
use nom::combinator::{map, opt};
use nom::multi::many1;
use nom::{
character::complete::{char, one_of, u32},
sequence::{delimited, preceded, terminated, tuple},
Finish, IResult,
};
use num::Complex;
advent_of_code::solution!(18);
type Direction = Complex<isize>;
const UP: Direction = Complex::<isize>::new(0, -1);
const RIGHT: Direction = Complex::<isize>::new(1, 0);
const DOWN: Direction = Complex::<isize>::new(0, 1);
const LEFT: Direction = Complex::<isize>::new(-1, 0);
struct DigMove {
direction: Direction,
delta: u32,
colour: u32,
}
fn dig_move(input: &str) -> IResult<&str, DigMove> {
let dir = map(one_of("URDL"), |d| match d {
'U' => UP,
'R' => RIGHT,
'D' => DOWN,
'L' => LEFT,
_ => panic!("Unexpected move: {d}"),
});
let rgb_hex = map(
delimited(char('('), preceded(char('#'), hex_digit1), char(')')),
|h| u32::from_str_radix(h, 16).unwrap(),
);
let (i, (direction, delta, colour)) =
tuple((dir, preceded(space1, u32), preceded(space1, rgb_hex)))(input)?;
Ok((
i,
DigMove {
direction,
delta,
colour,
},
))
}
fn parse_input(input: &str) -> IResult<&str, Vec<DigMove>> {
let (i, dig_moves) = many1(terminated(dig_move, opt(newline)))(input)?;
Ok((i, dig_moves))
}
fn get_inner_area(dig_moves: &[DigMove]) -> isize {
let (area, _) =
dig_moves
.iter()
.rev()
.fold((0, Complex::new(0, 0)), |(inner, prev), dig_move| {
let end_coords = prev - dig_move.direction.scale(dig_move.delta as isize);
let tmp = (prev.re * end_coords.im) - (end_coords.re * prev.im);
(inner + tmp, end_coords)
});
area.abs() / 2
}
pub fn part_one(input: &str) -> Option<isize> {
let (_, dig_moves) = parse_input(input).finish().unwrap();
let inner_area = get_inner_area(&dig_moves);
let perimeter: isize = dig_moves.iter().map(|t| t.delta).sum::<u32>() as isize;
let total_area = inner_area + perimeter / 2 + 1;
Some(total_area)
}
pub fn part_two(input: &str) -> Option<isize> {
let (_, dig_moves) = &mut parse_input(input).finish().unwrap();
dig_moves.iter_mut().for_each(|trench| {
trench.delta = trench.colour >> 4;
trench.direction = match trench.colour & 0b1111 {
0 => RIGHT,
1 => DOWN,
2 => LEFT,
3 => UP,
_ => panic!(
"Invalid direction decoded from colour: colour={:#08X}",
trench.colour
),
}
});
let inner_area = get_inner_area(dig_moves);
let perimeter: isize = dig_moves.iter().map(|t| t.delta).sum::<u32>() as isize;
let total_area = inner_area + perimeter / 2 + 1;
Some(total_area)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(62));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(952408144115));
}
}