nieuw jaar
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import * as fs from 'fs';
|
||||
import path from 'path';
|
||||
import {readFile} from "../utils";
|
||||
import {readFile} from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||
|
||||
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
|
||||
// }
|
||||
// })
|
||||
14
src/2022/day1/example.txt
Normal file
14
src/2022/day1/example.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
17
src/2022/day1/index.ts
Normal file
17
src/2022/day1/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as fs from 'fs';
|
||||
import path from 'path';
|
||||
import {readFile} from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||
|
||||
const ans = Math.max(
|
||||
...input
|
||||
.split("\n\n")
|
||||
.map(a => a.split("\n"))
|
||||
.map(buck => buck
|
||||
.map(Number)
|
||||
.reduce((a, b) => a + b), 0)
|
||||
)
|
||||
|
||||
console.log(ans);
|
||||
|
||||
2242
src/2022/day1/input.txt
Normal file
2242
src/2022/day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,34 +0,0 @@
|
||||
import path from 'path';
|
||||
import {readFile} from "../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt')).map((x: any) => x.split(' '));
|
||||
|
||||
let forward = 0;
|
||||
let depth = 0;
|
||||
|
||||
const add = (command: any) => {
|
||||
if (command[0] === 'forward') forward += Number(command[1]);
|
||||
else if (command[0] === 'down') depth += Number(command[1]);
|
||||
else depth -= Number(command[1]);
|
||||
}
|
||||
|
||||
input.map(add)
|
||||
|
||||
console.log("Part 1", forward * depth)
|
||||
|
||||
forward = 0;
|
||||
depth = 0;
|
||||
let aim = 0;
|
||||
|
||||
const execute = (command: any) => {
|
||||
if (command[0] === 'down') aim += Number(command[1]);
|
||||
else if (command[0] === 'up') aim -= Number(command[1]);
|
||||
else {
|
||||
forward += Number(command[1])
|
||||
depth += Number(command[1]) * aim
|
||||
}
|
||||
}
|
||||
|
||||
input.map(execute)
|
||||
|
||||
console.log("Part 2", forward * depth)
|
||||
@@ -1,5 +1,9 @@
|
||||
import * as fs from 'fs'
|
||||
|
||||
export const readFile = (file: string): any => {
|
||||
return fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n");
|
||||
}
|
||||
return fs.readFileSync(file, {encoding: "utf-8"});
|
||||
}
|
||||
|
||||
export const print = (array: any[]) => {
|
||||
array.forEach(x => console.log(x));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user