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
#[cfg(feature = "visualization")]
use super::day12_renderer::Renderer;
use crate::input::Input;
pub const SHIP_POSITION_ENTITY_IDX: usize = 0;
pub const SHIP_DIRECTION_ENTITY_IDX: usize = 1;
pub const WAYPOINT_ENTITY_IDX: usize = 2;
pub fn solve(input: &mut Input) -> Result<i32, String> {
let ship_position = (0, 0);
let ship_direction = (1, 0);
let waypoint = (10, -1);
let mut entities = [ship_position, ship_direction, waypoint];
let moved_entity_idx = input.part_values(SHIP_POSITION_ENTITY_IDX, WAYPOINT_ENTITY_IDX);
let rotated_entity_idx = input.part_values(SHIP_DIRECTION_ENTITY_IDX, WAYPOINT_ENTITY_IDX);
let rotation_matrices: [(i32, i32, i32, i32); 3] = [
(0, -1, 1, 0),
(-1, 0, 0, -1),
(0, 1, -1, 0),
];
#[cfg(feature = "visualization")]
let is_part_one = input.is_part_one();
#[cfg(feature = "visualization")]
let mut renderer = Renderer::new(&mut input.painter);
#[cfg(feature = "visualization")]
renderer.save_entities(entities);
for (line_idx, line) in input.text.lines().enumerate() {
let on_error = || {
format!("Line {}: Invalid input - expected N|S|E|W followd by i32, or R|L followed by 90|180|270", line_idx + 1)
};
if line.len() < 2 {
return Err(on_error());
}
let numeric_parameter = line[1..].parse::<i32>().map_err(|_| on_error())?;
let operation = line.as_bytes()[0];
match operation {
b'N' => {
entities[moved_entity_idx].1 -= numeric_parameter;
}
b'S' => {
entities[moved_entity_idx].1 += numeric_parameter;
}
b'E' => {
entities[moved_entity_idx].0 += numeric_parameter;
}
b'W' => {
entities[moved_entity_idx].0 -= numeric_parameter;
}
b'R' | b'L' => {
if !matches!(numeric_parameter, 90 | 180 | 270) {
return Err(on_error());
}
let rotation_idx = (if operation == b'L' {
360 - numeric_parameter
} else {
numeric_parameter
} / 90)
- 1;
let rotation = rotation_matrices[rotation_idx as usize];
let rotated = entities[rotated_entity_idx];
entities[rotated_entity_idx] = (
rotated.0 * rotation.0 + rotated.1 * rotation.1,
rotated.0 * rotation.2 + rotated.1 * rotation.3,
);
}
b'F' => {
let direction = entities[rotated_entity_idx];
let position = entities[SHIP_POSITION_ENTITY_IDX];
entities[SHIP_POSITION_ENTITY_IDX] = (
position.0 + numeric_parameter * direction.0,
position.1 + numeric_parameter * direction.1,
);
}
_ => {
return Err(on_error());
}
}
#[cfg(feature = "visualization")]
renderer.save_entities(entities);
}
#[cfg(feature = "visualization")]
renderer.render(is_part_one);
let position = entities[SHIP_POSITION_ENTITY_IDX];
Ok(position.0.abs() + position.1.abs())
}
#[test]
pub fn tests() {
use crate::{test_part_one, test_part_two};
let example = "F10\nN3\nF7\nR90\nF11";
test_part_one!(example => 25);
test_part_two!(example => 286);
let real_input = include_str!("day12_input.txt");
test_part_one!(real_input => 1007);
test_part_two!(real_input => 41212);
}