day 1
This commit is contained in:
parent
e4cf4670b1
commit
463715f582
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"deno.enable": true
|
||||||
|
}
|
||||||
1000
1/input.txt
Normal file
1000
1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
76
1/task.ts
Normal file
76
1/task.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
export async function solveTask1part1() {
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const inputData = await Deno.readFile('./1/input.txt');
|
||||||
|
const input = decoder.decode(inputData);
|
||||||
|
|
||||||
|
// console.log(input.substring(0,50) + '...');
|
||||||
|
const lines = input.split('\n')
|
||||||
|
.filter( l => l.trim() !== '');
|
||||||
|
console.log('lines', lines.slice(0, 5));
|
||||||
|
|
||||||
|
const lefts = lines.map( line => {
|
||||||
|
const leftStr = line.substring(0, line.indexOf(' ')).trim();
|
||||||
|
const left = parseInt(leftStr);
|
||||||
|
return left
|
||||||
|
}).sort();
|
||||||
|
const rights = lines.map( line => {
|
||||||
|
const rightStr = line.substring(line.indexOf(' ')).trim();
|
||||||
|
const right = parseInt(rightStr);
|
||||||
|
return right
|
||||||
|
}).sort();
|
||||||
|
console.log('lefts', lefts.slice(0, 5));
|
||||||
|
console.log('rights', rights.slice(0, 5));
|
||||||
|
|
||||||
|
const diffs = []
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const left = lefts[i];
|
||||||
|
const right = rights[i];
|
||||||
|
diffs.push(Math.abs(right - left));
|
||||||
|
}
|
||||||
|
|
||||||
|
const diffSum = diffs
|
||||||
|
.reduce( (p, c) => p + c, 0);
|
||||||
|
console.log('diffSum', diffSum);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function solveTask1part2() {
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const inputData = await Deno.readFile('./1/input.txt');
|
||||||
|
const input = decoder.decode(inputData);
|
||||||
|
|
||||||
|
const lines = input.split('\n')
|
||||||
|
.filter( l => l.trim() !== '');
|
||||||
|
console.log('lines', lines.slice(0, 5));
|
||||||
|
|
||||||
|
const lefts = lines.map( line => {
|
||||||
|
const leftStr = line.substring(0, line.indexOf(' ')).trim();
|
||||||
|
const left = parseInt(leftStr);
|
||||||
|
return left
|
||||||
|
}).sort();
|
||||||
|
const rights = lines.map( line => {
|
||||||
|
const rightStr = line.substring(line.indexOf(' ')).trim();
|
||||||
|
const right = parseInt(rightStr);
|
||||||
|
return right
|
||||||
|
}).sort();
|
||||||
|
console.log('lefts', lefts.slice(0, 5));
|
||||||
|
console.log('rights', rights.slice(0, 5));
|
||||||
|
|
||||||
|
const scores = []
|
||||||
|
let iR = 0;
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const left = lefts[i];
|
||||||
|
let multiplyer = 0;
|
||||||
|
while (rights[iR] < left) {
|
||||||
|
iR++;
|
||||||
|
}
|
||||||
|
while (rights[iR] === left) {
|
||||||
|
multiplyer++;
|
||||||
|
iR++;
|
||||||
|
}
|
||||||
|
scores.push(left * multiplyer);
|
||||||
|
}
|
||||||
|
|
||||||
|
const scoreSum = scores
|
||||||
|
.reduce( (p, c) => p + c, 0);
|
||||||
|
console.log('scoreSum', scoreSum);
|
||||||
|
}
|
||||||
8
deno.json
Normal file
8
deno.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"tasks": {
|
||||||
|
"dev": "deno run --watch main.ts"
|
||||||
|
},
|
||||||
|
"imports": {
|
||||||
|
"@std/assert": "jsr:@std/assert@1"
|
||||||
|
}
|
||||||
|
}
|
||||||
23
deno.lock
generated
Normal file
23
deno.lock
generated
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"version": "4",
|
||||||
|
"specifiers": {
|
||||||
|
"jsr:@std/assert@1": "1.0.9",
|
||||||
|
"jsr:@std/internal@^1.0.5": "1.0.5"
|
||||||
|
},
|
||||||
|
"jsr": {
|
||||||
|
"@std/assert@1.0.9": {
|
||||||
|
"integrity": "a9f0c611a869cc791b26f523eec54c7e187aab7932c2c8e8bea0622d13680dcd",
|
||||||
|
"dependencies": [
|
||||||
|
"jsr:@std/internal"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"@std/internal@1.0.5": {
|
||||||
|
"integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"workspace": {
|
||||||
|
"dependencies": [
|
||||||
|
"jsr:@std/assert@1"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
4
main.ts
Normal file
4
main.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import { solveTask1part1, solveTask1part2 } from "./1/task.ts";
|
||||||
|
|
||||||
|
await solveTask1part1();
|
||||||
|
await solveTask1part2();
|
||||||
1
main_test.ts
Normal file
1
main_test.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
import { assertEquals } from "@std/assert";
|
||||||
Loading…
x
Reference in New Issue
Block a user