Skip to content

Instantly share code, notes, and snippets.

@MidasXIV
Last active December 5, 2023 11:55
Show Gist options
  • Save MidasXIV/40704701ccc901b37ac5fa739360c6fa to your computer and use it in GitHub Desktop.
Save MidasXIV/40704701ccc901b37ac5fa739360c6fa to your computer and use it in GitHub Desktop.
Advent Of Code 2023.
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;
}
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;
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment