26

Day 5: Print Queue

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] Rin@lemm.ee 1 points 1 month ago

TypeScript

Solution

import { AdventOfCodeSolutionFunction } from "./solutions";

type RulesType = Map<number, Array<number>>;

const ReduceMiddleNumbers = (p: number, v: Array<number>) => p + v[(v.length - 1) / 2];

const CheckPages = (pages: Array<number>, rules: RulesType): [true] | [false, number, number] => {
    for (let index = 0; index < pages.length; index++) {
        const page = pages[index]; // [97,61,53,29,13] => 97
        const elementRules = rules.get(page);

        // there are no rules for this number
        if (elementRules === undefined)
            continue;

        for (let ruleIndex = 0; ruleIndex < elementRules.length; ruleIndex++) {
            const rule = elementRules[ruleIndex];
            const rulePos = pages.indexOf(rule);

            // the number specified in the rule doesn't exist in our page
            if (rulePos == -1)
                continue;

            // the number came before our current index, meaning it's bad.
            if (index > rulePos) 
                return [false, index, rulePos];
        }
    }

    return [true];
}


const Reposition = (pages: Array<number>, rules: RulesType) => {
    const newPages = [...pages];
    let res = CheckPages(newPages, rules);
    let i = 0; 
    // this will be public facing api and it's possible to create inf loops so, 10k limit
    while(!res[0] && i++ < 10000) {
        // yes I know the trick, but tricks < semantics
        const moveThisRight = newPages[res[1]];
        const moveThisLeft = newPages[res[2]];
        newPages[res[1]] = moveThisLeft;
        newPages[res[2]] = moveThisRight;

        res = CheckPages(newPages, rules);
    }

    return [...newPages];
}

export const solution_5: AdventOfCodeSolutionFunction = (input) => {
    const [rules_input, content_input] = input.split("\n\n").map(v => v.trim());

    const rules: RulesType = new Map();

    rules_input.split("\n").map(v => v.split("|").map(v => Number(v))).forEach((rule) => {
        const [k, v] = rule;
        if (rules.has(k))
            rules.get(k)!.push(v);
        else
            rules.set(k, [v]);
    });

    const correctArray: Array<Array<number>> = [];
    const incorrectArray: Array<Array<number>> = [];

    content_input.split("\n").map(v => v.split(",").map(v => Number(v))).forEach(pages => {
        if(CheckPages(pages, rules)[0])
            correctArray.push(pages);
        else
            incorrectArray.push(pages);
    });

    const part_1 = correctArray.reduce<number>(ReduceMiddleNumbers, 0);
    const part_2 = incorrectArray.map((v) => Reposition([...v], rules)).reduce<number>(ReduceMiddleNumbers, 0);

    return {
        part_1,
        part_2,
    }
}

this post was submitted on 05 Dec 2024
26 points (100.0% liked)

Advent Of Code

997 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS