|
| 1 | +/* |
| 2 | +input: string, lines: string[], dblines: string[][] |
| 3 | +copy(text: string) → clipboard |
| 4 | +error(message: string) → thrown error |
| 5 | +-5..mod(3) → @mod(-5, 3) |
| 6 | +*/ |
| 7 | + |
| 8 | +// Cardinals: |
| 9 | +// [[1,0],[-1,0],[0,1],[0,-1]] |
| 10 | +// +Diagonals: |
| 11 | +// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]] |
| 12 | + |
| 13 | +export {}; |
| 14 | + |
| 15 | +const practice = ` |
| 16 | +0,9 -> 5,9 |
| 17 | +8,0 -> 0,8 |
| 18 | +9,4 -> 3,4 |
| 19 | +2,2 -> 2,1 |
| 20 | +7,0 -> 7,4 |
| 21 | +6,4 -> 2,0 |
| 22 | +0,9 -> 2,9 |
| 23 | +3,4 -> 1,4 |
| 24 | +0,0 -> 8,8 |
| 25 | +5,5 -> 8,2 |
| 26 | +`; |
| 27 | +// input = practice; |
| 28 | + |
| 29 | +function drawLine(start: Point2D, end: Point2D, board: Board<number>) { |
| 30 | + let [x1, y1] = start; |
| 31 | + let [x2, y2] = end; |
| 32 | + const dx = Math.abs(x2 - x1); |
| 33 | + const dy = Math.abs(y2 - y1); |
| 34 | + const sx = x1 < x2 ? 1 : -1; |
| 35 | + const sy = y1 < y2 ? 1 : -1; |
| 36 | + let err = dx - dy; |
| 37 | + |
| 38 | + while (true) { |
| 39 | + board.set([x1, y1], board.get([x1, y1]) + 1); |
| 40 | + if (x1 === x2 && y1 === y2) break; |
| 41 | + const e2 = 2 * err; |
| 42 | + if (e2 > -dy) { |
| 43 | + err -= dy; |
| 44 | + x1 += sx; |
| 45 | + } |
| 46 | + if (e2 < dx) { |
| 47 | + err += dx; |
| 48 | + y1 += sy; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const board = makeBoard(0); |
| 54 | +input.trim().split("\n").forEach(line => { |
| 55 | + let [start, end] = line.split(" -> "); |
| 56 | + let [sx, sy] = start.split(",").map(Number); |
| 57 | + let [ex, ey] = end.split(",").map(Number); |
| 58 | + // if(!(sx == ex || sy == ey)) return;//throw new Error("can only move in one direction"); |
| 59 | + |
| 60 | + // console.log(line); |
| 61 | + drawLine([sx, sy], [ex, ey], board); |
| 62 | +}); |
| 63 | + |
| 64 | +// board.print(v => v === 0 ? "." : "" + v).dwth(log); |
| 65 | + |
| 66 | +let res = 0; |
| 67 | +board.forEach(v => { |
| 68 | + if(v >= 2) res += 1; |
| 69 | +}); |
| 70 | +res.dwth(log); |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +type nobi = number | bigint; |
| 76 | +type Board<T> = { |
| 77 | + get(pos: Point2D): T; |
| 78 | + set(pos: Point2D, t: T): void; |
| 79 | + clear(): void; |
| 80 | + forEach(visitor: (v: T, pos: Point2D) => void): void; |
| 81 | + print(printer?: (v: T, pos: Point2D) => string | nobi): string; |
| 82 | + copy(): Board<T>; |
| 83 | +}; |
| 84 | +function makeBoard<T>(fill: T): Board<T> { |
| 85 | + // it would be useful if board could center at 0,0 and expand infinitely |
| 86 | + let board: T[][] = []; |
| 87 | + let limits: |
| 88 | + | { min: Point2D, max: Point2D } |
| 89 | + | undefined; |
| 90 | + let reso: Board<T> = { |
| 91 | + clear: () => { |
| 92 | + board = []; |
| 93 | + }, |
| 94 | + get: (pos) => { |
| 95 | + if (!limits) return fill; |
| 96 | + if ( |
| 97 | + pos.op(limits.min, (a, b) => a < b).some(w => w) || |
| 98 | + pos.op(limits.max, (a, b) => a > b).some(w => w) |
| 99 | + ) return fill; |
| 100 | + if (!board[pos.y]) return fill; |
| 101 | + let bval = board[pos.y][pos.x]; |
| 102 | + return bval === undefined ? fill : bval; |
| 103 | + }, |
| 104 | + set: (pos, v) => { |
| 105 | + if (!limits) { |
| 106 | + limits = {min: dupe(pos), max: dupe(pos)}; |
| 107 | + } |
| 108 | + limits.min = pos.op(limits.min, (a, b) => Math.min(a, b)); |
| 109 | + limits.max = pos.op(limits.max, (a, b) => Math.max(a, b)); |
| 110 | + if (!board[pos.y]) board[pos.y] = []; |
| 111 | + board[pos.y][pos.x] = v; |
| 112 | + }, |
| 113 | + forEach: visitor => { |
| 114 | + if (!limits) return; |
| 115 | + for (let y = limits.min.y; y <= limits.max.y; y++) { |
| 116 | + for (let x = limits.min.x; x <= limits.max.x; x++) { |
| 117 | + visitor(reso.get([x, y]), [x, y]); |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + copy: () => { |
| 122 | + let nb = makeBoard<T>(fill); |
| 123 | + reso.forEach((v, pos) => nb.set(pos, v)); |
| 124 | + return nb; |
| 125 | + }, |
| 126 | + print: (printer = v => v as any): string => { |
| 127 | + // ratelimit print |
| 128 | + if (!limits) return "*no board to print*"; |
| 129 | + let ylength = 0; |
| 130 | + for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) { |
| 131 | + ylength = Math.max(y.toString().length, ylength); |
| 132 | + } |
| 133 | + const resc: string[] = []; |
| 134 | + let llen: number = limits.max.x - limits.min.x + 3; |
| 135 | + for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) { |
| 136 | + let line = ""; |
| 137 | + for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) { |
| 138 | + line += printer(reso.get([x, y]), [x, y]); |
| 139 | + } |
| 140 | + if(line.length > llen) llen = line.length; |
| 141 | + resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |"); |
| 142 | + } |
| 143 | + resc.unshift( |
| 144 | + " ".repeat(ylength) + |
| 145 | + " .-" + |
| 146 | + "-".repeat(llen) + |
| 147 | + "-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1), |
| 148 | + ); |
| 149 | + resc.push( |
| 150 | + " ".repeat(ylength) + |
| 151 | + " '-" + |
| 152 | + "-".repeat(llen) + |
| 153 | + "-'", |
| 154 | + ); |
| 155 | + return resc.join("\n"); |
| 156 | + }, |
| 157 | + }; |
| 158 | + return reso; |
| 159 | +} |
0 commit comments