DEV Community

Discussion on: AoC Day 12: Subterranean Sustainability

Collapse
 
themindfuldev profile image
Tiago Romero Garcia

JavaScript solution

I'm gonna omit reader.js which is the same as the other solutions, but you can find it at github.com/themindfuldev/advent-of...

12-common.js

const parseInput = lines => {
    const initialStateRegex = /^initial state: (?<initialState>.+)$/;
    const noteRegex = /^(?<pattern>.+) => (?<pot>.)$/;

    const { initialState } = lines[0].match(initialStateRegex).groups;
    const notes = new Map();

    for (let note of lines.slice(2)) {
        const { pattern, pot } = note.match(noteRegex).groups;
        notes.set(pattern, pot);
    }

    return { initialState, notes };
};

const processGeneration = (state, notes) => {
    let { start, pots } = state;
    const n = pots.length;

    let next = '';
    let prefixExtensions = 0;
    let suffixExtensions = 0;
    for (let i = 0; i < n - 2; i++) {
        const segment = pots.substring(i, i + 5);
        if (notes.has(segment)) {
            const pot = notes.get(segment);
            next += pot;

            if (pot === '#') {
                if (i < 2 && prefixExtensions === 0) {
                    prefixExtensions += 2 - i;
                }
                if ((i > n - 6) && suffixExtensions === 0) {
                    suffixExtensions += i - (n - 6);
                }
            } 

        }
        else {
            next += '.';
        }
    }    
    const prefix = '.'.repeat(2 + prefixExtensions);
    const suffix = '.'.repeat(suffixExtensions);

    return {
        start: start - prefixExtensions,
        pots: prefix + next + suffix
    }
};

const processGenerations = (initialState, notes, generations = 1, log = true) => {
    let state = {
        start: -4,
        pots: `....${initialState}....`
    };

    if (log) {
        console.log(state.pots);
    }

    for (let i = 0; i < generations; i++) {
        state = processGeneration(state, notes);
        if (log) {
            console.log(state.pots);
        }
    }

    return state;
}

const sumPots = ({ start, pots }) => {
    const n = pots.length;

    return pots.split('').reduce((sum, pot, i) => sum + (pot === '#' ? i + start : 0), 0);
};

module.exports = {
    parseInput,
    processGeneration,
    processGenerations,
    sumPots
};

12a.js

const { readFile } = require('./reader');
const {
    parseInput,
    processGenerations,
    sumPots
} = require('./12-common');

(async () => {
    const lines = await readFile('12-input.txt');

    const { initialState, notes } = parseInput(lines);

    const lastGeneration = processGenerations(initialState, notes, 20, false);

    const potsSum = sumPots(lastGeneration);

    console.log(`The sum of the numbers of all pots is ${potsSum}`);
})();

12b.js

const { readFile } = require('./reader');
const {
    parseInput,
    processGeneration,
    processGenerations,
    sumPots
} = require('./12-common');

(async () => {
    const lines = await readFile('12-input.txt');

    const { initialState, notes } = parseInput(lines);

    const initialBatch = processGenerations(initialState, notes, 160, false);
    const initialSum = sumPots(initialBatch);
    console.log(`The sum for the first 160 batch is ${initialSum}`);

    const diffBatch = processGeneration(initialBatch, notes);
    const diffSum = sumPots(diffBatch) - initialSum;
    console.log(`The sum for the just 1 generation is ${diffSum}`);

    const totalSum = initialSum + diffSum * (50000000000 - 160);
    console.log(`The total sum is ${totalSum}`);
})();