Zikeji

joined 2 years ago
[โ€“] Zikeji@programming.dev 4 points 1 month ago

Javascript

Short and sweet. Basically just handle all the range overlaps for part 2 and then do basic subtraction to get the answer.

spoiler

const input = require('fs').readFileSync('input-day5.txt', 'utf-8');

/** @typedef {[number, number]} Range */

/** @type {[Range[], number[]]} */
const [freshRanges, availableIngredients] = input.split("\n\n").map(l => l.split("\n")).map((l, i) => (i === 1) ? l.map(v => parseInt(v, 10)) : l.map(v => v.split('-').map(vv => parseInt(vv, 10))));


let freshOnHand = 0;

for (const ingredient of availableIngredients) {
    for (const [start, stop] of freshRanges) {
        if (ingredient >= start && ingredient <= stop) {
            freshOnHand += 1;
            break;
        }
    }
}

console.log(`Part 1 Answer: ${freshOnHand}`);

const sortedRanges = [...freshRanges].sort((a, b) => a[0] - b[0]);

const mergedRanges = [];
let current = sortedRanges[0];

for (let i = 1; i < sortedRanges.length; i++) {
    const [nextStart, nextEnd] = sortedRanges[i];
    
    if (nextStart <= current[1] + 1) {
        current = [current[0], Math.max(current[1], nextEnd)];
    } else {
        mergedRanges.push(current);
        current = [nextStart, nextEnd];
    }
}

mergedRanges.push(current);

const freshIngredientCount = mergedRanges.reduce((acc, [start, stop]) => acc + ((stop + 1) - start), 0)

console.log(`Part 2 Answer: ${freshIngredientCount}`);

[โ€“] Zikeji@programming.dev 28 points 1 month ago

Saved this. My doggo is nearly 15 and whole he's still healthy I am much too afraid of losing him. I suspect I'll need a reminder like this in the future.

[โ€“] Zikeji@programming.dev 3 points 1 month ago

Javascript

After smashing out a functional version in 20 minutes, I converted it into a OOP approach for a more appealing solution.

Solution

const input = require('fs').readFileSync('input-day4.txt', 'utf-8');

class PrintingDepartmentMap {
    /** @param {string} input */
    constructor(input) {
        /** @type {number[][]} */
        this.map = input.split("\n").map(r => r.split('').map(v => v === '@' ? 1 : 0));
    }

    /**
     * @param {number} x
     * @param {number} y
     * @returns {number} the value of that tile, or -1 if invalid
     */
    getCoord(x, y) {
        if (x < 0 || y < 0 || x >= this.map[0].length || y >= this.map.length) {
            return -1;
        }
        return this.map[y][x];
    }

    /**
     * @param {number} x
     * @param {number} y
     * @returns {number} the number of adjacent tiles that are >= 1
     */
    countAdjacent(x, y) {
        return [
            // top-left
            this.getCoord(x - 1, y - 1) >= 1,
            // top
            this.getCoord(x, y - 1) >= 1,
            // top-right
            this.getCoord(x + 1, y - 1) >= 1,
            // right
            this.getCoord(x + 1, y) >= 1,
            // bottom-right
            this.getCoord(x + 1, y + 1) >= 1,
            // bottom
            this.getCoord(x, y + 1) >= 1,
            // bottom-left
            this.getCoord(x - 1, y + 1) >= 1,
            // left
            this.getCoord(x - 1, y) >= 1
        ].reduce((acc, v) => !!v ? acc + 1 : acc, 0);
    }

    /**
     * transform in place the map replacing any rolls (1) with (2) if they are accessible
     * @returns {PrintingDepartmentMap}
     */
    markAccessable() {
        for (let y = 0; y < this.map.length; y += 1) {
            for (let x = 0; x < this.map[0].length; x += 1) {
                if (this.map[y][x] < 1) continue;
                if (this.countAdjacent(x, y) < 4) {
                    this.map[y][x] = 2;
                }

            }
        }
        return this;
    }

    /** @returns {number} the number of removed rolls */
    removeAccessable() {
        let removed = 0;
        for (let y = 0; y < this.map.length; y += 1) {
            for (let x = 0; x < this.map[0].length; x += 1) {
                if (this.map[y][x] === 2) {
                    removed += 1;
                    this.map[y][x] = 0;
                }
            }
        }
        return removed;
    }
}

const map = new PrintingDepartmentMap(input);

let removed = 0;
while (true) {
    const justRemoved = map.markAccessable().removeAccessable();
    if (removed === 0) {
        console.log(`Part 1 Answer: ${justRemoved}`);
    }
    if (justRemoved === 0) break;
    removed += justRemoved;
}

console.log(`Part 2 Answer: ${removed}`);

[โ€“] Zikeji@programming.dev 10 points 1 month ago (4 children)

Sure, which makes the transition suck even lol.

[โ€“] Zikeji@programming.dev 30 points 1 month ago (6 children)

:(

Time to switch to yarn or something.

[โ€“] Zikeji@programming.dev 3 points 1 month ago

Javascript

More bruteforcing! There are probably better ways to do this but I'm happy enough with this lol.

SolutionYou can replace the require('fs') on the first line with the input and run it in your browser console as well.

const input = require('fs').readFileSync('input-day2.txt', 'utf-8');

let idsPart1 = [];
let idsPart2 = [];

input.split(',').forEach(range => {
    const [start, end] = range.split('-').map(v => parseInt(v, 10));
    let cursor = start;

    while (cursor <= end) {
        const cursorString = cursor.toString(10);

        // part 1 check
        let halfLength = Math.floor(cursorString.length / 2);
        let left = cursorString.slice(0, halfLength);
        let right = cursorString.slice(halfLength, cursorString.length);
        if (left === right) {
            idsPart1.push(cursor);
        }

        // part 2 check
        let sequenceLength = 1;
        while (sequenceLength <= halfLength) {
            const sequence = cursorString.slice(0, sequenceLength);
            let builtString = sequence;
            while (builtString.length < cursorString.length) {
                builtString = `${builtString}${sequence}`;
            }
            if (builtString === cursorString) {
                idsPart2.push(cursor);
                break;
            }

            sequenceLength += 1;
        }

        cursor += 1;
    }
})

const answer1 = idsPart1.flat().reduce((acc, cur) => acc += cur, 0);
const answer2 = idsPart2.flat().reduce((acc, cur) => acc += cur, 0);

console.log(`Part 1 Answer: ${answer1}`);
console.log(`Part 2 Answer: ${answer2}`);

[โ€“] Zikeji@programming.dev 5 points 1 month ago (1 children)

A straightforward day 1 for a Javascript / NodeJS solution.

Poorly Optimized JS Solution

let position = 50;
let answer1 = 0;
let answer2 = 0;

const sequence = require('fs').readFileSync('input-day1.txt', 'utf-8');

sequence.split('\n').forEach(instruction => {
    const turn = instruction.charAt(0);
    let distance = parseInt(instruction.slice(1), 10);

    while (distance > 0) {
        distance -= 1;

        if (turn === 'L') {
            position -= 1;
        } else if (turn === 'R') {
            position += 1;
        }

        if (position >= 100) {
            position -= 100;
        } else if (position < 0) {
            position += 100;
        }

        if (position === 0) {
            answer2 += 1;
        }
    }

    if (position === 0) {
        answer1 += 1;
    }
});

console.log(`Part 1 Answer: ${answer1}`);
console.log(`Part 2 Answer: ${answer2}`);

[โ€“] Zikeji@programming.dev 5 points 1 month ago

Streaming like movies and TV, or like Twitch?

And free as in the legal free ones, or illegal free ones?

[โ€“] Zikeji@programming.dev 101 points 1 month ago (7 children)

It's worth noting that Ozempic face is a side effect of rapid weight loss, not a side effect of Ozempic.

[โ€“] Zikeji@programming.dev 70 points 1 month ago

If I didn't know Trump was a flip flopping erratic personality him saying they have a lot on common would be very concerning lol.

[โ€“] Zikeji@programming.dev 12 points 1 month ago (1 children)

Does not appear to be the correct MR. Comments on the issue allude to "they never pushed it" so sounds like there never was an MR. Watching the announcement where they demo'd it, it wrote much more than is in that MR. Not to defend OpenAI, I hate vibe coded solutions that add so many useless comments.

Write. Readable. Code.

[โ€“] Zikeji@programming.dev 16 points 1 month ago

THERE'S NO LAWS AGAINST THE POKEMON BATMAN

view more: โ€น prev next โ€บ