From 5e9be1b66b9efe53586d3ee04607391d0d71082a Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Mon, 3 Dec 2018 10:03:32 +0000 Subject: [PATCH] Day 2 --- 02/TaskPart1.txt | 23 +++++ 02/TaskPart2.txt | 15 +++ 02/input.txt | 250 +++++++++++++++++++++++++++++++++++++++++++++++ 02/inputtest.txt | 4 + 02/solution01.js | 48 +++++++++ 02/solution02.js | 63 ++++++++++++ 6 files changed, 403 insertions(+) create mode 100644 02/TaskPart1.txt create mode 100644 02/TaskPart2.txt create mode 100644 02/input.txt create mode 100644 02/inputtest.txt create mode 100644 02/solution01.js create mode 100644 02/solution02.js diff --git a/02/TaskPart1.txt b/02/TaskPart1.txt new file mode 100644 index 0000000..1dea787 --- /dev/null +++ b/02/TaskPart1.txt @@ -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? \ No newline at end of file diff --git a/02/TaskPart2.txt b/02/TaskPart2.txt new file mode 100644 index 0000000..91d8648 --- /dev/null +++ b/02/TaskPart2.txt @@ -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.) \ No newline at end of file diff --git a/02/input.txt b/02/input.txt new file mode 100644 index 0000000..6696a02 --- /dev/null +++ b/02/input.txt @@ -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 \ No newline at end of file diff --git a/02/inputtest.txt b/02/inputtest.txt new file mode 100644 index 0000000..ea2924d --- /dev/null +++ b/02/inputtest.txt @@ -0,0 +1,4 @@ +aabcdefghijklmnopqrstuvwxy +abcdefgbhijklmnopbqrstuvwx +abcdeygbhijklmnopbqrstuvwx +abcdeygbhijhfdsnopbqrstuvw \ No newline at end of file diff --git a/02/solution01.js b/02/solution01.js new file mode 100644 index 0000000..40eb220 --- /dev/null +++ b/02/solution01.js @@ -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)); \ No newline at end of file diff --git a/02/solution02.js b/02/solution02.js new file mode 100644 index 0000000..536756d --- /dev/null +++ b/02/solution02.js @@ -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; + } +}; \ No newline at end of file