Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08d3d99

Browse filesBrowse files
2017, day 19
1 parent 8a9f6cf commit 08d3d99
Copy full SHA for 08d3d99

File tree

3 files changed

+70
-0
lines changed
Filter options

3 files changed

+70
-0
lines changed

‎src/main.rs

Copy file name to clipboardExpand all lines: src/main.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fn main() {
8787
(2017, 16) => year_2017::day16::run(&inputs::read_first_line(year, day)),
8888
(2017, 17) => year_2017::day17::run(354),
8989
(2017, 18) => year_2017::day18::run(inputs::read(year, day)),
90+
(2017, 19) => year_2017::day19::run(inputs::read(year, day)),
9091

9192
// Other
9293
(_, _) => panic!("Not implemented :("),

‎src/year_2017/day19.rs

Copy file name to clipboard
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::collections::HashMap;
2+
3+
pub fn run(input: Vec<String>) {
4+
let part1 = solve(input.clone()).0;
5+
println!("part 1: {}", part1);
6+
assert_eq!(part1, "RYLONKEWB");
7+
8+
let part2 = solve(input.clone()).1;
9+
println!("part 2: {}", part2);
10+
assert_eq!(part2, 16016);
11+
}
12+
13+
fn solve(input: Vec<String>) -> (String, u64) {
14+
let mut grid = HashMap::new();
15+
16+
// Parse the input
17+
for y in 0..input.len() {
18+
for x in 0..input[y].len() {
19+
grid.insert((x as i64, y as i64), input[y].chars().nth(x).unwrap());
20+
}
21+
}
22+
23+
// find the starting position
24+
let mut pos_y: i64 = 0;
25+
let mut pos_x: i64 = input[0].chars().enumerate().find(|&(_, c)| c == '|').unwrap().0 as i64;
26+
let mut dir = 0;
27+
let dirs = [(0, 1), (-1, 0), (0, -1), (1, 0)];
28+
29+
// move around the grid until we are done
30+
let mut letters = String::new();
31+
let mut steps = 0;
32+
loop {
33+
let c = *grid.entry((pos_x, pos_y)).or_insert(' ');
34+
match c {
35+
'A'...'Z' => {
36+
letters.push(c);
37+
}
38+
'|' | '-' => {}
39+
'+' => {
40+
// we either need to go dir+1 or dir-1
41+
let c1 = *grid.entry((pos_x + dirs[(dir + 1) % 4].0, pos_y + dirs[(dir + 1) % 4].1))
42+
.or_insert(' ');
43+
let c2 = *grid.entry((pos_x + dirs[(dir + 3) % 4].0, pos_y + dirs[(dir + 3) % 4].1))
44+
.or_insert(' ');
45+
match (c1, c2) {
46+
(' ', ' ') => panic!("both directions result in space"),
47+
(_, ' ') => {
48+
dir = (dir + 1) % 4;
49+
}
50+
(' ', _) => {
51+
dir = (dir + 3) % 4;
52+
}
53+
_ => panic!("neither direction results in a space"),
54+
}
55+
}
56+
' ' => {
57+
// we are done
58+
break;
59+
}
60+
_ => panic!("unknown char: {}", c as u8),
61+
}
62+
pos_x += dirs[dir].0;
63+
pos_y += dirs[dir].1;
64+
steps += 1;
65+
}
66+
67+
return (letters, steps);
68+
}

‎src/year_2017/mod.rs

Copy file name to clipboardExpand all lines: src/year_2017/mod.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pub mod day15;
1616
pub mod day16;
1717
pub mod day17;
1818
pub mod day18;
19+
pub mod day19;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.