Last active
December 13, 2018 02:14
-
-
Save dominicgan/22a3e069025e072592edbea35e5583f8 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://adventofcode.com/2018/day/1/input') | |
.then(res => { | |
// convert html response body to text | |
res.text().then(rawData => { | |
// convert rawData to array | |
const data = rawData.trim().split('\n').map(elem => parseInt(elem)); | |
console.log(data); | |
localStorage.setItem('advofcode-puzzle1', JSON.stringify(data)); // store item in localstorage | |
// reduce data into single value | |
const result = data.reduce((accumulator, currentValue) => accumulator + currentValue); | |
console.log(result); | |
}); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// convert rawData to array | |
const data = JSON.parse(localStorage.getItem('advofcode-puzzle1')); | |
console.log(data); | |
let freqObj = [0]; | |
let counter = 1; | |
let curFreq = data[0]; | |
let iter = 0; | |
const maxLen = data.length - 1; | |
while (freqObj.indexOf(curFreq) === -1) { | |
freqObj.push(curFreq); | |
curFreq+= data[counter]; | |
const existFreq = freqObj.indexOf(curFreq); | |
if (existFreq > -1) console.log(curFreq, existFreq, freqObj.length); | |
if (counter === maxLen) { | |
iter++; | |
counter = 0; // reset counter | |
} else { | |
counter++; | |
} | |
} | |
console.log('first repeated frequency is', curFreq); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://adventofcode.com/2018/day/1/input') | |
.then(res => { | |
// convert html response body to text | |
res.text().then(rawData => { | |
// convert rawData to array | |
const data = rawData.trim().split('\n').map(elem => parseInt(elem)); | |
console.log(data); | |
localStorage.setItem('advofcode-puzzle1', JSON.stringify(data)); // store item in localstorage | |
}); | |
}); | |
(function(){ | |
const data = JSON.parse(localStorage.getItem('advofcode-puzzle2')); | |
// console.log(data) | |
let twolettercount = 0; | |
let threelettercount = 0; | |
data.map(elem => { | |
const letters = elem.split('').sort(); | |
let dupes = []; | |
let dupeObj = {}; | |
letters.map((elem, i) => { | |
if (elem === letters[i+1]) { | |
dupes.push(elem); | |
} | |
}); | |
if (dupes.length) { | |
// count similar letters in dupes | |
dupes.map(elem => { | |
if (!dupeObj[elem]) { | |
dupeObj[elem] = 2; | |
} else { | |
dupeObj[elem]++; | |
} | |
}); | |
// console.log(dupes, dupeObj, Object.values(dupeObj)); | |
let has3letters = false; | |
let has2letters = false; | |
Object.values(dupeObj).map(elem => { | |
if (elem > 2) {has3letters = true} | |
if (elem == 2) {has2letters = true} | |
}); | |
if (has3letters) { | |
threelettercount++; | |
} | |
if (has2letters) { | |
twolettercount++; | |
} | |
} | |
}); | |
console.log(twolettercount, threelettercount, twolettercount * threelettercount); | |
console.log('checksum is', twolettercount * threelettercount); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
const data = JSON.parse(localStorage.getItem('advofcode-puzzle2')); | |
console.log(data.length); | |
const letterArr = data.map(elem => { | |
return elem.split(''); | |
}); | |
// console.log(letterArr); | |
const differences = []; | |
letterArr.map((word, i) => { | |
const letterArr2 = [...letterArr]; | |
letterArr2.splice(i,1); | |
console.log(letterArr2.length); | |
letterArr2.map(nextword => { | |
const worddiff = []; | |
word.map((char, k) => { | |
if (char !== nextword[k]) { | |
worddiff.push({ | |
diffId: k, | |
arr1: char, | |
arr2: nextword[k] | |
}); | |
} | |
}); | |
differences.push({ | |
str1: word.join(''), | |
str2: nextword.join(''), | |
diff: worddiff | |
}); | |
}); | |
}); | |
console.log(differences); | |
// find items with 1 difference only | |
const singledifference = differences.filter(elem => { | |
return elem.diff.length === 1; | |
}); | |
console.log(singledifference); | |
// check if singledifferences contain only 2 matching items and that both are equal | |
if (singledifference.length === 2 && singledifference[0].str1 === singledifference[1].str2 && singledifference[0].str1 === singledifference[1].str2) { | |
console.log('found! difference:', singledifference[0].diff); | |
// get common letters from strings by testing first match | |
const firstString = singledifference[0]; | |
const commonletters = firstString.str1.split(''); | |
commonletters.splice(singledifference[0].diff[0].diffId,1); | |
// console.log(commonletters); | |
// console.log(commonletters.join('')); | |
console.log('answer', commonletters.join('')); | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://adventofcode.com/2018/day/3/input') | |
.then(res => { | |
// convert html response body to text | |
res.text().then(rawData => { | |
// convert rawData to array | |
const data = rawData.trim().split('\n').map(elem => { | |
return { | |
id: parseInt(elem.split(' @')[0].split('#')[1]), | |
coordinates: { | |
x: parseInt(elem.split(' @ ')[1].split(',')[0]), | |
y: parseInt(elem.split(' @ ')[1].split(',')[1].split(':')[0]) | |
}, | |
dimensions: { | |
width: parseInt(elem.split(' @ ')[1].split(',')[1].split(': ')[1].split('x')[0]), | |
height: parseInt(elem.split(' @ ')[1].split(',')[1].split(': ')[1].split('x')[1]) | |
} | |
}; | |
}); | |
console.log(data); | |
localStorage.setItem('advofcode-puzzle3', JSON.stringify(data)); // store item in localstorage | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment