nieuw jaar
This commit is contained in:
10
src/2021/day1/example.txt
Normal file
10
src/2021/day1/example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
35
src/2021/day1/index.ts
Normal file
35
src/2021/day1/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as fs from 'fs';
|
||||
import path from 'path';
|
||||
import {readFile} from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||
|
||||
let count = -1;
|
||||
let prev = -1;
|
||||
input.forEach((inp: any) => {
|
||||
const measure = Number(inp);
|
||||
if (measure > prev) count++;
|
||||
prev = measure;
|
||||
})
|
||||
|
||||
console.log(count);
|
||||
|
||||
let window: any = [];
|
||||
count = -1;
|
||||
prev = -1;
|
||||
input.forEach((inp: any) => {
|
||||
const measure = Number(inp);
|
||||
|
||||
if (window.length === 3) {
|
||||
window.shift();
|
||||
}
|
||||
|
||||
window.push(measure);
|
||||
|
||||
let measureSum = window.reduce((sum: number, current: number) => sum + current, 0);
|
||||
|
||||
if (measureSum > prev && window.length === 3) count++;
|
||||
prev = measureSum;
|
||||
})
|
||||
|
||||
console.log(count);
|
||||
2000
src/2021/day1/input.txt
Normal file
2000
src/2021/day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
src/2021/day2/example.txt
Normal file
6
src/2021/day2/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
||||
56
src/2021/day2/index.ts
Normal file
56
src/2021/day2/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import path from 'path';
|
||||
import {readFile} from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'example.txt')).map(x => x.split(' ')).map((cmd) => ({cmd: cmd[0], val: Number(cmd[1])}))
|
||||
|
||||
let forward = 0;
|
||||
let depth = 0;
|
||||
|
||||
type Entry = {
|
||||
cmd: string,
|
||||
val: number
|
||||
}
|
||||
|
||||
const add = (entry: Entry) => {
|
||||
if (entry.cmd === 'forward') forward += entry.val;
|
||||
else if (entry.cmd === 'down') depth += entry.val;
|
||||
else depth -= entry.val;
|
||||
}
|
||||
|
||||
input.map(add)
|
||||
|
||||
console.log("Part 1", forward * depth)
|
||||
|
||||
forward = 0;
|
||||
depth = 0;
|
||||
let aim = 0;
|
||||
|
||||
const execute = (entry: Entry) => {
|
||||
if (entry.cmd === 'down') aim += entry.val;
|
||||
else if (entry.cmd === 'up') aim -= entry.val;
|
||||
else {
|
||||
forward += entry.val
|
||||
depth += entry.val * aim
|
||||
}
|
||||
}
|
||||
|
||||
input.map(execute)
|
||||
|
||||
console.log("Part 2", forward * depth)
|
||||
|
||||
//console.log(input)
|
||||
const res =
|
||||
input
|
||||
.map(x => x)
|
||||
.push({cmd: 0, val: 0} )
|
||||
|
||||
console.log(res);
|
||||
// .print()
|
||||
// .reduce((prev: any, cur: any, curIdx: number, array: any) => {
|
||||
// console.log(curIdx, cur, prev,);
|
||||
// return {
|
||||
// forward: 0,
|
||||
// depth: 0,
|
||||
// aim: 0
|
||||
// }
|
||||
// })
|
||||
1000
src/2021/day2/input.txt
Normal file
1000
src/2021/day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user