Day 04
This commit is contained in:
parent
3df6d07314
commit
b08f238f2e
3 changed files with 1032 additions and 1 deletions
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);
|
Loading…
Add table
Add a link
Reference in a new issue