Day6

 avatar
unknown
javascript
2 years ago
1.1 kB
7
Indexable
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}`)
Editor is loading...
Leave a Comment