Last active
December 5, 2023 11:55
-
-
Save MidasXIV/40704701ccc901b37ac5fa739360c6fa to your computer and use it in GitHub Desktop.
Revisions
-
MidasXIV revised this gist
Dec 5, 2023 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
MidasXIV revised this gist
Dec 5, 2023 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ const day2Solution = (text) => { const rules = { red: 12, green: 13, blue: 14 } const textToArrayOfStrings = text.split(`\n`); const isGameValid = (str) => { const cubes = str.split(",").map(a => a.trim()); return cubes.every(cubeSet => { const [cubeValue, cubeColor ] = cubeSet.split(" "); return Number.parseInt(cubeValue) <= rules[cubeColor]; }); } const getGameId = str => Number.parseInt(str.split(" ")[1]); const output = textToArrayOfStrings.reduce((acc, str) => { const splits = str.split(":"); const gameID = getGameId(splits[0]); const gameSets = splits[1].split(";"); const isGameValidOp = gameSets.every(isGameValid); return acc + (isGameValidOp ? gameID: 0); }, 0); return output; } -
MidasXIV revised this gist
Dec 2, 2023 . 1 changed file with 44 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ const normalizeString = (str) => { const numberText = str.replace(/one|two|three|four|five|six|seven|eight|nine/g, (match) => { switch (match) { case "one": return "1"; case "two": return "2"; case "three": return "3"; case "four": return "4"; case "five": return "5"; case "six": return "6"; case "seven": return "7"; case "eight": return "8"; case "nine": return "9"; default: return match; } }); return numberText; } const day2Solution = (text) => { const textToArrayOfStrings = text.split(`\n`) const numberPattern = /\d+/g; const getCodeFromString = (str) => { const input = str.match(numberPattern).join(''); const code = input[0] + input.slice(-1); return code; } const output = textToArrayOfStrings.reduce((acc, str) => { const normalizedString = normalizeString(str); const extractedCodeInString = getCodeFromString(normalizedString); const code = Number.parseInt(extractedCodeInString); return acc + code; }, 0); return output; } -
MidasXIV created this gist
Dec 2, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ const day1Solution = (text) => { const textToArrayOfStrings = text.split(`\n`) const numberPattern = /\d+/g; const getCodeFromString = (str) => { const input = str.match(numberPattern).join(''); const code = input[0] + input.slice(-1); return code; } const output = textToArrayOfStrings.reduce((acc, str) => { const code = Number.parseInt(getCodeFromString(str)); return acc + code; }, 0); return output; }