Skip to content

Instantly share code, notes, and snippets.

@MidasXIV
Last active December 5, 2023 11:55
Show Gist options
  • Select an option

  • Save MidasXIV/40704701ccc901b37ac5fa739360c6fa to your computer and use it in GitHub Desktop.

Select an option

Save MidasXIV/40704701ccc901b37ac5fa739360c6fa to your computer and use it in GitHub Desktop.

Revisions

  1. MidasXIV revised this gist Dec 5, 2023. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. MidasXIV revised this gist Dec 5, 2023. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions adventOfCode-2023-21.js
    Original 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;
    }
  3. MidasXIV revised this gist Dec 2, 2023. 1 changed file with 44 additions and 0 deletions.
    44 changes: 44 additions & 0 deletions adventOfCode-2023-2.js
    Original 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;
    }
  4. MidasXIV created this gist Dec 2, 2023.
    14 changes: 14 additions & 0 deletions adventOfCode-2023-1.js
    Original 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;
    }