Skip to content

Commit 5c5ddaf

Browse files
committed
Year 2018 Day 11
1 parent 377f31f commit 5c5ddaf

File tree

8 files changed

+77
-0
lines changed

8 files changed

+77
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ pie
250250
| 8 | [I Heard You Like Registers](https://adventofcode.com/2017/day/8) | [Source](src/year2017/day08.rs) | 46 |
251251
| 9 | [Stream Processing](https://adventofcode.com/2017/day/9) | [Source](src/year2017/day09.rs) | 13 |
252252
| 10 | [Knot Hash](https://adventofcode.com/2017/day/10) | [Source](src/year2017/day10.rs) | 112 |
253+
| 11 | [Hex Ed](https://adventofcode.com/2017/day/11) | [Source](src/year2017/day11.rs) | 20 |
253254

254255
## 2016
255256

benches/benchmark.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ mod year2017 {
102102
benchmark!(year2017, day08);
103103
benchmark!(year2017, day09);
104104
benchmark!(year2017, day10);
105+
benchmark!(year2017, day11);
105106
}
106107

107108
mod year2019 {

input/year2017/day11.txt

+1
Large diffs are not rendered by default.

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub mod year2017 {
9191
pub mod day08;
9292
pub mod day09;
9393
pub mod day10;
94+
pub mod day11;
9495
}
9596

9697
/// # Rescue Santa from deep space with a solar system adventure.

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ fn year2017() -> Vec<Solution> {
152152
solution!(year2017, day08),
153153
solution!(year2017, day09),
154154
solution!(year2017, day10),
155+
solution!(year2017, day11),
155156
]
156157
}
157158

src/year2017/day11.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! # Hex Ed
2+
//!
3+
//! Hex grid parsing and navigation uses
4+
//! [Axial Coordinates](https://www.redblobgames.com/grids/hexagons/#coordinates-cube)
5+
//! exactly as described in the excellent [Red Blob Games](https://www.redblobgames.com/) blog.
6+
//!
7+
//! As mentioned in the blog, the Manhattan distance to the center has the formula
8+
//! `(q.abs() + r.abs() + s.abs()) / 2`
9+
type Input = (i32, i32);
10+
11+
pub fn parse(input: &str) -> Input {
12+
let mut iter = input.bytes();
13+
let mut q: i32 = 0;
14+
let mut r: i32 = 0;
15+
let mut part_one = 0;
16+
let mut part_two = 0;
17+
18+
while let Some(first) = iter.next() {
19+
match first {
20+
b'n' => match iter.next().unwrap_or(0) {
21+
b'e' => {
22+
q += 1;
23+
r -= 1;
24+
}
25+
b'w' => q -= 1,
26+
_ => r -= 1,
27+
},
28+
b's' => match iter.next().unwrap_or(0) {
29+
b'e' => q += 1,
30+
b'w' => {
31+
q -= 1;
32+
r += 1;
33+
}
34+
_ => r += 1,
35+
},
36+
_ => (),
37+
}
38+
39+
// q + r + s = 0, so we can always determine s given the other two.
40+
let s = q + r;
41+
// Manhattan distance to the center.
42+
part_one = (q.abs() + r.abs() + s.abs()) / 2;
43+
// Keep track of furthest distance.
44+
part_two = part_two.max(part_one);
45+
}
46+
47+
(part_one, part_two)
48+
}
49+
50+
pub fn part1(input: &Input) -> i32 {
51+
input.0
52+
}
53+
54+
pub fn part2(input: &Input) -> i32 {
55+
input.1
56+
}

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ mod year2017 {
9595
mod day08_test;
9696
mod day09_test;
9797
mod day10_test;
98+
mod day11_test;
9899
}
99100

100101
mod year2019 {

tests/year2017/day11_test.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use aoc::year2017::day11::*;
2+
3+
const EXAMPLE: &str = "se,sw,se,sw,sw,s,n";
4+
5+
#[test]
6+
fn part1_test() {
7+
let input = parse(EXAMPLE);
8+
assert_eq!(part1(&input), 3);
9+
}
10+
11+
#[test]
12+
fn part2_test() {
13+
let input = parse(EXAMPLE);
14+
assert_eq!(part2(&input), 4);
15+
}

0 commit comments

Comments
 (0)