Skip to content

Commit fe59a67

Browse files
committed
Working part one
1 parent d12273d commit fe59a67

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

2017/10/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Answers
2+
3+
| Part 1 | Part 2 |
4+
|---------|--------|
5+
| `38628` | ` ` |
6+
7+
## --- Day 10: Knot Hash ---
8+
9+
You come across some programs that are trying to implement a software emulation of a hash based on knot-tying. The hash these programs are implementing isn't very strong, but you decide to help them anyway. You make a mental note to remind the Elves later not to invent their own cryptographic functions.
10+
11+
This hash function simulates tying a knot in a circle of string with 256 marks on it. Based on the input to be hashed, the function repeatedly selects a span of string, brings the ends together, and gives the span a half-twist to reverse the order of the marks within it. After doing this many times, the order of the marks is used to build the resulting hash.
12+
13+
4--5 pinch 4 5 4 1
14+
/ \ 5,0,1 / \/ \ twist / \ / \
15+
3 0 --> 3 0 --> 3 X 0
16+
\ / \ /\ / \ / \ /
17+
2--1 2 1 2 5
18+
19+
20+
To achieve this, begin with a _list_ of numbers from `0` to `255`, a _current position_ which begins at `0` (the first element in the list), a _skip size_ (which starts at `0`), and a sequence of _lengths_ (your puzzle input). Then, for each length:
21+
22+
* _Reverse_ the order of that _length_ of elements in the _list_, starting with the element at the _current position_.
23+
* _Move_ the _current position_ forward by that _length_ plus the _skip size_.
24+
* _Increase_ the _skip size_ by one.
25+
26+
The _list_ is circular; if the _current position_ and the _length_ try to reverse elements beyond the end of the list, the operation reverses using as many extra elements as it needs from the front of the list. If the _current position_ moves past the end of the list, it wraps around to the front. _Lengths_ larger than the size of the _list_ are invalid.
27+
28+
Here's an example using a smaller list:
29+
30+
Suppose we instead only had a circular list containing five elements, `0, 1, 2, 3, 4`, and were given input lengths of `3, 4, 1, 5`.
31+
32+
* The list begins as `[0] 1 2 3 4` (where square brackets indicate the _current position_).
33+
* The first length, `3`, selects `([0] 1 2) 3 4` (where parentheses indicate the sublist to be reversed).
34+
* After reversing that section (`0 1 2` into `2 1 0`), we get `([2] 1 0) 3 4`.
35+
* Then, the _current position_ moves forward by the _length_, `3`, plus the _skip size_, 0: `2 1 0 [3] 4`. Finally, the _skip size_ increases to `1`.
36+
37+
* The second length, `4`, selects a section which wraps: `2 1) 0 ([3] 4`.
38+
* The sublist `3 4 2 1` is reversed to form `1 2 4 3`: `4 3) 0 ([1] 2`.
39+
* The _current position_ moves forward by the _length_ plus the _skip size_, a total of `5`, causing it not to move because it wraps around: `4 3 0 [1] 2`. The _skip size_ increases to `2`.
40+
41+
* The third length, `1`, selects a sublist of a single element, and so reversing it has no effect.
42+
* The _current position_ moves forward by the _length_ (`1`) plus the _skip size_ (`2`): `4 [3] 0 1 2`. The _skip size_ increases to `3`.
43+
44+
* The fourth length, `5`, selects every element starting with the second: `4) ([3] 0 1 2`. Reversing this sublist (`3 0 1 2 4` into `4 2 1 0 3`) produces: `3) ([4] 2 1 0`.
45+
* Finally, the _current position_ moves forward by `8`: `3 4 2 1 [0]`. The _skip size_ increases to `4`.
46+
47+
In this example, the first two numbers in the list end up being `3` and `4`; to check the process, you can multiply them together to produce `12`.
48+
49+
However, you should instead use the standard list size of `256` (with values `0` to `255`) and the sequence of _lengths_ in your puzzle input. Once this process is complete, _what is the result of multiplying the first two numbers in the list_?

2017/10/input.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const PART_TWO_TWIST_END_CONCAT = [17, 31, 73, 47, 23];
2+
3+
module.exports = {
4+
sampleInput: {
5+
size: 5,
6+
twists: [3, 4, 1, 5],
7+
},
8+
input: {
9+
size: 256,
10+
twists: [130, 126, 1, 11, 140, 2, 255, 207, 18, 254, 246, 164, 29, 104, 0, 224],
11+
},
12+
inputTwo: {
13+
size: 256,
14+
twists: `130,126,1,11,140,2,255,207,18,254,246,164,29,104,0,224`
15+
.split('')
16+
.map(v => v.charCodeAt())
17+
.concat(PART_TWO_TWIST_END_CONCAT),
18+
},
19+
};

2017/10/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
130,126,1,11,140,2,255,207,18,254,246,164,29,104,0,224

2017/10/knot.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { range } = require('lodash');
2+
const assert = require('assert')
3+
4+
class Knot {
5+
constructor(size, current_position = 0, skip = 0) {
6+
this.string = range(size);
7+
this.current_position = current_position;
8+
this.skip = skip;
9+
}
10+
11+
performTwists(twists) {
12+
twists.forEach(twist => {
13+
let indices = range(twist).map(v => (v + this.current_position) % this.string.length);
14+
let points = indices.map(i => this.string[i]);
15+
points.reverse();
16+
indices.forEach((string_i, root_i) => (this.string[string_i] = points[root_i]));
17+
this.current_position =
18+
(this.current_position + twist + this.skip++) % this.string.length;
19+
});
20+
21+
return this.string;
22+
}
23+
24+
computeAnswer() {
25+
assert.ok(this.string.length >= 2);
26+
return this.string[0] * this.string[1];
27+
}
28+
}
29+
30+
module.exports = Knot;

2017/10/part-one.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { input, sampleInput } = require('./input');
2+
const Knot = require('./knot');
3+
const assert = require('assert');
4+
5+
// Tests
6+
let sample_knot = new Knot(sampleInput.size);
7+
sample_knot.performTwists(sampleInput.twists);
8+
assert.deepStrictEqual(sample_knot.string, [3, 4, 2, 1, 0]);
9+
assert.strictEqual(sample_knot.computeAnswer(), 12);
10+
assert.strictEqual(sample_knot.current_position, 4);
11+
12+
let knot = new Knot(input.size);
13+
knot.performTwists(input.twists);
14+
15+
console.log(knot.computeAnswer());

2017/10/part-two.js

Whitespace-only changes.

0 commit comments

Comments
 (0)