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
use crate::input::Input;
use std::collections::HashMap;
pub fn solve(input: &mut Input) -> Result<String, String> {
let mut map = HashMap::new();
let mut entrance_at_top_x = None;
for (line_idx, line) in input.text.lines().enumerate() {
for (char_idx, char) in line.chars().enumerate() {
if line_idx == 0 && char == '|' {
entrance_at_top_x = Some(char_idx);
}
map.insert((char_idx as i16, line_idx as i16), char as u8);
}
}
let mut current_position = match entrance_at_top_x {
None => {
return Err("No | at top row".to_string());
}
Some(x) => (x as i16, 0_i16),
};
let mut direction = (0, 1);
let mut seen_letters = String::new();
let mut packet_steps = 0;
'outer_loop: loop {
packet_steps += 1;
if packet_steps >= 100_000 {
return Err(format!("Aborting after {} steps", packet_steps));
}
current_position = (
current_position.0 + direction.0,
current_position.1 + direction.1,
);
match map.get(¤t_position) {
Some(b'+') => {
for &new_direction in &[(0_i16, 1_i16), (1, 0), (-1, 0), (0, -1)] {
if new_direction == (-direction.0, -direction.1) {
} else {
let adjacent = (
current_position.0 + new_direction.0,
current_position.1 + new_direction.1,
);
if let Some(&c) = map.get(&adjacent) {
if c == b'|' || c == b'-' {
direction = new_direction;
continue 'outer_loop;
}
}
}
}
}
Some(c) if (b'A'..=b'Z').contains(c) => {
seen_letters.push(*c as char);
}
Some(&c) if c == b'-' || c == b'|' => {
}
_ => {
break Ok(input.part_values(seen_letters, packet_steps.to_string()));
}
}
}
}
#[test]
pub fn tests() {
use crate::{test_part_one, test_part_two};
let real_input = include_str!("day19_input.txt");
test_part_one!(real_input => "KGPTMEJVS".to_string());
test_part_two!(real_input => "16328".to_string());
}