Commit 5b3fbd3d authored by Philipp Rummelsberger's avatar Philipp Rummelsberger
Browse files

.

parent 8948d9a1
const importer = require('../../utils/import');
async function question_01() {
const result = await importer.importInputAsArray('../2021/01/input.txt')
.then(list => {
let count = 0;
list.reduce((prev, curr) => {
if (prev < curr || !prev) {
count ++;
}
return curr;
}, undefined);
return count;
})
console.log('TADA ', result);
}
async function question_02() {
const result = await importer.importInputAsArray('../2021/01/input.txt')
.then(list => {
const copy = list;
const data = list
.map((_, index) => copy.slice(index, index + 3))
.filter(element => element.length === 3)
.map(element => element.reduce((prev, curr) => parseInt(prev) + parseInt(curr), 0));
console.log('DATA: ', data);
return data;
})
.then(list => {
let count = 0;
list.reduce((prev, curr) => {
if (prev < curr || !prev) {
count ++;
}
return curr;
}, undefined);
return count;
})
console.log('TADA ', result);
}
question_02()
199
200
208
210
200
207
240
269
260
263
const importer = require('../../utils/import');
async function question_01() {
const result = await importer.importInputAsArray('../2021/02/input.txt')
.then(data => data.map(d => d.split(' ')))
.then(data => data.reduce((prev, curr) => {
const key = curr[0];
const value = parseInt(curr[1]);
const prevValue = parseInt(prev[key]);
return {
...prev,
[key]: key === 'up' ? prevValue - value : prevValue + value,
}
}, { forward: 0, down: 0, up: 0 }))
.then(data => ({
horizontal_position: data.forward,
depth: data.up + data.down,
}))
.then(data => data.horizontal_position * data.depth)
console.log('TADA ', result);
}
async function question_02() {
const result = await importer.importInputAsArray('../2021/02/input.txt')
.then(data => data.map(d => d.split(' ')))
.then(data => data.reduce((prev, curr) => {
const key = curr[0];
const value = parseInt(curr[1]);
const prevValue = parseInt(prev[key]);
let aim = prev.aim;
if (key === 'up') {
aim = prev.aim - value;
} else if (key === 'down') {
aim = prev.aim + value;
}
return {
forward: key === 'forward' ? prev.forward + value : prev.forward,
aim: aim,
down: key === 'forward' ? prev.down + (aim * value) : prev.down,
}
}, { forward: 0, down: 0, up: 0, aim: 0 }))
.then(data => ({
horizontal_position: data.forward,
depth: data.down,
}))
.then(data => data.horizontal_position * data.depth)
console.log('TADA ', result);
}
question_01();
question_02();
forward 4
down 8
down 3
down 1
forward 8
up 6
down 4
forward 2
down 4
down 6
down 7
forward 1
down 4
down 6
forward 7
down 2
up 8
up 3
forward 1
forward 2
down 3
down 8
forward 6
forward 5
down 4
down 1
up 5
down 5
down 2
up 6
forward 4
forward 3
down 8
down 9
up 2
forward 1
forward 2
down 1
forward 3
down 7
up 6
down 1
down 7
down 5
forward 8
down 5
down 1
down 7
up 9
forward 6
up 8
down 3
down 9
down 3
forward 2
forward 1
forward 4
down 7
up 8
down 1
up 1
forward 4
down 7
forward 5
forward 2
forward 1
up 8
down 2
up 6
down 7
down 4
up 6
forward 2
forward 8
down 8
down 2
forward 2
forward 9
down 1
forward 5
down 4
forward 4
down 2
down 1
forward 7
down 1
down 5
down 5
up 5
forward 7
forward 6
forward 3
forward 9
forward 3
forward 5
down 8
down 9
forward 7
up 5
up 7
down 5
up 9
forward 9
up 8
up 2
forward 5
down 2
forward 2
down 4
up 4
down 2
up 3
up 8
down 3
down 4
down 7
forward 3
forward 9
down 1
down 2
down 5
down 1
forward 2
forward 2
up 8
down 4
forward 7
up 6
down 9
down 6
up 1
down 2
forward 6
down 4
up 1
forward 3
down 4
down 1
up 8
forward 3
down 5
up 2
down 8
down 4
up 2
down 2
forward 6
up 4
up 2
down 2
forward 7
down 5
forward 2
forward 8
up 3
forward 5
up 6
down 4
down 1
down 8
down 2
forward 8
up 2
down 5
up 8
down 1
down 1
down 5
up 4
down 1
down 3
down 8
forward 6
down 9
forward 6
up 2
forward 1
forward 9
down 9
down 3
down 9
down 6
down 4
down 8
forward 1
down 1
forward 2
up 2
forward 8
down 1
up 6
down 4
down 3
forward 8
up 7
down 6
down 1
down 2
forward 1
up 5
up 7
down 6
down 4
down 5
forward 9
down 7
down 9
down 5
forward 9
forward 7
forward 9
forward 8
up 4
forward 5
down 7
forward 8
up 1
forward 3
forward 2
forward 2
down 7
forward 9
down 7
down 9
forward 6
forward 8
up 5
up 8
up 7
up 6
forward 7
down 6
down 5
down 3
forward 7
down 7
forward 6
down 4
down 2
down 9
down 2
up 8
down 8
down 3
down 4
forward 3
up 6
down 9
forward 1
down 3
forward 9
down 6
forward 9
forward 8
forward 5
up 2
forward 5
up 7
down 6
forward 6
down 8
forward 2
down 7
down 8
up 1
forward 3
forward 5
down 3
forward 8
up 7
forward 9
forward 6
forward 1
forward 7
down 5
forward 3
down 5
down 6
down 7
down 3
down 8
up 5
forward 2
forward 5
up 7
up 4
forward 1
forward 1
down 1
down 7
forward 4
up 8
forward 5
down 9
up 7
forward 8
down 4
forward 4
forward 6
down 8
forward 7
down 1
forward 9
down 9
up 1
down 6
forward 6
down 7
down 4
forward 6
forward 3
down 5
up 5
up 7
up 5
down 6
forward 7
up 3
down 2
forward 6
down 8
down 7
up 9
forward 3
forward 1
down 8
forward 6
forward 4
up 9
forward 3
down 1
forward 4
forward 9
forward 2
forward 8
forward 1
forward 2
forward 7
down 6
forward 6
up 2
forward 5
up 8
down 9
up 8
down 5
down 1
down 6
up 4
down 4
down 5
up 6
down 8
down 8
forward 9
forward 8
forward 2
down 2
up 3
forward 2
down 8
down 8
forward 3
forward 5
down 9
down 2
forward 6
forward 7
down 7
forward 4
forward 2
down 1
down 6
up 5
down 2
forward 3
forward 9
down 9
down 3
forward 3
forward 6
down 2
forward 5
forward 7
down 6
forward 4
down 6
forward 6
forward 3
forward 3
forward 8
down 4
up 4
down 6
down 4
down 9
forward 7
forward 4
forward 7
down 3
forward 1
down 7
down 3
forward 1
down 7
down 5
forward 6
up 7
down 7
forward 5
forward 5
up 9
down 7
forward 1
forward 2
down 4
down 8
down 7
forward 4
forward 4
forward 3
down 8
down 7
down 8
forward 2
down 2
forward 2
forward 4
up 6
down 4
up 3
forward 7
down 9
down 3
forward 3
down 2
down 2
up 5
down 4
forward 3
forward 3
up 7
forward 8
forward 6
down 3
forward 2
down 6
up 1
down 7
down 7
forward 8
up 1
up 8
up 4
up 1
forward 4
forward 9
down 9
down 5
down 3
forward 8
down 3
forward 4
down 6
down 9
down 3
forward 6
up 1
up 4
forward 9
down 3
up 1
forward 4
up 1
forward 8
down 9
up 1
forward 3
down 7
down 7
down 3
forward 7
forward 5
down 8
up 8
down 6
down 4
forward 9
down 9
up 5
forward 6
down 8
up 8
down 2
forward 1
down 8
down 2
forward 7
forward 2
down 2
forward 5
up 2
down 6
down 1
down 6
down 3
up 4
forward 4
forward 8
down 3
forward 9
forward 6
down 2
up 2
down 2
up 4
down 8
forward 5
down 4
forward 3
down 4
forward 6
down 8
down 2
up 7
down 3
down 6
up 1
forward 8
up 5
down 1
forward 3
down 2
down 5
up 5
up 2
down 2
down 2
down 4
forward 3
up 7
forward 8
forward 4
down 3
forward 8
down 4
down 9
down 7
up 3
up 4
down 4
forward 3
down 3
up 5
down 1
forward 4
forward 9