Day 1 of AdventOfCode
This commit is contained in:
21
AdventOfCode2018/1stDay/challenge2/challenge.txt
Normal file
21
AdventOfCode2018/1stDay/challenge2/challenge.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
--- Part Two ---
|
||||
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.
|
||||
|
||||
For example, using the same list of changes above, the device would loop as follows:
|
||||
|
||||
Current frequency 0, change of +1; resulting frequency 1.
|
||||
Current frequency 1, change of -2; resulting frequency -1.
|
||||
Current frequency -1, change of +3; resulting frequency 2.
|
||||
Current frequency 2, change of +1; resulting frequency 3.
|
||||
(At this point, the device continues from the start of the list.)
|
||||
Current frequency 3, change of +1; resulting frequency 4.
|
||||
Current frequency 4, change of -2; resulting frequency 2, which has already been seen.
|
||||
In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
|
||||
|
||||
Here are other examples:
|
||||
|
||||
+1, -1 first reaches 0 twice.
|
||||
+3, +3, +4, -2, -4 first reaches 10 twice.
|
||||
-6, +3, +8, +5, -6 first reaches 5 twice.
|
||||
+7, +7, -2, -7, -4 first reaches 14 twice.
|
||||
What is the first frequency your device reaches twice?
|
||||
24
AdventOfCode2018/1stDay/challenge2/index.js
Normal file
24
AdventOfCode2018/1stDay/challenge2/index.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const fs = require('fs');
|
||||
let input = fs.readFileSync('input.txt')
|
||||
.toString()
|
||||
.split('\n')
|
||||
.map((x) => parseInt(x));
|
||||
|
||||
let seenFrequencies = new Set([0]);
|
||||
let total = 0;
|
||||
let i = 0;
|
||||
|
||||
while (true) {
|
||||
if (i === input.length) {
|
||||
i = 0;
|
||||
continue;
|
||||
}
|
||||
total += input[i];
|
||||
if (seenFrequencies.has(total)) {
|
||||
break;
|
||||
}
|
||||
seenFrequencies.add(total);
|
||||
i++;
|
||||
}
|
||||
|
||||
console.log(total)
|
||||
1008
AdventOfCode2018/1stDay/challenge2/input.txt
Normal file
1008
AdventOfCode2018/1stDay/challenge2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user