Skip to content

Instantly share code, notes, and snippets.

@splintor
Created September 25, 2018 04:04
Show Gist options
  • Save splintor/6637a0f0c8482116481ebc37706d5fe0 to your computer and use it in GitHub Desktop.
Save splintor/6637a0f0c8482116481ebc37706d5fe0 to your computer and use it in GitHub Desktop.
My adventofcode solutions
function machine(s) {
const steps = s.split(/[\r\n]+/).map(l => Number(l));
let counter = 0, index = 0;
while (index < steps.length) {
const step = steps[index];
++steps[index];
index += step;
++counter;
}
return counter;
}
function machine2(s) {
const steps = s.split(/[\r\n]+/).map(l => Number(l));
let counter = 0, index = 0;
while (index < steps.length) {
const step = steps[index];
if (step >= 3) {
--steps[index];
} else {
++steps[index];
}
index += step;
++counter;
}
console.log(steps);
return counter;
}
function unique(s) {
const hasDuplicate = c => {
const dic = {};
return c.reduce((acc, w) => acc || w in dic || (dic[w] = false), false);
};
return s.split(/[\r\n]+/)
.map(l => l.split(/\W+/))
.reduce((acc, c) => acc + (hasDuplicate(c) ? 0 : 1), 0);
}
function anagram(s) {
const sorted = s => s.split('').sort().join('');
const hasDuplicate = c => {
const dic = {};
return c.reduce((acc, w) => acc || sorted(w) in dic || (dic[sorted(w)] = false), false);
};
return s.split(/[\r\n]+/)
.map(l => l.split(/\W+/))
.reduce((acc, c) => acc + (hasDuplicate(c) ? 0 : 1), 0);
}
function checksum1(s) {
return s.split(/[\r\n]+/).reduce((sum, c) => sum += Math.max(...c.split(/\W+/)) - Math.min(...c.split(/\W+/)), 0);
}
function checksum1(s) {
return s.split(/[\r\n]+/)
.map(l => l.split(/\W+/))
.reduce((acc, c) => acc += Math.max(...c) - Math.min(...c), 0);
}
function findQuota(line, itemToDivide, itemToDivideIndex) {
return line.reduce((acc, item, index) => itemToDivideIndex !== index && itemToDivide % item === 0 ? itemToDivide / item : acc, 0);
}
function checksum2(s) {
function findQuota(line, itemToDivide, itemToDivideIndex) {
return line.reduce((acc, item, index) =>
itemToDivideIndex !== index && itemToDivide % item === 0
? itemToDivide / item : acc, 0);
}
function lineQuota(line) {
return line.reduce((lineAcc, item, index) =>
lineAcc || findQuota(line, item, index), 0);
}
const parsedLines = s.split(/[\r\n]+/).map(l => l.split(/\W+/));
return parsedLines.reduce((acc, line) => acc + lineQuota(line), 0);
}
function sum(n) {
const maze = {};
const mazeAt = (x, y) => maze[`x${x}y${y}`] || 0;
const setMazeAt = (x, y, value) => maze[`x${x}y${y}`] = value;
let x = 0, y = 0;
setMazeAt(x, y, 1);
while (true) {
if (mazeAt(x, y - 1) && !mazeAt(x - 1, y)) { // has below, but not left
--x; // move left
} else if (mazeAt(x - 1, y) && !mazeAt(x, y + 1)) { // has left, but not above
++y; // move up
} else if (mazeAt(x + 1, y) && !mazeAt(x, y - 1)) { // has right, but now below
--y; // move down
} else {
++x; // else, move right
}
const sum = mazeAt(x + 1, y) + mazeAt(x + 1, y + 1) + mazeAt(x + 1, y - 1) +
mazeAt(x - 1, y) + mazeAt(x - 1, y + 1) + mazeAt(x - 1, y - 1) +
mazeAt(x, y + 1) + mazeAt(x, y - 1);
if (sum > n) {
return sum;
} else {
setMazeAt(x, y, sum);
}
}
}
sum(361527)
363010
function distance(n) { // n > 1
let loop = 1;
let counter = 1;
while (counter + loop * 8 < n) {
counter += loop * 8;
++loop;
}
while (counter + loop * 2 < n) {
counter += loop * 2;
}
return loop + Math.abs(n - (counter + loop));
}
distance(361527)
36
35
34
33
32
31
17
16
15
14
13
30
18
5
4
3
12
29
19
6
1
2
11
28
20
7
8
9
10
27
21
22
23
24
25
26
...
1
9
25
49
81
121
1
3
6
10
15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment