Skip to content

Commit b0e48e6

Browse files
committed
Year 2016 Day 1
1 parent 71a6098 commit b0e48e6

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pie
7373
* [2021](#2021) (11 ms)
7474
* [2020](#2020) (286 ms)
7575
* [2019](#2019) (21 ms)
76+
* [2016](#2016) (in progress)
7677
* [2015](#2015) (93 ms)
7778

7879
## 2022
@@ -238,6 +239,12 @@ pie
238239
| 24 | [Planet of Discord](https://adventofcode.com/2019/day/24) | [Source](src/year2019/day24.rs) | 139 |
239240
| 25 | [Cryostasis](https://adventofcode.com/2019/day/25) | [Source](src/year2019/day25.rs) | 2721 |
240241

242+
## 2016
243+
244+
| Day | Problem | Solution | Benchmark (μs) |
245+
| --- | --- | --- | --: |
246+
| 1 | [No Time for a Taxicab](https://adventofcode.com/2016/day/1) | [Source](src/year2016/day01.rs) | 3 |
247+
241248
## 2015
242249

243250
```mermaid

benches/benchmark.rs

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ mod year2015 {
6363
benchmark!(year2015, day25);
6464
}
6565

66+
mod year2016 {
67+
benchmark!(year2016, day01);
68+
}
69+
6670
mod year2019 {
6771
benchmark!(year2019, day01);
6872
benchmark!(year2019, day02);

input/year2016/day01.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R5, R4, R2, L3, R1, R1, L4, L5, R3, L1, L1, R4, L2, R1, R4, R4, L2, L2, R4, L4, R1, R3, L3, L1, L2, R1, R5, L5, L1, L1, R3, R5, L1, R4, L5, R5, R1, L185, R4, L1, R51, R3, L2, R78, R1, L4, R188, R1, L5, R5, R2, R3, L5, R3, R4, L1, R2, R2, L4, L4, L5, R5, R4, L4, R2, L5, R2, L1, L4, R4, L4, R2, L3, L4, R2, L3, R3, R2, L2, L3, R4, R3, R1, L4, L2, L5, R4, R4, L1, R1, L5, L1, R3, R1, L2, R1, R1, R3, L4, L1, L3, R2, R4, R2, L2, R1, L5, R3, L3, R3, L1, R4, L3, L3, R4, L2, L1, L3, R2, R3, L2, L1, R4, L3, L5, L2, L4, R1, L4, L4, R3, R5, L4, L1, L1, R4, L2, R5, R1, R1, R2, R1, R5, L1, L3, L5, R2

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ pub mod year2015 {
187187
pub mod day25;
188188
}
189189

190+
/// # Defeat the Easter Bunny to save Christmas.
191+
pub mod year2016 {
192+
pub mod day01;
193+
}
194+
190195
/// # Rescue Santa from deep space with a solar system adventure.
191196
pub mod year2019 {
192197
pub mod day01;

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ fn all_solutions() -> Vec<Solution> {
101101
solution!(year2015, day23),
102102
solution!(year2015, day24),
103103
solution!(year2015, day25),
104+
// 2016
105+
solution!(year2016, day01),
104106
// 2019
105107
solution!(year2019, day01),
106108
solution!(year2019, day02),

src/year2016/day01.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! # No Time for a Taxicab
2+
//!
3+
//! The solution is short as it leverages three utility classes, [`hash`] for speedy sets,
4+
//! [`parse`] for extracting integers from surrounding text and [`point`] for two dimensional
5+
//! rotations and translations.
6+
//!
7+
//! [`hash`]: crate::util::hash
8+
//! [`parse`]: crate::util::parse
9+
//! [`point`]: crate::util::point
10+
use crate::util::hash::*;
11+
use crate::util::parse::*;
12+
use crate::util::point::*;
13+
14+
type Pair = (u8, i32);
15+
16+
pub fn parse(input: &str) -> Vec<Pair> {
17+
let first = input.bytes().filter(u8::is_ascii_uppercase);
18+
let second = input.iter_signed();
19+
first.zip(second).collect()
20+
}
21+
22+
pub fn part1(input: &[Pair]) -> i32 {
23+
let mut position = ORIGIN;
24+
let mut direction = UP;
25+
26+
for &(turn, amount) in input {
27+
direction =
28+
if turn == b'L' { direction.counter_clockwise() } else { direction.clockwise() };
29+
position += direction * amount;
30+
}
31+
32+
position.manhattan(ORIGIN)
33+
}
34+
35+
pub fn part2(input: &[Pair]) -> i32 {
36+
let mut position = ORIGIN;
37+
let mut direction = UP;
38+
let mut visited = FastSet::with_capacity(1000);
39+
40+
for &(turn, amount) in input {
41+
direction =
42+
if turn == b'L' { direction.counter_clockwise() } else { direction.clockwise() };
43+
44+
for _ in 0..amount {
45+
position += direction;
46+
if !visited.insert(position) {
47+
return position.manhattan(ORIGIN);
48+
}
49+
}
50+
}
51+
52+
unreachable!()
53+
}

tests/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ mod year2015 {
5656
mod day25_test;
5757
}
5858

59+
mod year2016 {
60+
mod day01_test;
61+
}
62+
5963
mod year2019 {
6064
mod day01_test;
6165
mod day02_test;

tests/year2016/day01_test.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use aoc::year2016::day01::*;
2+
3+
const FIRST_EXAMPLE: &str = "R5, L5, R5, R3";
4+
const SECOND_EXAMPLE: &str = "R8, R4, R4, R8";
5+
6+
#[test]
7+
fn part1_test() {
8+
let input = parse(FIRST_EXAMPLE);
9+
assert_eq!(part1(&input), 12);
10+
}
11+
12+
#[test]
13+
fn part2_test() {
14+
let input = parse(SECOND_EXAMPLE);
15+
assert_eq!(part2(&input), 4);
16+
}

0 commit comments

Comments
 (0)