-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.rs
65 lines (58 loc) · 2.03 KB
/
day19.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
use std::collections::HashMap;
use std::fs;
pub(crate) fn day19() {
let input = fs::read_to_string("input/2017/day19/input.txt").unwrap();
let (part_a, part_b) = route(input.as_str());
println!("{}", part_a);
println!("{}", part_b);
}
fn route(input: &str) -> (String, i32) {
let map: HashMap<(i32, i32), char> = input.lines().enumerate()
.flat_map(|(x, line)|
line.chars().enumerate()
.filter(|(_, c)| *c != ' ')
.map(|(y, c)| ((x as i32, y as i32), c))
.collect::<Vec<((i32, i32), char)>>()
).collect();
let mut pos: (i32, i32) = *map.keys().find(|(x, _)| *x == 0).unwrap();
let mut ans = String::new();
let mut steps = 0;
let mut dir = (1, 0);
loop {
match map.get(&pos) {
Some('|') | Some('-') => {} // Continue
Some('+') => { // Find new dir
let lookup = if dir.0 != 0 { [(0, 1), (0, -1)] } else { [(1, 0), (-1, 0)] };
let new_dir = lookup.iter()
.find(|(dx, dy)| map.get(&(pos.0 + dx, pos.1 + dy)).is_some());
match new_dir {
Some(n) => { dir = *n }
None => { panic!("No new dir found at {:?}", pos) }
}
}
Some(c) => { ans.push(*c); }
None => { return (ans, steps); } // Nowhere else to go
}
pos = (pos.0 + dir.0, pos.1 + dir.1);
steps += 1;
}
}
#[cfg(test)]
mod day19_tests {
use std::fs;
use crate::y2017::day19::route;
#[test]
fn test_works() {
let input = fs::read_to_string("input/2017/day19/test.txt").unwrap();
let (part_a, part_b) = route(input.as_str());
assert_eq!("ABCDEF", part_a);
assert_eq!(38, part_b);
}
#[test]
fn input_works() {
let input = fs::read_to_string("input/2017/day19/input.txt").unwrap();
let (part_a, part_b) = route(input.as_str());
assert_eq!("XYFDJNRCQA", part_a);
assert_eq!(17450, part_b);
}
}