|
| 1 | +const log = console.log; |
| 2 | + |
| 3 | +const useExample = 1; |
| 4 | + |
| 5 | +const filename = useExample ? "example.txt" : "input.txt"; |
| 6 | +const data = Deno.readTextFileSync(filename); |
| 7 | + |
| 8 | +// "0,9 -> 5,9" |
| 9 | +// "8,0 -> 0,8" |
| 10 | +const vents = data.split("\n").map((l) => |
| 11 | + l.split(" -> ").map((r) => r.split(",")).map((xy) => ({ |
| 12 | + x: parseInt(xy[0]), |
| 13 | + y: parseInt(xy[1]), |
| 14 | + })) |
| 15 | +).map((v) => ({ start: v[0], stop: v[1] })); |
| 16 | + |
| 17 | +const ventsStraight = vents.filter(isStraightLine); |
| 18 | + |
| 19 | +type Coord = { x: number; y: number }; |
| 20 | +type Vent = { start: Coord; stop: Coord }; |
| 21 | + |
| 22 | +const ventsMaxX = Math.max( |
| 23 | + ...vents.flat().map((v) => [v.start.x, v.stop.x]).flat(), |
| 24 | +); |
| 25 | +const ventsMaxY = Math.max( |
| 26 | + ...vents.flat().map((v) => [v.start.y, v.stop.y]).flat(), |
| 27 | +); |
| 28 | + |
| 29 | +const size = Math.max(ventsMaxX, ventsMaxY) + 1; |
| 30 | + |
| 31 | +const scoresStraight = array0toN(size).map((y) => |
| 32 | + array0toN(size).map((x) => scorePoint(x, y, ventsStraight)) |
| 33 | +); |
| 34 | +if (useExample) log(prettyFormat(scoresStraight)); |
| 35 | + |
| 36 | +const totalScoreStraight = scoresStraight.flat().filter((s) => s >= 2).length; |
| 37 | + |
| 38 | +console.info( |
| 39 | + "part1\t", |
| 40 | + totalScoreStraight, |
| 41 | +); // 5, 6283 |
| 42 | + |
| 43 | +const scores = array0toN(size).map((y) => |
| 44 | + array0toN(size).map((x) => scorePoint(x, y, vents)) |
| 45 | +); |
| 46 | +if (useExample) log(prettyFormat(scores)); |
| 47 | + |
| 48 | +const totalScore = scores.flat().filter((s) => s >= 2).length; |
| 49 | + |
| 50 | +console.info( |
| 51 | + "part2\t", |
| 52 | + totalScore, |
| 53 | +); // 12, 18864 |
| 54 | + |
| 55 | +function scorePoint( |
| 56 | + x: number, |
| 57 | + y: number, |
| 58 | + vents: Vent[], |
| 59 | + shouldLog = false, |
| 60 | +): number { |
| 61 | + return vents.filter((v) => pointIsOnVent(x, y, v, shouldLog)).length; |
| 62 | +} |
| 63 | + |
| 64 | +function pointIsOnVent( |
| 65 | + x: number, |
| 66 | + y: number, |
| 67 | + v: Vent, |
| 68 | + shouldLog = false, |
| 69 | +): boolean { |
| 70 | + const x1 = v.start.x; |
| 71 | + const x2 = v.stop.x; |
| 72 | + const y1 = v.start.y; |
| 73 | + const y2 = v.stop.y; |
| 74 | + |
| 75 | + const lx = Math.abs(x1 - x2); |
| 76 | + const ly = Math.abs(y1 - y2); |
| 77 | + |
| 78 | + const minX = Math.min(v.start.x, v.stop.x); |
| 79 | + const minY = Math.min(v.start.y, v.stop.y); |
| 80 | + const maxX = Math.max(v.start.x, v.stop.x); |
| 81 | + const maxY = Math.max(v.start.y, v.stop.y); |
| 82 | + |
| 83 | + if (lx === 0) { |
| 84 | + // vertical |
| 85 | + return x === x1 && minY <= y && y <= maxY; |
| 86 | + } else if (ly === 0) { |
| 87 | + // horizontal |
| 88 | + return y === y1 && minX <= x && x <= maxX; |
| 89 | + } else { |
| 90 | + // diagonal |
| 91 | + if (lx !== ly) throw "Not 45 deg"; |
| 92 | + |
| 93 | + if (!(between(x1, x, x2) && between(y1, y, y2))) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + const dx = x - x1; |
| 98 | + const dy = y - y1; |
| 99 | + |
| 100 | + const rx = x2 > x1 ? 1 : -1; |
| 101 | + const ry = y2 > y1 ? 1 : -1; |
| 102 | + |
| 103 | + return (dx * rx === dy * ry); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function between(a: number, b: number, c: number): boolean { |
| 108 | + return (a <= b && b <= c) || (a >= b && b >= c); |
| 109 | +} |
| 110 | + |
| 111 | +function isStraightLine(v: Vent): boolean { |
| 112 | + return v.start.x == v.stop.x || v.start.y == v.stop.y; |
| 113 | +} |
| 114 | + |
| 115 | +import { red } from "https://deno.land/[email protected]/fmt/colors.ts"; |
| 116 | + |
| 117 | +function prettyFormat(scores: number[][]) { |
| 118 | + return scores.map((line) => |
| 119 | + line.map((p: number) => |
| 120 | + p == 0 ? "." : (p >= 2 ? red(p.toString()) : p.toString()) |
| 121 | + ).join("") |
| 122 | + ).join("\n"); |
| 123 | +} |
| 124 | + |
| 125 | +function array0toN(n: number) { |
| 126 | + return [...Array(n).keys()]; |
| 127 | +} |
| 128 | + |
| 129 | +// Från Johan Andersson |
| 130 | +// |
| 131 | +// (defun in-line? (p from to) |
| 132 | +// (destructuring-bind ((px py) (fx fy) (tx ty)) (list p from to) |
| 133 | +// (and (or (= fx tx) |
| 134 | +// (= fy ty) |
| 135 | +// (= (- fx fy) (- tx ty))) |
| 136 | +// (<= (min fx tx) px (max fx tx)) |
| 137 | +// (<= (min fy ty) py (max fy ty))))) |
| 138 | + |
| 139 | +// DAY05> (in-line? '(2 3) '(2 3) '(3 4)) |
| 140 | +// T |
| 141 | +// DAY05> (in-line? '(2 3) '(1 3) '(3 4)) |
| 142 | +// NIL |
| 143 | +// DAY05> (in-line? '(3 0) '(2 0) '(4 0)) |
| 144 | +// T |
| 145 | +// DAY05> (in-line? '(3 1) '(2 0) '(4 0)) |
| 146 | +// NIL |
0 commit comments