mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-13 11:13:47 +00:00
Import AdventOfCode2018 JavaScript solutions
git-subtree-dir: AdventOfCode JS/2018 git-subtree-mainline:b44957b275git-subtree-split:5e9be1b66b
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
After feeling like you've been falling for a few minutes, you look at the device's tiny screen. "Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock." Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.
|
||||
|
||||
For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:
|
||||
|
||||
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.
|
||||
In this example, the resulting frequency is 3.
|
||||
|
||||
Here are other example situations:
|
||||
|
||||
+1, +1, +1 results in 3
|
||||
+1, +1, -2 results in 0
|
||||
-1, -2, -3 results in -6
|
||||
Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?
|
||||
@@ -0,0 +1,20 @@
|
||||
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?
|
||||
@@ -0,0 +1,977 @@
|
||||
-12
|
||||
-6
|
||||
-12
|
||||
+1
|
||||
+3
|
||||
+3
|
||||
-1
|
||||
+10
|
||||
-8
|
||||
-9
|
||||
-11
|
||||
-2
|
||||
-7
|
||||
+15
|
||||
+16
|
||||
+14
|
||||
-2
|
||||
-5
|
||||
+11
|
||||
-8
|
||||
-5
|
||||
-1
|
||||
-19
|
||||
-14
|
||||
+6
|
||||
-16
|
||||
-8
|
||||
-13
|
||||
-19
|
||||
-11
|
||||
+3
|
||||
-19
|
||||
+3
|
||||
-19
|
||||
+9
|
||||
-15
|
||||
-11
|
||||
+18
|
||||
-9
|
||||
+13
|
||||
-17
|
||||
-16
|
||||
-13
|
||||
-16
|
||||
+19
|
||||
+17
|
||||
-18
|
||||
-8
|
||||
-5
|
||||
+6
|
||||
+9
|
||||
+8
|
||||
-7
|
||||
-8
|
||||
+2
|
||||
-9
|
||||
+2
|
||||
-9
|
||||
-5
|
||||
-3
|
||||
+4
|
||||
-13
|
||||
-5
|
||||
+11
|
||||
+14
|
||||
+5
|
||||
+17
|
||||
-1
|
||||
+9
|
||||
+6
|
||||
+14
|
||||
-9
|
||||
+4
|
||||
-3
|
||||
-13
|
||||
-5
|
||||
+13
|
||||
-2
|
||||
-10
|
||||
-18
|
||||
-12
|
||||
+4
|
||||
+13
|
||||
-10
|
||||
-11
|
||||
-10
|
||||
-17
|
||||
-13
|
||||
+12
|
||||
-19
|
||||
-7
|
||||
-13
|
||||
+5
|
||||
-8
|
||||
-11
|
||||
+5
|
||||
+7
|
||||
+17
|
||||
-14
|
||||
+22
|
||||
+14
|
||||
+7
|
||||
-8
|
||||
+7
|
||||
-19
|
||||
+14
|
||||
-20
|
||||
+16
|
||||
+12
|
||||
+3
|
||||
+5
|
||||
+1
|
||||
-8
|
||||
+1
|
||||
+15
|
||||
+3
|
||||
-6
|
||||
-8
|
||||
+9
|
||||
+3
|
||||
-19
|
||||
-19
|
||||
-19
|
||||
+1
|
||||
-16
|
||||
-13
|
||||
+5
|
||||
-13
|
||||
+2
|
||||
-15
|
||||
-15
|
||||
-4
|
||||
+12
|
||||
+1
|
||||
-10
|
||||
-5
|
||||
-11
|
||||
-9
|
||||
-11
|
||||
-7
|
||||
+10
|
||||
-18
|
||||
+16
|
||||
-12
|
||||
-19
|
||||
-17
|
||||
-18
|
||||
+2
|
||||
+15
|
||||
+11
|
||||
+9
|
||||
-17
|
||||
+4
|
||||
+9
|
||||
-18
|
||||
-6
|
||||
-1
|
||||
-1
|
||||
-18
|
||||
-15
|
||||
-18
|
||||
-7
|
||||
+16
|
||||
+18
|
||||
+18
|
||||
-2
|
||||
-4
|
||||
+1
|
||||
+15
|
||||
+16
|
||||
+18
|
||||
+7
|
||||
-18
|
||||
+6
|
||||
+8
|
||||
+15
|
||||
-8
|
||||
-16
|
||||
-3
|
||||
+11
|
||||
+9
|
||||
+1
|
||||
+4
|
||||
-18
|
||||
+3
|
||||
-16
|
||||
-14
|
||||
-18
|
||||
-10
|
||||
+6
|
||||
-12
|
||||
-12
|
||||
-2
|
||||
-8
|
||||
+1
|
||||
-3
|
||||
-11
|
||||
+1
|
||||
-5
|
||||
-4
|
||||
-5
|
||||
+8
|
||||
+8
|
||||
-4
|
||||
-10
|
||||
+16
|
||||
-1
|
||||
+3
|
||||
+9
|
||||
+6
|
||||
+24
|
||||
+12
|
||||
-2
|
||||
+11
|
||||
-1
|
||||
-9
|
||||
+21
|
||||
-15
|
||||
+12
|
||||
+19
|
||||
-3
|
||||
+21
|
||||
+20
|
||||
+16
|
||||
-4
|
||||
+15
|
||||
+9
|
||||
+13
|
||||
-16
|
||||
+13
|
||||
-1
|
||||
-16
|
||||
+13
|
||||
+19
|
||||
+14
|
||||
+19
|
||||
+14
|
||||
-17
|
||||
+18
|
||||
+1
|
||||
+11
|
||||
+18
|
||||
+18
|
||||
+22
|
||||
-8
|
||||
+18
|
||||
-1
|
||||
+19
|
||||
-11
|
||||
-20
|
||||
+11
|
||||
+8
|
||||
-11
|
||||
+25
|
||||
+13
|
||||
+3
|
||||
-1
|
||||
-3
|
||||
+17
|
||||
+15
|
||||
+7
|
||||
+19
|
||||
-7
|
||||
+19
|
||||
-1
|
||||
-12
|
||||
-14
|
||||
+17
|
||||
-15
|
||||
-1
|
||||
-18
|
||||
+9
|
||||
+14
|
||||
-19
|
||||
+10
|
||||
-9
|
||||
-11
|
||||
+7
|
||||
+20
|
||||
+20
|
||||
-1
|
||||
+19
|
||||
+1
|
||||
+7
|
||||
-19
|
||||
-15
|
||||
+3
|
||||
+1
|
||||
+17
|
||||
+11
|
||||
-7
|
||||
+15
|
||||
-7
|
||||
-10
|
||||
+13
|
||||
-24
|
||||
-21
|
||||
-4
|
||||
-4
|
||||
-14
|
||||
+6
|
||||
+3
|
||||
-25
|
||||
+9
|
||||
-7
|
||||
+15
|
||||
+12
|
||||
+14
|
||||
+20
|
||||
-11
|
||||
-11
|
||||
-15
|
||||
-14
|
||||
-9
|
||||
-2
|
||||
-2
|
||||
-3
|
||||
-1
|
||||
-18
|
||||
-2
|
||||
+6
|
||||
+17
|
||||
+9
|
||||
-1
|
||||
+18
|
||||
+20
|
||||
+21
|
||||
+27
|
||||
-17
|
||||
-11
|
||||
-4
|
||||
-18
|
||||
+24
|
||||
+33
|
||||
+4
|
||||
+12
|
||||
+8
|
||||
+8
|
||||
-1
|
||||
-17
|
||||
+6
|
||||
-18
|
||||
+16
|
||||
+4
|
||||
+1
|
||||
+6
|
||||
-1
|
||||
-11
|
||||
+18
|
||||
+15
|
||||
+17
|
||||
-4
|
||||
+13
|
||||
+1
|
||||
-20
|
||||
+18
|
||||
+3
|
||||
-16
|
||||
-4
|
||||
-12
|
||||
-10
|
||||
+8
|
||||
-18
|
||||
+22
|
||||
+21
|
||||
+12
|
||||
+1
|
||||
+6
|
||||
-24
|
||||
+7
|
||||
+1
|
||||
+11
|
||||
+14
|
||||
+20
|
||||
-5
|
||||
-2
|
||||
+4
|
||||
+9
|
||||
-20
|
||||
+60
|
||||
+15
|
||||
+18
|
||||
+14
|
||||
-19
|
||||
+13
|
||||
+3
|
||||
+7
|
||||
+8
|
||||
+5
|
||||
-14
|
||||
-13
|
||||
+20
|
||||
+5
|
||||
-1
|
||||
-15
|
||||
+29
|
||||
+14
|
||||
-19
|
||||
+7
|
||||
-10
|
||||
+13
|
||||
+3
|
||||
-7
|
||||
-2
|
||||
-19
|
||||
+8
|
||||
-15
|
||||
-13
|
||||
-13
|
||||
-1
|
||||
-9
|
||||
-14
|
||||
+10
|
||||
-9
|
||||
+18
|
||||
-3
|
||||
-12
|
||||
-7
|
||||
+21
|
||||
+2
|
||||
+7
|
||||
+7
|
||||
+10
|
||||
-15
|
||||
-16
|
||||
-6
|
||||
-8
|
||||
-18
|
||||
+11
|
||||
+13
|
||||
+7
|
||||
+10
|
||||
-14
|
||||
-14
|
||||
+17
|
||||
+22
|
||||
+23
|
||||
+15
|
||||
-4
|
||||
+32
|
||||
+7
|
||||
-5
|
||||
-3
|
||||
-10
|
||||
-14
|
||||
+9
|
||||
+12
|
||||
+10
|
||||
-12
|
||||
+17
|
||||
+1
|
||||
-19
|
||||
+16
|
||||
+14
|
||||
-2
|
||||
+1
|
||||
-7
|
||||
+17
|
||||
+16
|
||||
-8
|
||||
-15
|
||||
+18
|
||||
+19
|
||||
+14
|
||||
+52
|
||||
+8
|
||||
-22
|
||||
-2
|
||||
-2
|
||||
+13
|
||||
+6
|
||||
-4
|
||||
+2
|
||||
+20
|
||||
-14
|
||||
-73
|
||||
+119
|
||||
+20
|
||||
+26
|
||||
+4
|
||||
+9
|
||||
+14
|
||||
-42
|
||||
-18
|
||||
+44
|
||||
+18
|
||||
-23
|
||||
+77
|
||||
-11
|
||||
+15
|
||||
+52
|
||||
+29
|
||||
-7
|
||||
-11
|
||||
+31
|
||||
-29
|
||||
+5
|
||||
-20
|
||||
+435
|
||||
+15
|
||||
-156
|
||||
+71279
|
||||
-19
|
||||
-2
|
||||
+11
|
||||
-4
|
||||
-1
|
||||
-15
|
||||
+8
|
||||
+14
|
||||
+19
|
||||
-16
|
||||
-13
|
||||
+19
|
||||
+3
|
||||
-7
|
||||
-1
|
||||
-12
|
||||
-15
|
||||
-14
|
||||
-2
|
||||
+14
|
||||
+8
|
||||
-1
|
||||
-1
|
||||
-7
|
||||
-2
|
||||
-4
|
||||
-8
|
||||
-8
|
||||
+7
|
||||
+10
|
||||
-14
|
||||
-2
|
||||
-13
|
||||
-1
|
||||
+8
|
||||
-14
|
||||
-9
|
||||
+1
|
||||
+17
|
||||
-11
|
||||
-13
|
||||
+10
|
||||
+9
|
||||
+17
|
||||
-18
|
||||
-1
|
||||
+10
|
||||
-7
|
||||
+11
|
||||
-3
|
||||
+10
|
||||
+1
|
||||
+8
|
||||
-7
|
||||
+26
|
||||
+17
|
||||
-16
|
||||
-12
|
||||
+22
|
||||
-16
|
||||
+9
|
||||
-17
|
||||
-7
|
||||
+3
|
||||
+13
|
||||
-18
|
||||
+8
|
||||
+6
|
||||
+15
|
||||
+19
|
||||
+7
|
||||
+7
|
||||
-18
|
||||
+15
|
||||
+14
|
||||
+3
|
||||
+10
|
||||
-3
|
||||
+2
|
||||
+8
|
||||
+9
|
||||
-6
|
||||
+10
|
||||
+1
|
||||
+15
|
||||
-3
|
||||
-11
|
||||
+5
|
||||
-8
|
||||
-15
|
||||
+11
|
||||
-3
|
||||
-19
|
||||
-15
|
||||
-10
|
||||
+12
|
||||
-8
|
||||
+7
|
||||
+2
|
||||
+8
|
||||
+3
|
||||
-18
|
||||
+3
|
||||
-2
|
||||
-5
|
||||
-7
|
||||
+21
|
||||
+4
|
||||
-6
|
||||
+1
|
||||
+3
|
||||
+15
|
||||
-16
|
||||
+24
|
||||
+15
|
||||
-16
|
||||
-2
|
||||
-11
|
||||
+10
|
||||
-11
|
||||
+17
|
||||
+15
|
||||
+8
|
||||
+11
|
||||
+21
|
||||
+5
|
||||
+12
|
||||
+13
|
||||
-15
|
||||
-16
|
||||
-14
|
||||
+2
|
||||
-11
|
||||
+14
|
||||
-13
|
||||
+12
|
||||
-17
|
||||
+6
|
||||
-13
|
||||
+9
|
||||
-1
|
||||
+20
|
||||
-18
|
||||
-18
|
||||
-21
|
||||
-6
|
||||
+21
|
||||
+13
|
||||
-12
|
||||
-15
|
||||
-5
|
||||
+39
|
||||
+20
|
||||
+13
|
||||
+6
|
||||
-1
|
||||
+12
|
||||
+1
|
||||
-3
|
||||
+14
|
||||
-5
|
||||
-3
|
||||
+17
|
||||
+12
|
||||
-13
|
||||
+18
|
||||
+16
|
||||
-12
|
||||
-16
|
||||
-9
|
||||
+5
|
||||
-7
|
||||
-1
|
||||
-17
|
||||
-4
|
||||
-8
|
||||
+5
|
||||
-13
|
||||
+9
|
||||
-19
|
||||
+15
|
||||
+13
|
||||
-19
|
||||
-15
|
||||
-2
|
||||
+14
|
||||
-2
|
||||
-8
|
||||
+2
|
||||
+15
|
||||
-5
|
||||
-13
|
||||
+19
|
||||
+21
|
||||
+3
|
||||
+11
|
||||
-17
|
||||
+12
|
||||
+17
|
||||
+19
|
||||
-16
|
||||
+17
|
||||
+5
|
||||
-10
|
||||
+13
|
||||
+3
|
||||
-8
|
||||
+18
|
||||
-8
|
||||
-9
|
||||
-11
|
||||
+5
|
||||
-11
|
||||
-10
|
||||
-19
|
||||
+7
|
||||
-3
|
||||
-7
|
||||
-11
|
||||
+26
|
||||
-10
|
||||
+16
|
||||
+13
|
||||
-10
|
||||
+5
|
||||
+7
|
||||
-16
|
||||
+22
|
||||
-9
|
||||
-6
|
||||
+40
|
||||
-13
|
||||
+7
|
||||
-9
|
||||
+20
|
||||
+7
|
||||
+8
|
||||
-6
|
||||
+3
|
||||
-10
|
||||
-14
|
||||
+10
|
||||
-2
|
||||
+15
|
||||
-3
|
||||
+2
|
||||
+17
|
||||
+4
|
||||
+3
|
||||
+13
|
||||
+15
|
||||
-8
|
||||
+16
|
||||
+9
|
||||
-10
|
||||
+7
|
||||
+4
|
||||
+9
|
||||
-4
|
||||
+1
|
||||
-13
|
||||
-1
|
||||
+10
|
||||
-19
|
||||
+20
|
||||
-8
|
||||
-9
|
||||
-6
|
||||
-15
|
||||
-8
|
||||
+11
|
||||
+17
|
||||
+9
|
||||
-7
|
||||
-17
|
||||
-7
|
||||
+9
|
||||
+4
|
||||
-7
|
||||
+6
|
||||
+9
|
||||
-13
|
||||
-5
|
||||
-8
|
||||
+6
|
||||
-2
|
||||
-16
|
||||
+10
|
||||
-15
|
||||
+7
|
||||
+4
|
||||
+3
|
||||
-2
|
||||
+6
|
||||
-14
|
||||
-12
|
||||
-10
|
||||
+11
|
||||
-10
|
||||
+2
|
||||
-15
|
||||
-6
|
||||
+8
|
||||
-6
|
||||
-4
|
||||
+22
|
||||
-6
|
||||
+17
|
||||
+8
|
||||
+6
|
||||
-8
|
||||
-4
|
||||
-21
|
||||
-7
|
||||
+2
|
||||
+9
|
||||
-26
|
||||
-6
|
||||
+5
|
||||
-20
|
||||
-38
|
||||
-4
|
||||
-45
|
||||
-15
|
||||
+14
|
||||
-3
|
||||
-12
|
||||
+22
|
||||
+12
|
||||
-15
|
||||
+29
|
||||
-95
|
||||
-15
|
||||
-3
|
||||
+1
|
||||
-31
|
||||
-22
|
||||
-17
|
||||
-7
|
||||
-15
|
||||
-5
|
||||
+2
|
||||
-3
|
||||
+15
|
||||
+12
|
||||
-5
|
||||
-10
|
||||
+1
|
||||
+7
|
||||
-6
|
||||
+17
|
||||
+2
|
||||
+2
|
||||
-19
|
||||
-7
|
||||
+15
|
||||
-6
|
||||
-7
|
||||
-1
|
||||
+7
|
||||
-12
|
||||
-6
|
||||
-18
|
||||
-14
|
||||
+6
|
||||
-4
|
||||
-16
|
||||
-13
|
||||
+5
|
||||
-13
|
||||
+4
|
||||
+16
|
||||
-8
|
||||
+13
|
||||
+11
|
||||
-2
|
||||
-7
|
||||
-5
|
||||
-8
|
||||
+1
|
||||
+9
|
||||
+6
|
||||
-2
|
||||
+19
|
||||
-4
|
||||
+5
|
||||
+10
|
||||
+2
|
||||
-9
|
||||
+10
|
||||
-15
|
||||
+7
|
||||
+11
|
||||
+6
|
||||
-14
|
||||
-11
|
||||
-6
|
||||
+3
|
||||
-16
|
||||
-19
|
||||
-6
|
||||
-3
|
||||
-3
|
||||
-11
|
||||
+19
|
||||
+16
|
||||
-10
|
||||
+14
|
||||
+15
|
||||
+2
|
||||
+15
|
||||
+21
|
||||
+15
|
||||
+7
|
||||
+19
|
||||
+2
|
||||
+52
|
||||
+13
|
||||
+24
|
||||
-32
|
||||
-126
|
||||
-18
|
||||
+1
|
||||
-17
|
||||
-14
|
||||
-14
|
||||
+3
|
||||
-19
|
||||
-17
|
||||
+13
|
||||
+17
|
||||
-20
|
||||
-1
|
||||
+17
|
||||
+2
|
||||
+14
|
||||
-7
|
||||
-8
|
||||
+12
|
||||
-1
|
||||
-14
|
||||
+1
|
||||
+26
|
||||
-14
|
||||
-17
|
||||
+3
|
||||
-17
|
||||
+13
|
||||
-3
|
||||
+5
|
||||
-7
|
||||
+20
|
||||
-2
|
||||
+24
|
||||
+20
|
||||
-13
|
||||
-19
|
||||
-22
|
||||
-19
|
||||
+12
|
||||
+35
|
||||
+5
|
||||
+34
|
||||
+8
|
||||
+2
|
||||
+11
|
||||
-2
|
||||
-1
|
||||
+13
|
||||
-9
|
||||
-7
|
||||
-2
|
||||
-28
|
||||
-33
|
||||
-71491
|
||||
@@ -0,0 +1,9 @@
|
||||
+3
|
||||
+12
|
||||
-6
|
||||
-2
|
||||
-4
|
||||
-12
|
||||
+4
|
||||
+5
|
||||
-7
|
||||
@@ -0,0 +1,17 @@
|
||||
// AdventOfCode2018-01
|
||||
var fs = require('fs');
|
||||
var frequency = 0;
|
||||
|
||||
var textFile = fs.readFileSync('./input.txt').toString();
|
||||
var textArray = textFile.split(/\r?\n/);
|
||||
|
||||
textArray.forEach(line => {
|
||||
if (line.substr(0,1) == '+') {
|
||||
frequency += parseInt(line.substr(1));
|
||||
}
|
||||
else {
|
||||
frequency -= parseInt(line.substr(1));
|
||||
}
|
||||
});
|
||||
|
||||
console.log('The resulting frequency is ' + frequency);
|
||||
@@ -0,0 +1,50 @@
|
||||
// AdventOfCode2018-01
|
||||
var freqChanges = [];
|
||||
|
||||
function parseInput(inputFile) {
|
||||
// for each line in text file parse the symbol (first character) and the number into an array of objects
|
||||
var fs = require('fs'),
|
||||
readline = require('readline'),
|
||||
instream = fs.createReadStream(inputFile),
|
||||
outstream = new (require('stream'))(),
|
||||
rl = readline.createInterface(instream, outstream);
|
||||
|
||||
rl.on('line', function (line) {
|
||||
var object = {
|
||||
operation: line.substr(0,1),
|
||||
value: parseInt(line.substr(1))
|
||||
}
|
||||
freqChanges.push(object);
|
||||
});
|
||||
|
||||
rl.on('close', function (line) {
|
||||
//console.log(line);
|
||||
//console.log('done reading file.');
|
||||
});
|
||||
}
|
||||
|
||||
parseInput('./input1.txt');
|
||||
|
||||
// Starting from zero, loop through all the entries in the array and apply the operations
|
||||
var frequency = 0;
|
||||
console.log('length=' + freqChanges.length);
|
||||
freqChanges.forEach( function (object) {
|
||||
console.log('Object contains ' + object.toString());
|
||||
if (object.operation == '+') {
|
||||
frequency += object.value;
|
||||
}
|
||||
else {
|
||||
frequency -= object.value;
|
||||
}
|
||||
});
|
||||
// for (i in freqChanges) { //(var i = 0; i < freqChanges.length; i++) {
|
||||
// console.log(freqChanges[i].toString());
|
||||
// if (freqChanges[i].operation = '+') {
|
||||
// frequency += freqChanges[i].value;
|
||||
// }
|
||||
// else {
|
||||
// frequency -= freqChanges[i].value;
|
||||
// }
|
||||
// }
|
||||
|
||||
console.log('The resulting frequency is ' + frequency);
|
||||
@@ -0,0 +1,29 @@
|
||||
// AdventOfCode2018-01
|
||||
|
||||
var frequency = 0;
|
||||
|
||||
// for each line in text file parse the symbol (first character) and the number into an array of objects
|
||||
var fs = require('fs'),
|
||||
readline = require('readline'),
|
||||
instream = fs.createReadStream('./input1.txt'),
|
||||
outstream = new (require('stream'))(),
|
||||
rl = readline.createInterface(instream, outstream);
|
||||
|
||||
rl.on('line', function (line) {
|
||||
console.log(line.substr(0,1));
|
||||
if (line.substr(0,1) = '+') {
|
||||
console.log(parseInt(line.substr(1)));
|
||||
frequency += parseInt(line.substr(1));
|
||||
}
|
||||
else {
|
||||
console.log(parseInt(line.substr(1)));
|
||||
frequency -= parseInt(line.substr(1));
|
||||
}
|
||||
});
|
||||
|
||||
rl.on('close', function (line) {
|
||||
//console.log(line);
|
||||
//console.log('done reading file.');
|
||||
});
|
||||
|
||||
console.log('The resulting frequency is ' + frequency);
|
||||
@@ -0,0 +1,33 @@
|
||||
// AdventOfCode2018-01
|
||||
var fs = require('fs');
|
||||
var frequency = 0;
|
||||
var frequencyHistory = [];
|
||||
var loopCount = 0;
|
||||
|
||||
var textFile = fs.readFileSync('./input.txt').toString();
|
||||
var textArray = textFile.split(/\r?\n/);
|
||||
|
||||
var foundDuplicate = false;
|
||||
while (!foundDuplicate) {
|
||||
for (var line of textArray) {
|
||||
if (line.substr(0,1) == '+') {
|
||||
frequency += parseInt(line.substr(1));
|
||||
}
|
||||
else {
|
||||
frequency -= parseInt(line.substr(1));
|
||||
}
|
||||
//console.log(frequency.toString());
|
||||
if (frequencyHistory.includes(frequency)) {
|
||||
console.log('The first repeated frequency is ' + frequency);
|
||||
firstRepeatedFrequency = frequency;
|
||||
foundDuplicate = true;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
frequencyHistory.push(frequency);
|
||||
}
|
||||
};
|
||||
loopCount += 1;
|
||||
}
|
||||
|
||||
console.log(loopCount);
|
||||
@@ -0,0 +1,23 @@
|
||||
--- Day 2: Inventory Management System ---
|
||||
You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.
|
||||
|
||||
Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"
|
||||
|
||||
"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.
|
||||
|
||||
Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).
|
||||
|
||||
To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.
|
||||
|
||||
For example, if you see the following box IDs:
|
||||
|
||||
abcdef contains no letters that appear exactly two or three times.
|
||||
bababc contains two a and three b, so it counts for both.
|
||||
abbcde contains two b, but no letter appears exactly three times.
|
||||
abcccd contains three c, but no letter appears exactly two times.
|
||||
aabcdd contains two a and two d, but it only counts once.
|
||||
abcdee contains two e.
|
||||
ababab contains three a and three b, but it only counts once.
|
||||
Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.
|
||||
|
||||
What is the checksum for your list of box IDs?
|
||||
@@ -0,0 +1,15 @@
|
||||
--- Part Two ---
|
||||
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
|
||||
|
||||
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
|
||||
|
||||
abcde
|
||||
fghij
|
||||
klmno
|
||||
pqrst
|
||||
fguij
|
||||
axcye
|
||||
wvxyz
|
||||
The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.
|
||||
|
||||
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)
|
||||
@@ -0,0 +1,250 @@
|
||||
mphcuiszrnjzxwkbgdzqeoyxfa
|
||||
mihcuisgrnjzxwkbgdtqeoylia
|
||||
mphauisvrnjgxwkbgdtqeiylfa
|
||||
mphcuisnrnjzxwkbgdgqeoylua
|
||||
mphcuisurnjzxwkbgdtqeoilfi
|
||||
mkhcuisvrnjzowkbgdteeoylfa
|
||||
mphcoicvrnjzxwksgdtqeoylfa
|
||||
mxhcuisvrndzxwkbgdtqeeylfa
|
||||
dphcuisijnjzxwkbgdtqeoylfa
|
||||
mihvuisvrqjzxwkbgdtqeoylfa
|
||||
mphcuisrrnvzxwkbgdtqeodlfa
|
||||
mphtuisdrnjzxskbgdtqeoylfa
|
||||
mphcutmvsnjzxwkbgdtqeoylfa
|
||||
mphcunsvrnjzswkggdtqeoylfa
|
||||
mphcuisvrwjzxwkbpdtqeoylfr
|
||||
mphcujsdrnjzxwkbgdtqeovlfa
|
||||
mpfcuisvrdjzxwkbgdtteoylfa
|
||||
mppcuisvrpjzxwkbgdtqeoywfa
|
||||
mphcuisvrnjzxwkbfptqroylfa
|
||||
mphcuisvrnjzxwkbgstoeoysfa
|
||||
mphcufsvrnjzcwkbgdeqeoylfa
|
||||
mphcuissrnjzxwkbgdkquoylfa
|
||||
sphcuxsvrnjzxwkbgdtqioylfa
|
||||
mphcuiivrhjzxwkbgdtqevylfa
|
||||
echcuisvrnjzxwkbgltqeoylfa
|
||||
mphcuisvrljexwkbvdtqeoylfa
|
||||
mpjcuisvrnjzxwkhidtqeoylfa
|
||||
mphcuisvrfjzmwkbgdtqeoylfl
|
||||
mwhcuisvrnjzxwkbgdtqeoytfm
|
||||
mphcuisvrsjzxwkbgdaqeoylfh
|
||||
mohcuisvrnjzxwkbgdtqtoymfa
|
||||
maycuisvrnjzxwkbgdtqboylfa
|
||||
pphcuisvqnjzxwkbgdtqeoylfd
|
||||
mprcuisvrnjtxwmbgdtqeoylfa
|
||||
mfhcuisgrnjzxckbgdtqeoylfa
|
||||
mphiubsvrnjzxwkbgdtqeoyufa
|
||||
dphctisvrnjzxwkbgdtqeoylfk
|
||||
mphcuisvrnjznwksgdtqeoyzfa
|
||||
mpwcuisvrnjziwkbgdtqaoylfa
|
||||
mphduzsvrnjznwkbgdtqeoylfa
|
||||
mphccisvrnjzxwebgdtqeoylqa
|
||||
xphcuisvrnjzxwkfvdtqeoylfa
|
||||
mphcupsvrnjzxwkbgdtfeoylpa
|
||||
mphcuisvrtjzjwkbgdtqeoylfe
|
||||
mpbcuisvrnjzxwkbgdmieoylfa
|
||||
mphcuisvrnjzxwkbgjtqetylaa
|
||||
mphcuisvrnjzxwpbgdtgdoylfa
|
||||
ophcufsvrqjzxwkbgdtqeoylfa
|
||||
iphcuhsvrnjzxwkbgetqeoylfa
|
||||
mphcuisvunjzxwwbgdtqeoylqa
|
||||
mphcpisvrnjzowkbgdtveoylfa
|
||||
mphcuisvrnjzxhkbgdtqeotlla
|
||||
mphcuisvrnjzxwkbodtgeoylha
|
||||
mphcuisvrjjzxwkbwdtqtoylfa
|
||||
mphcwisvrnjnxwkbgjtqeoylfa
|
||||
mplcuicqrnjzxwkbgdtqeoylfa
|
||||
mphcuisvrnjzxydbgdtqeoylfn
|
||||
ophckisvrnjzxwkbgdtqeozlfa
|
||||
mphcuisvrkjzxwkbgdtteoblfa
|
||||
yphcuisvrnjcxwkbggtqeoylfa
|
||||
mphcuisvrnazxwfbqdtqeoylfa
|
||||
mphcuisvrmjzxwkbgdtlwoylfa
|
||||
mphctksvrnjzxwibgdtqeoylfa
|
||||
mphcuisprnjzxlebgdtqeoylfa
|
||||
mphcuisnrnjzxakbgdtueoylfa
|
||||
mphcuiavrnjoxwtbgdtqeoylfa
|
||||
nphcuisvrnjzxwkbgdtqzoylfk
|
||||
mphcuisrrnjmxwkbgdtqdoylfa
|
||||
mphcuisvrujzxwkvgdtqehylfa
|
||||
mphcuisvrnfzxwkogdtqebylfa
|
||||
mphcuisvrnjwdwkbgdtqeoyxfa
|
||||
mphcuisvrntzxwkrgxtqeoylfa
|
||||
mpzcuisvrnjzxwebgdtqeoylsa
|
||||
aphcuikvrnjzxwwbgdtqeoylfa
|
||||
mphcqisvrnjzxwkpgdtqeoelfa
|
||||
mphcuusvrnjzxwkbgdtjeodlfa
|
||||
mphcuisvrnjzewkbgdtteoylza
|
||||
mphcuisvanjzxwkbgdtheoylfc
|
||||
mphcjishrnjzxwkbgltqeoylfa
|
||||
mpxcuislrnjzxwkbgdtqeoynfa
|
||||
mphcuisvrnjjxwkbgdtmeoxlfa
|
||||
mphcimsvrnjzxwkbsdtqeoylfa
|
||||
mphcxisvcnjzxwjbgdtqeoylfa
|
||||
mphcuisbrvjzxwkbgdtqeoymfa
|
||||
mplcuisvrnjzxwkbgdtaenylfa
|
||||
mphcuihvrnjzxwkygytqeoylfa
|
||||
mphcbisvrnjzxhkbgdtqezylfa
|
||||
mphcuisarnjzxwkbgatqeoylfv
|
||||
mphcumsvrnjzxwkbgdrqebylfa
|
||||
mlhcuisvrnwzxwkbgdtqeoylfx
|
||||
mpkcuisvrkjzxwkbgdtqeoylfo
|
||||
mphcuissrnjzxwkbgdtqmoylfc
|
||||
mphcuiwvrnjuxwkfgdtqeoylfa
|
||||
mphcuicvlnjzxwkbgdvqeoylfa
|
||||
mphcuisvrvvzxwkbfdtqeoylfa
|
||||
myhcuisvrnjpxwkbgntqeoylfa
|
||||
mpocuisvrnjzxwtbgitqeoylfa
|
||||
mphcuisvrnjzxwkbgdtwewyqfa
|
||||
mphcuisvtnjzxwwbgdtqeoolfa
|
||||
mphcuisvrnjzxgkbgdyqeoyyfa
|
||||
mphcuisvrdjzxwkbgpyqeoylfa
|
||||
bphcuisvrnjzxwkbgxtqefylfa
|
||||
sphcuisvrdjzxwktgdtqeoylfa
|
||||
mphcuvsvrnjmxwobgdtqeoylfa
|
||||
mphcuisvrnjzxwkbsdtqeuylfb
|
||||
mnhcmisvynjzxwkbgdtqeoylfa
|
||||
mphckisvrnjzxwkhgdkqeoylfa
|
||||
mpacuisvrnjzxwkbgdtqeoolaa
|
||||
mpgcuisvrnjzxwkbzdtqeoynfa
|
||||
mphcuisvrojzxwkbzdtqeoylga
|
||||
mphcuisvknjfxwkbydtqeoylfa
|
||||
mphcuistrnjzxwkbgdqqeuylfa
|
||||
bpvcuiszrnjzxwkbgdtqeoylfa
|
||||
mphcuxsvrnjzswkbgdtqeoelfa
|
||||
mphcuisvbnjzxwlbgdtqeoylla
|
||||
mphcuisvonczxwkbgktqeoylfa
|
||||
mphcuisvrnkzxwvbgdtquoylfa
|
||||
mphcuisvrnjzxokfgdtqeoylia
|
||||
tphcuisvrnjzxwkbjdwqeoylfa
|
||||
mihcuisvrnjzpwibgdtqeoylfa
|
||||
mphcuisvrejzxwkbgdtqjuylfa
|
||||
mprcuisvrnjixwkxgdtqeoylfa
|
||||
mpqcuiszrnjzxwkbgdtqeodlfa
|
||||
mphcuasvrnjzzakbgdtqeoylva
|
||||
mphcuisvrnjzmwkbtdtqeoycfa
|
||||
mphcuisvrnjzxwkbcdtqioylxa
|
||||
mphckisvrnjzxwkbcdtqeoylfm
|
||||
mphcuisvrnjuxwbogdtqeoylfa
|
||||
mphcuisdrnjzxwkbldtqeoylfx
|
||||
mphcuisvrnjoxwkbgdtqeyyyfa
|
||||
mphcuicvqnjzxwkbgdtqeoylna
|
||||
mpmcuisvrnjzxwkbgdtqktylfa
|
||||
mphcuisvrnqzxwkggdtqeoykfa
|
||||
mphcuisvryjzxwkbydtqejylfa
|
||||
mphcugsvrnjzxwkbghtqeeylfa
|
||||
rphcuusvrnjzxwkwgdtqeoylfa
|
||||
zphwuiyvrnjzxwkbgdtqeoylfa
|
||||
cphcuivvrnjzxwkbgdtqenylfa
|
||||
mphcuisvrnjzxwkagotqevylfa
|
||||
mprcuisvrcjzxwkbgdtqeoytfa
|
||||
mphjugsvrnezxwkbgdtqeoylfa
|
||||
mphcuisvryjzxwkbgltqeoylaa
|
||||
mphcursvrnjzxfkbgdtqeoydfa
|
||||
mphcuisvrcuzxwkbgdtqeoylfw
|
||||
mphcuisvrijzxwkbgdtqeoelfh
|
||||
xphcuisvenjzxjkbgdtqeoylfa
|
||||
mphcuisvrnazxwkbgdeqeoylaa
|
||||
mphcuisbrsjzxwkbgdtqeoygfa
|
||||
mlhvuisvrnjzxwkbgdtqeoylfh
|
||||
mphcuisvrnjzxukbgdtqeoyhfy
|
||||
mpzcuilvrnjzawkbgdtqeoylfa
|
||||
hphcuisjfnjzxwkbgdtqeoylfa
|
||||
mahcuisvrnjzxwkegdtqeoylfi
|
||||
mphcuixvrnjzcwkbgdtqetylfa
|
||||
mphcuisvrnjzxwkdgdtqeoklfj
|
||||
mlhcuisvrnjzxwkbgdteeoylka
|
||||
mphcuifvrnjbxwkrgdtqeoylfa
|
||||
mphcuasvrnjzzwkbgdtqeoylva
|
||||
mphcuisvrnjzxwkboutqeoylba
|
||||
mbhcuisvcnjzxwklgdtqeoylfa
|
||||
mpbcuisvrnjzxgkbgdtqesylfa
|
||||
mphcuisvrnjfswkbgdtqeoylfd
|
||||
mphcuisvrnjzxwkbgdoweoysfa
|
||||
uphcuisvrnjzrwkbgdtqelylfa
|
||||
mphcuisvrnjzxwkbgdtqyoylsi
|
||||
mpqcuiqvxnjzxwkbgdtqeoylfa
|
||||
mphcuisorfjzxwkbgatqeoylfa
|
||||
mphcuisvrntfxwkbzdtqeoylfa
|
||||
mphcuisvrnrzxwkbgdtueoylfl
|
||||
mphcuisvrnjzewkagdtyeoylfa
|
||||
mpocuisdrnjzxwkbgdtqeozlfa
|
||||
mphcuisvrnjjxwkbgdtoeoylfm
|
||||
mphcuisvenjzxwkbgdtqwoylza
|
||||
mpmcuisvrnjzxwkbgdtqeoxlfr
|
||||
mphcuisvgnjhxwkbgdtqeoplfa
|
||||
mphcuisvrnjzowkdgdtqeoyyfa
|
||||
mphcuisqynjzxwkbgdtqeoylda
|
||||
hphcuisvgnjzxwkbgdtbeoylfa
|
||||
iphcuipvrnuzxwkbgdtqeoylfa
|
||||
mphcuisvrnjzsikbpdtqeoylfa
|
||||
mpwcuhsvrnjzxbkbgdtqeoylfa
|
||||
mnhjuisvcnjzxwkbgdtqeoylfa
|
||||
mphcudsvrnjzxwkbgdtqloilfa
|
||||
mpncuiwvrwjzxwkbgdtqeoylfa
|
||||
mphcuisvrnjgawkbgdtqeoylya
|
||||
mphcuisvrnjzxwkbggtteoslfa
|
||||
mphcuisvrnjzxwkbgdvqeoylpe
|
||||
mphcuisvrnczxfkbgktqeoylfa
|
||||
mphcuifvrnjzxwkbgdbmeoylfa
|
||||
mphcuisvrnjytwkbgdtqeoylla
|
||||
mphcuisvrnjzxwkbgdtjeoxlfn
|
||||
mphjuisvrnjzxwkbghtqeoyffa
|
||||
mphcuisvrnjzxkrbgdtqeoylaa
|
||||
mphcbisvrnjzxwkbgttqeoylfs
|
||||
mphkuksvbnjzxwkbgdtqeoylfa
|
||||
nphcuidvrnjzxwhbgdtqeoylfa
|
||||
mphguzsvrnjzxwkbgdaqeoylfa
|
||||
mihcuisfrnjzxwkbgdtqhoylfa
|
||||
mphcuisvrnrzxwpbgdtqesylfa
|
||||
zphcuisvrnjzxwkbddtqeoylaa
|
||||
mphcuigvmnjzxwkbgdtqeoylba
|
||||
mjhcuisvrnjzxjkbgdtqeoylha
|
||||
mphnuisvrnjznwkbgdtqnoylfa
|
||||
mkhcuisvrnjcxwkbgdqqeoylfa
|
||||
mphcuisvenjzxwbbqdtqeoylfa
|
||||
qphcuisnrnjzawkbgdtqeoylfa
|
||||
mphcuisvrdjzxwkbgdtqeoywca
|
||||
mphcuzsvvnjzxwfbgdtqeoylfa
|
||||
pphcuxsvrnjzxwkbgdtmeoylfa
|
||||
mphiuvsvrnjzxlkbgdtqeoylfa
|
||||
mphlqisvrnjzxkkbgdtqeoylfa
|
||||
mmhcuisvrnjzxwkbgatqeoylea
|
||||
mphduisrrnjoxwkbgdtqeoylfa
|
||||
mphcuisvrnjnxwkvgdyqeoylfa
|
||||
mphcuvsvrnjzxgkbgdtqeoylfz
|
||||
mphcuisvryjzxwkbggtqkoylfa
|
||||
iphcuisvrdjzxwkbgotqeoylfa
|
||||
mphcuisvrnjzxwhbgdtqwoyofa
|
||||
mphcorbvrnjzxwkbgdtqeoylfa
|
||||
mghcuisvrnpzxykbgdtqeoylfa
|
||||
mphauisvrnjnxwkbzdtqeoylfa
|
||||
mphcgisvrnjzxwkwgdtqeoygfa
|
||||
mphcuisvrnjzxwkggotqeoylba
|
||||
mphcuesvrnjzxwkbgdwqebylfa
|
||||
yphcuisvrnjzxwkbgdxqeoylja
|
||||
ephyuisvrnjzywkbgdtqeoylfa
|
||||
mfhcuisqrnjzxwkbgdlqeoylfa
|
||||
mphkuisvrnjzxwkbertqeoylfa
|
||||
mphcuusgrnjzxwkbggtqeoylfa
|
||||
mphcuildrnjvxwkbgdtqeoylfa
|
||||
mphcuiuvrnjzlwkbgwtqeoylfa
|
||||
mppcuisvrljzxwkbgdtqeoylfw
|
||||
mphcwiwvrnjzxwsbgdtqeoylfa
|
||||
mphcubivrnjzxwkqgdtqeoylfa
|
||||
mphcuisvrnjpxwkngdtqeoylpa
|
||||
pchcuisvrgjzxwkbgdtqeoylfa
|
||||
mphcuisvlnjzxwkbgdtmeoylfw
|
||||
mphcuisvrnjzywkbgdvqeoylfj
|
||||
mpzcuisvrnezxwktgdtqeoylfa
|
||||
mphcuisvrnjbxwkbgzrqeoylfa
|
||||
mphcuisvrnjzxwktgdtqeodtfa
|
||||
jphcuiavrnjzxwkbgdtqeoylfv
|
||||
mphcuisvrnjzxwkbddppeoylfa
|
||||
mphcuissrkjzxwkbgxtqeoylfa
|
||||
mphcuisvrhjzxwxbgdtqeoylxa
|
||||
mphcvisvgnjjxwkbgdtqeoylfa
|
||||
mphcuisprnjwxwtbgdtqeoylfa
|
||||
mphcuissrnjzxqkbgdtqeoymfa
|
||||
mphcuiabrnjzxokbgdtqeoylfa
|
||||
mphcuisvrnczxwkbgmtpeoylfa
|
||||
@@ -0,0 +1,4 @@
|
||||
aabcdefghijklmnopqrstuvwxy
|
||||
abcdefgbhijklmnopbqrstuvwx
|
||||
abcdeygbhijklmnopbqrstuvwx
|
||||
abcdeygbhijhfdsnopbqrstuvw
|
||||
@@ -0,0 +1,48 @@
|
||||
// AdventOfCode2018-02
|
||||
|
||||
var twiceCounter = 0;
|
||||
var thriceCounter = 0;
|
||||
|
||||
// Import the input file
|
||||
var fs = require('fs');
|
||||
var frequency = 0;
|
||||
|
||||
var textFile = fs.readFileSync('./input.txt').toString();
|
||||
var textArray = textFile.split(/\r?\n/);
|
||||
|
||||
// For each line of the input file
|
||||
textArray.forEach(line => {
|
||||
var searchCharHistory = [];
|
||||
var lineTwiceCounter = 0;
|
||||
var lineThriceCounter = 0;
|
||||
for (i = 0; i < line.length; i++) {
|
||||
searchChar = line.charAt(i);
|
||||
if (!searchCharHistory.indexOf(searchChar) >= 0) {
|
||||
// If we haven't already counted this letter
|
||||
//console.log('Searching for ' + searchChar + ' in line ' + line);
|
||||
var re = new RegExp(searchChar, 'g');
|
||||
var matchCharCount = (line.match(re) || []).length;
|
||||
//console.log(matchCharCount);
|
||||
// Check if a letter appears twice (and increment the counter)
|
||||
if (matchCharCount == 2) {
|
||||
lineTwiceCounter += 1;
|
||||
}
|
||||
// Check if a letter appears three times (and increment the counter)
|
||||
else if (matchCharCount == 3) {
|
||||
lineThriceCounter += 1;
|
||||
}
|
||||
}
|
||||
searchCharHistory.push(searchChar);
|
||||
}
|
||||
if (lineTwiceCounter > 0) {
|
||||
twiceCounter += 1;
|
||||
}
|
||||
if (lineThriceCounter > 0) {
|
||||
thriceCounter += 1;
|
||||
}
|
||||
});
|
||||
|
||||
console.log(twiceCounter);
|
||||
console.log(thriceCounter);
|
||||
// multiply the two counters together
|
||||
console.log('The checksum is ' + (twiceCounter * thriceCounter));
|
||||
@@ -0,0 +1,63 @@
|
||||
// AdventOfCode2018-02
|
||||
|
||||
var twiceCounter = 0;
|
||||
var thriceCounter = 0;
|
||||
|
||||
// Import the input file
|
||||
var fs = require('fs');
|
||||
var frequency = 0;
|
||||
|
||||
var textFile = fs.readFileSync('./input.txt').toString();
|
||||
var textArray = textFile.split(/\r?\n/);
|
||||
|
||||
function compareStrings (string1, string2) {
|
||||
if (string1 == string2) {
|
||||
// Do nothing, it's the same string
|
||||
}
|
||||
|
||||
// Split the characters of both strings into arrays
|
||||
var arr1 = [];
|
||||
var arr2 = [];
|
||||
for (ii = 0; ii < string1.length; ii++) {
|
||||
arr1.push(string1.charAt(ii));
|
||||
}
|
||||
for (jj = 0; jj < string2.length; jj++) {
|
||||
arr2.push(string2.charAt(jj));
|
||||
}
|
||||
|
||||
var wrongCharCounter = 0;
|
||||
var sameChars = [];
|
||||
// For each letter in the first array, check if it corresponds to the appropriate letter in the second array
|
||||
for (kk = 0; kk < arr1.length; kk++) {
|
||||
if (arr1[kk] != arr2[kk]) {
|
||||
wrongCharCounter += 1;
|
||||
}
|
||||
else {
|
||||
sameChars.push(arr1[kk]);
|
||||
}
|
||||
}
|
||||
if (wrongCharCounter == 1) {
|
||||
var re = new RegExp(',', 'g');
|
||||
console.log('Common letters are: ' + sameChars.toString().replace(re, ''));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var endLoop = false;
|
||||
// For each line of the input file
|
||||
for (i = 0; i < textArray.length; i++) {
|
||||
// Compare this line with each other line in the array
|
||||
for (j = 0; j < textArray.length; j++) {
|
||||
if (compareStrings(textArray[i],textArray[j])) {
|
||||
// If it finds a string with just one letter wrong
|
||||
endLoop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (endLoop) {
|
||||
break;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Josh Creek
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1 @@
|
||||
# AdventOfCode2018
|
||||
Reference in New Issue
Block a user