|
| 1 | +import {fhmain} from "../../../src/fheader"; |
| 2 | +fhmain(__filename); |
| 3 | +/* |
| 4 | +input: string, lines: string[], dblines: string[][] |
| 5 | +copy(text: string) → clipboard |
| 6 | +error(message: string) → thrown error |
| 7 | +-5..mod(3) → @mod(-5, 3) |
| 8 | +*/ |
| 9 | + |
| 10 | +// Cardinals: |
| 11 | +// [[1,0],[-1,0],[0,1],[0,-1]] |
| 12 | +// +Diagonals: |
| 13 | +// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]] |
| 14 | + |
| 15 | +export {}; |
| 16 | + |
| 17 | +const practice = ` |
| 18 | +6,10 |
| 19 | +0,14 |
| 20 | +9,10 |
| 21 | +0,3 |
| 22 | +10,4 |
| 23 | +4,11 |
| 24 | +6,0 |
| 25 | +6,12 |
| 26 | +4,1 |
| 27 | +0,13 |
| 28 | +10,12 |
| 29 | +3,4 |
| 30 | +3,0 |
| 31 | +8,4 |
| 32 | +1,10 |
| 33 | +2,14 |
| 34 | +8,10 |
| 35 | +9,0 |
| 36 | +
|
| 37 | +fold along y=7 |
| 38 | +fold along x=5 |
| 39 | +`; |
| 40 | +// input = practice; |
| 41 | +input = input.trim(); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +const [dots, folds] = input.split("\n\n"); |
| 47 | + |
| 48 | +const dts = dots.split("\n").map(v => v.split(",").map(Number)); |
| 49 | + |
| 50 | +let board = makeBoard("."); |
| 51 | + |
| 52 | +for(const dot of dts) { |
| 53 | + board.set(dot, "#"); |
| 54 | +} |
| 55 | + |
| 56 | +const firstfold = folds.split("\n").map(fold => fold.split(" ")[2].split("=")); |
| 57 | + |
| 58 | +const res = new Set(); |
| 59 | + |
| 60 | +function applyfold(fold) { |
| 61 | + const newboard = makeBoard("."); |
| 62 | + |
| 63 | + const m = fold[0] === "x" ? 0 : 1; |
| 64 | + const q = m === 0 ? [+fold[1], 0] : [0, +fold[1]]; |
| 65 | + console.log(fold, m, q, +firstfold[1], firstfold[1]); |
| 66 | + |
| 67 | + board.forEach((tile, pos) => { |
| 68 | + if(tile === "#") { |
| 69 | + if(pos[m] > +fold[1]) { |
| 70 | + newboard.set( |
| 71 | + pos.sub(q) |
| 72 | + .dwth(v => v[m] = -v[m]) |
| 73 | + .add(q), |
| 74 | + "#", |
| 75 | + ); |
| 76 | + }else{ |
| 77 | + newboard.set(pos, "#"); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + board = newboard; |
| 83 | +} |
| 84 | + |
| 85 | +// board.print().dwth(log); |
| 86 | +for(const fold of firstfold) { |
| 87 | + applyfold(fold); |
| 88 | +} |
| 89 | +board.print().dwth(log); |
| 90 | + |
| 91 | +let count = 0; |
| 92 | +board.forEach((itm) => { |
| 93 | + if(itm === "#") { |
| 94 | + count++; |
| 95 | + } |
| 96 | +}); |
| 97 | +count.dwth(log); |
| 98 | +// expect= |
| 99 | +/* |
| 100 | + .-------------------------------------------. -1 .. 6 |
| 101 | +-1 | ......................................... | |
| 102 | + 0 | ..##...##..####...##.#..#.####..##..#..#. | |
| 103 | + 1 | .#..#.#..#.#.......#.#..#....#.#..#.#.#.. | |
| 104 | + 2 | .#....#..#.###.....#.####...#..#....##... | |
| 105 | + 3 | .#....####.#.......#.#..#..#...#....#.#.. | |
| 106 | + 4 | .#..#.#..#.#....#..#.#..#.#....#..#.#.#.. | |
| 107 | + 5 | ..##..#..#.#.....##..#..#.####..##..#..#. | |
| 108 | + 6 | ......................................... | |
| 109 | + '-------------------------------------------' |
| 110 | +*/ |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +type nobi = number | bigint; |
| 117 | +type Board<T> = { |
| 118 | + get(pos: Point2D): T; |
| 119 | + set(pos: Point2D, t: T): void; |
| 120 | + clear(): void; |
| 121 | + forEach(visitor: (v: T, pos: Point2D) => void): void; |
| 122 | + print(printer?: (v: T, pos: Point2D) => string | nobi): string; |
| 123 | + copy(): Board<T>; |
| 124 | +}; |
| 125 | +function makeBoard<T>(fill: T): Board<T> { |
| 126 | + // it would be useful if board could center at 0,0 and expand infinitely |
| 127 | + let board: T[][] = []; |
| 128 | + let limits: |
| 129 | + | { min: Point2D, max: Point2D } |
| 130 | + | undefined; |
| 131 | + let reso: Board<T> = { |
| 132 | + clear: () => { |
| 133 | + board = []; |
| 134 | + }, |
| 135 | + get: (pos) => { |
| 136 | + if (!limits) return fill; |
| 137 | + if ( |
| 138 | + pos.op(limits.min, (a, b) => a < b).some(w => w) || |
| 139 | + pos.op(limits.max, (a, b) => a > b).some(w => w) |
| 140 | + ) return fill; |
| 141 | + if (!board[pos.y]) return fill; |
| 142 | + let bval = board[pos.y][pos.x]; |
| 143 | + return bval === undefined ? fill : bval; |
| 144 | + }, |
| 145 | + set: (pos, v) => { |
| 146 | + if (!limits) { |
| 147 | + limits = {min: dupe(pos), max: dupe(pos)}; |
| 148 | + } |
| 149 | + limits.min = pos.op(limits.min, (a, b) => Math.min(a, b)); |
| 150 | + limits.max = pos.op(limits.max, (a, b) => Math.max(a, b)); |
| 151 | + if (!board[pos.y]) board[pos.y] = []; |
| 152 | + board[pos.y][pos.x] = v; |
| 153 | + }, |
| 154 | + forEach: visitor => { |
| 155 | + if (!limits) return; |
| 156 | + for (let y = limits.min.y; y <= limits.max.y; y++) { |
| 157 | + for (let x = limits.min.x; x <= limits.max.x; x++) { |
| 158 | + visitor(reso.get([x, y]), [x, y]); |
| 159 | + } |
| 160 | + } |
| 161 | + }, |
| 162 | + copy: () => { |
| 163 | + let nb = makeBoard<T>(fill); |
| 164 | + reso.forEach((v, pos) => nb.set(pos, v)); |
| 165 | + return nb; |
| 166 | + }, |
| 167 | + print: (printer = v => v as any): string => { |
| 168 | + // ratelimit print |
| 169 | + if (!limits) return "*no board to print*"; |
| 170 | + let ylength = 0; |
| 171 | + for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) { |
| 172 | + ylength = Math.max(y.toString().length, ylength); |
| 173 | + } |
| 174 | + const resc: string[] = []; |
| 175 | + let llen: number = limits.max.x - limits.min.x + 3; |
| 176 | + for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) { |
| 177 | + let line = ""; |
| 178 | + for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) { |
| 179 | + line += printer(reso.get([x, y]), [x, y]); |
| 180 | + } |
| 181 | + if(line.length > llen) llen = line.length; |
| 182 | + resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |"); |
| 183 | + } |
| 184 | + resc.unshift( |
| 185 | + " ".repeat(ylength) + |
| 186 | + " .-" + |
| 187 | + "-".repeat(llen) + |
| 188 | + "-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1), |
| 189 | + ); |
| 190 | + resc.push( |
| 191 | + " ".repeat(ylength) + |
| 192 | + " '-" + |
| 193 | + "-".repeat(llen) + |
| 194 | + "-'", |
| 195 | + ); |
| 196 | + return resc.join("\n"); |
| 197 | + }, |
| 198 | + }; |
| 199 | + return reso; |
| 200 | +} |
0 commit comments