Skip to content

Instantly share code, notes, and snippets.

@FelicitusNeko
Created December 18, 2019 01:36
Show Gist options
  • Select an option

  • Save FelicitusNeko/7b52edacfeb025378bfc2f24a2ae5e55 to your computer and use it in GitHub Desktop.

Select an option

Save FelicitusNeko/7b52edacfeb025378bfc2f24a2ae5e55 to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 16
‎‎​const FFT = (input, maxPhase) => {
let data = input.split('').map(i => parseInt(i));
for (let phase = 0; phase < maxPhase; phase++) {
let newData = [];
for (let X = 0; X < input.length; X++) {
let newVal = 0;
for (let Y = X; Y < input.length; Y++) {
switch (~~((Y + 1) / (X + 1)) % 4) {
case 1: newVal += data[Y]; break;
case 3: newVal -= data[Y]; break;
}
}
newData.push(Math.abs(newVal) % 10);
}
data = newData;
}
return data.join('');
}
[
['12345678', 4, '01029498'],
['80871224585914546619083218645595', 100, '24176176'],
['19617804207202209144916044189917', 100, '73745418'],
['69317163492948606335995924319873', 100, '52432133']
].forEach((i,x) => {
let result = FFT(i[0], i[1]);
console.assert(result.substr(0, i[2].length) === i[2], `Fail test #${x} (got ${result}, expected starting with ${i[2]})`);
});
let bigData = '59709511599794439805414014219880358445064269099345553494818286560304063399998657801629526113732466767578373307474609375929817361595469200826872565688108197109235040815426214109531925822745223338550232315662686923864318114370485155264844201080947518854684797571383091421294624331652208294087891792537136754322020911070917298783639755047408644387571604201164859259810557018398847239752708232169701196560341721916475238073458804201344527868552819678854931434638430059601039507016639454054034562680193879342212848230089775870308946301489595646123293699890239353150457214490749319019572887046296522891429720825181513685763060659768372996371503017206185697';
console.log(FFT(bigData, 100).substr(0, 8));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment