Skip to content

Commit 2e2d81c

Browse files
committed
Day 25
1 parent d5a5c6c commit 2e2d81c

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

2017/aoc25.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
{
4+
const op = (value, dir, state) => (tape, i) => {
5+
tape[i] = value;
6+
return [state, i + dir];
7+
};
8+
9+
const execute = (rules, state, steps, i = 0) => {
10+
const tape = {};
11+
while (steps--) [state, i] = rules[`${state}${tape[i] || 0}`](tape, i);
12+
return Object.values(tape).reduce((a, b) => a + b);
13+
};
14+
15+
const input = document.body.textContent.trim().split('\n\n');
16+
const [, state0, steps] = input[0].match(/state ([A-Z])\.\n.+?(\d+)/);
17+
const reOp = /[A-Z](?=[.:])|[01]|left|right/;
18+
const rules = input.slice(1).map(p => p.split('\n').map(s => s.match(reOp)[0]))
19+
.reduce((acc, [st, , v0, d0, st0, , v1, d1, st1]) => ({
20+
...acc,
21+
[`${st}0`]: op(+v0, d0 === 'left' ? -1 : 1, st0),
22+
[`${st}1`]: op(+v1, d1 === 'left' ? -1 : 1, st1),
23+
}), {});
24+
25+
console.log(execute(rules, state0, +steps));
26+
}

scraps.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ browser console on the /input page (F12 → Console):
2121
const getGrid = n => new Array(n).fill().map(() => new Array(n).fill(0));
2222
const getGrid2 = n => [...new Array(n)].map(() => new Array(n).fill(0));
2323

24+
// Timed execution
25+
(f => { console.time('main'); f(); console.timeEnd('main'); })(() => { // eslint-disable-line
26+
});
27+
28+
// Timed execution for Promises
29+
const timed = f => () => { console.time('main'); f(); console.timeEnd('main'); }; // eslint-disable-line
30+
2431
// Generate and draw infinite grid from an odd-sized center segment
2532
const halfWidth = Math.floor(input[0].length / 2);
2633
const key = (i, j) => `${i},${j}`;
@@ -47,13 +54,6 @@ browser console on the /input page (F12 → Console):
4754
return a.map(row => row.join('').replace(/\] | {2}| \[/g, s => s.trim() || ' ')).join('\n');
4855
};
4956

50-
// Timed execution
51-
(f => { console.time('main'); f(); console.timeEnd('main'); })(() => { // eslint-disable-line
52-
});
53-
54-
// Timed execution for Promises
55-
const timed = f => () => { console.time('main'); f(); console.timeEnd('main'); }; // eslint-disable-line
56-
5757
// Permutation generator and minmax tracker (separate)
5858
const permute = function* (arr, used = []) {
5959
if (arr.length === 0) yield [...used];

0 commit comments

Comments
 (0)