Day 04
This commit is contained in:
parent
3df6d07314
commit
b08f238f2e
3 changed files with 1032 additions and 1 deletions
|
@ -27,6 +27,7 @@
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"Day01": "cd ./src/01_Calories && deno run --allow-read=./input.txt Calories.ts",
|
"Day01": "cd ./src/01_Calories && deno run --allow-read=./input.txt Calories.ts",
|
||||||
"Day02": "cd ./src/02_RockPaperScissors && deno run --allow-read=./input.txt RockPaperScissors.ts",
|
"Day02": "cd ./src/02_RockPaperScissors && deno run --allow-read=./input.txt RockPaperScissors.ts",
|
||||||
"Day03": "cd ./src/03_RucksackReorganization && deno run --allow-read=./input.txt RucksackReorganization.ts"
|
"Day03": "cd ./src/03_RucksackReorganization && deno run --allow-read=./input.txt RucksackReorganization.ts",
|
||||||
|
"Day04": "cd ./src/04_CampCleanup && deno run --allow-read=./input.txt CampCleanup.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
src/04_CampCleanup/CampCleanup.ts
Normal file
30
src/04_CampCleanup/CampCleanup.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
const input = Deno.readTextFileSync('./input.txt').trimEnd().split('\n');
|
||||||
|
|
||||||
|
function includes(left: [number, number], right: [number, number]): number {
|
||||||
|
if (
|
||||||
|
(left[0] <= right[0] && left[1] >= right[1]) ||
|
||||||
|
(left[0] >= right[0] && left[1] <= right[1])
|
||||||
|
) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function overlap(left: [number, number], right: [number, number]): number {
|
||||||
|
if (left[0] > right[1] || left[1] < right[0]) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let includeScore = 0;
|
||||||
|
let overlapScore = 0;
|
||||||
|
for (const line of input) {
|
||||||
|
const sections = line.match(/^(\d+)-(\d+),(\d+)-(\d+)$/)!.splice(1);
|
||||||
|
const [startLeft, endLeft, startRight, endRight] = sections.map((id) => {
|
||||||
|
return parseInt(id);
|
||||||
|
}) as [number, number, number, number];
|
||||||
|
includeScore += includes([startLeft, endLeft], [startRight, endRight]);
|
||||||
|
overlapScore += overlap([startLeft, endLeft], [startRight, endRight]);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(includeScore);
|
||||||
|
console.log(overlapScore);
|
1000
src/04_CampCleanup/input.txt
Normal file
1000
src/04_CampCleanup/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue