Day6

mail@pastecode.io avatar
unknown
javascript
7 months ago
1.1 kB
3
Indexable
Never
const path = require('path');
const fs = require('fs');
const input = fs
    .readFileSync(path.join(__dirname, 'input.txt'), 'utf8').trim().split('\n')
const partOne = (input) => {

    // sortera datan
    let times = []
    input.forEach(d => {
        let time = d.slice(d.indexOf(':') + 1).trim()
        times.push(time.replace(/\s+/g, ' ').trim().split(' ').map(Number));
    });
    //omvandla datan till objekt
    const obj = {
        time: times[0],
        distance: times[1],
    }

    // Formel för uträkning = (time-input) * input
    let answer = []
    let length = [];
    let inputs = 0;
    let multi = 1;
    for (let i = 0; i < obj.time.length; i++) {
        answer[i] = []
        for (let j = 0; j < obj.time[i]; j++) {
            inputs = (obj.time[i] - j) * j
            if (inputs > obj.distance[i]) {
                answer[i].push(inputs)
            }
        } length.push(answer[i].length)
        multi = multi * length[i]

    } return multi;

}
test = partOne(input);
console.log(`Svar på del 1 är ${test}`)
Leave a Comment