Published
Edited
Dec 12, 2020
Insert cell
Insert cell
Insert cell
parse = input => input.split('\n').map(n => parseInt(n))
Insert cell
parse(testInput)
Insert cell
Insert cell
part1 = input =>
parse(input)
.sort((a, b) => a - b)
.reduce((diffs, val, idx, arr) => {
if (idx === 0) return [val];
diffs.push(val - arr[idx - 1]);
return diffs;
}, [])
.reduce(
(count, diff) => {
const key = diff == 1 ? 'ones' : 'threes';
count[key]++;
return count;
},
{ ones: 0, threes: 1 }
)
Insert cell
part1(testInput)
Insert cell
Insert cell
part1(testInput2)
Insert cell
Insert cell
{
const { ones, threes } = part1(input);
return ones * threes;
}
Insert cell
Insert cell
_ = require('lodash')
Insert cell
removeIndex = (arr, idx) => {
return arr.slice(0, idx).concat(arr.slice(idx + 1));
}
Insert cell
removeIndex( [0,1,2,3,4,5], 2 )
Insert cell
removeIndex( [0,1,2,3,4,5], 0 )
Insert cell
removeIndex( [0,1,2,3,4,5], 5 )
Insert cell
validAdapter = (current, adapter) => adapter - current <= 3 && adapter > current
Insert cell
validAdapter(0, 3)
Insert cell
{
let calls = 0;
// wrapped in a block for Observable's weird global const namespace
const stackAdapters = _.memoize(
(target, adapterBag, current = 0) => {
let sum = 0;

adapterBag.every((adapter, idx, arr) => {
// Speed things up by stopping when we get to an adapter above our current
if (!validAdapter(current, adapter)) return adapter <= current + 3;

// If we have hit our target joltage then we have found a valid combination... but not unique?
if (validAdapter(adapter, target)) {
// throw JSON.stringify([target, current, adapter, arr]);
sum++;
} else {
if (calls++ > 100000) throw "Too much recursion! " + sum;
const newBag = removeIndex(adapterBag, idx);
sum += stackAdapters(target, newBag, adapter);
}

return true;
});

return sum;
},

(target, adapterBag, current = 0) => current
);
const parsedTest = parse(input).sort((a, b) => a - b);
const max = Math.max.apply(null, parsedTest);
return stackAdapters(max + 3, parsedTest);
}
Insert cell
parse(testInput)
.map(n => [n, validAdapter(19, n)])
.filter(p => p[1])
Insert cell
[ 1, 4, ]
Insert cell
{
// 57127475625984 is too high
// const parsedTest = parse(input).sort((a, b) => a - b);
// const max = Math.max.apply(null, parsedTest);
// return stackAdapters(max + 3, parsedTest);
}
Insert cell
function validPermutations(sortedList) {
if (sortedList[0] + 3 >= sortedList[sortedList.length - 1]) return 1; // if we got to a valid adapter
let compatibleSequences = 0;
const adapter = sortedList[0];
for (let i = 1; sortedList[i] <= adapter + 3; i++) {
compatibleSequences += validPermutations(sortedList.slice(i));
}
return compatibleSequences;
}
Insert cell
part2 = input => validPermutations(parse(input).sort((a, b) => a - b))
// .map((adapter, idx, arr) => {
// let compatibleAdapters = 0;
// for (let i = idx + 1; arr[i] <= adapter + 3; i++) {
// compatibleAdapters++;
// }
// return [adapter, compatibleAdapters];
// // return compatibleAdapters;
// })
// .reduce((product, branches, idx, arr) => {
// if (branches === 0) return product;
// return product * branches;
// }, 1)
Insert cell
part2(testInput)
Insert cell
part2(testInput2)
Insert cell
// part2(input)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more