From 45fca03de9d2ecc2488a24c73990a41d48a23e09 Mon Sep 17 00:00:00 2001 From: Jonas Seiler Date: Tue, 6 Jun 2023 19:42:44 +0200 Subject: [PATCH] add Day5 Part 2 --- Day5/Vents.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/Day5/Vents.ts b/Day5/Vents.ts index 1c83d86..1869696 100644 --- a/Day5/Vents.ts +++ b/Day5/Vents.ts @@ -61,7 +61,75 @@ async function Part1() { } async function Part2() { - + const file = await Deno.open("./Day5/input.txt"); + const lines = readLines(file); + + let maxX = 0; + let maxY = 0; + + const Vents: Vent[] = []; + + for await (const line of lines) { + const [Start,End] = line.split(" -> "); + const [xFrom,yFrom] = Start.split(","); + const [xEnd,yEnd] = End.split(","); + maxX = Math.max(+xFrom,+xEnd,maxX); + maxY = Math.max(+yFrom,+yEnd,+maxY); + const vent = {xFrom: +xFrom, yFrom: +yFrom, xTo: +xEnd, yTo: +yEnd}; + Vents.push(vent); + } + + const field: number[][] = new Array(maxX+1).fill([]); + for (const i in field) { + field[i] = new Array(maxY + 1); + field[i].fill(0); + } + + for (const v of Vents) { + if (v.xFrom == v.xTo) { + for (let i = Math.min(v.yFrom,v.yTo); i <= Math.max(v.yTo,v.yFrom); i++) { + field[v.xFrom][i]++; + } + } else if (v.yFrom == v.yTo) { + for (let i = Math.min(v.xFrom,v.xTo); i <= Math.max(v.xTo,v.xFrom); i++) { + field[i][v.yFrom]++; + } + } else { + if (v.xFrom < v.xTo) { + if (v.yFrom < v.yTo) { + for (let i = 0; i <= v.xTo - v.xFrom; i++) { + field[v.xFrom + i][v.yFrom + i]++; + } + } else { + for (let i = 0; i <= v.xTo - v.xFrom; i++) { + field[v.xFrom + i][v.yFrom - i]++; + } + } + } else { + if (v.yFrom < v.yTo) { + for (let i = 0; i <= v.xFrom - v.xTo; i++) { + field[v.xFrom - i][v.yFrom + i]++; + } + } else { + for (let i = 0; i <= v.xFrom - v.xTo; i++) { + field[v.xFrom - i][v.yFrom - i]++; + } + } + } + } + } + + let overlap = 0; + + for (const i in field) { + for (const j in field[i]) { + if (field[i][j] > 1) { + overlap++; + } + } + } + + return overlap; } console.log("Part1: " + await Part1());