Skip to content

Commit 377f31f

Browse files
committed
Year 2017 Day 10
1 parent 57db72f commit 377f31f

File tree

8 files changed

+76
-0
lines changed

8 files changed

+76
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ pie
249249
| 7 | [Recursive Circus](https://adventofcode.com/2017/day/7) | [Source](src/year2017/day07.rs) | 85 |
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 |
252+
| 10 | [Knot Hash](https://adventofcode.com/2017/day/10) | [Source](src/year2017/day10.rs) | 112 |
252253

253254
## 2016
254255

benches/benchmark.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ mod year2017 {
101101
benchmark!(year2017, day07);
102102
benchmark!(year2017, day08);
103103
benchmark!(year2017, day09);
104+
benchmark!(year2017, day10);
104105
}
105106

106107
mod year2019 {

input/year2017/day10.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14,58,0,116,179,16,1,104,2,254,167,86,255,55,122,244

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub mod year2017 {
9090
pub mod day07;
9191
pub mod day08;
9292
pub mod day09;
93+
pub mod day10;
9394
}
9495

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

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ fn year2017() -> Vec<Solution> {
151151
solution!(year2017, day07),
152152
solution!(year2017, day08),
153153
solution!(year2017, day09),
154+
solution!(year2017, day10),
154155
]
155156
}
156157

src/year2017/day10.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! # Knot Hash
2+
//!
3+
//! Instead of reversing elements from the starting position then trying to handle wrap around,
4+
//! its easier use [`rotate_left`] to rotate the array by the same amount so that the starting
5+
//! position is always zero, then take advantage of the built in [`reverse`] method.
6+
//!
7+
//! [`rotate_left`]: slice::rotate_left
8+
//! [`reverse`]: slice::reverse
9+
use crate::util::parse::*;
10+
use std::fmt::Write;
11+
12+
pub fn parse(input: &str) -> &str {
13+
input
14+
}
15+
16+
pub fn part1(input: &str) -> u32 {
17+
let lengths: Vec<_> = input.iter_unsigned().collect();
18+
let knot = hash(&lengths, 1);
19+
knot.iter().take(2).product()
20+
}
21+
22+
pub fn part2(input: &str) -> String {
23+
let mut lengths: Vec<_> = input.trim().bytes().map(|b| b as usize).collect();
24+
lengths.extend([17, 31, 73, 47, 23]);
25+
26+
let knot = hash(&lengths, 64);
27+
let mut result = String::new();
28+
29+
for chunk in knot.chunks_exact(16) {
30+
let reduced = chunk.iter().fold(0, |acc, n| acc ^ n);
31+
let _ = write!(&mut result, "{reduced:02x}");
32+
}
33+
34+
result
35+
}
36+
37+
fn hash(lengths: &[usize], rounds: usize) -> Vec<u32> {
38+
let mut knot: Vec<_> = (0..256).collect();
39+
let mut position = 0;
40+
let mut skip = 0;
41+
42+
for _ in 0..rounds {
43+
for &length in lengths {
44+
let next = length + skip;
45+
knot[0..length].reverse();
46+
knot.rotate_left(next % 256);
47+
position += next;
48+
skip += 1;
49+
}
50+
}
51+
52+
// Rotate the array the other direction so that the original starting position is restored.
53+
knot.rotate_right(position % 256);
54+
knot
55+
}

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mod year2017 {
9494
mod day07_test;
9595
mod day08_test;
9696
mod day09_test;
97+
mod day10_test;
9798
}
9899

99100
mod year2019 {

tests/year2017/day10_test.rs

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

0 commit comments

Comments
 (0)