|
| 1 | +import * as fs from "fs" |
| 2 | +import { fileURLToPath } from "url" |
| 3 | + |
| 4 | +const abs = Math.abs |
| 5 | + |
| 6 | +function flip<T>(a: T[][]) { |
| 7 | + const flipped = Array.from(Array(a[0].length)).map(() => Array.from(Array(a.length))) |
| 8 | + for (let x = 0; x < flipped.length; x++) { |
| 9 | + for (let y = 0; y < flipped[0].length; y++) { |
| 10 | + flipped[x][y] = a[y][x] |
| 11 | + } |
| 12 | + } |
| 13 | + return flipped |
| 14 | +} |
| 15 | + |
| 16 | +function print<T>(a: T[][]) { |
| 17 | + for (let y = 0; y < a[0].length; y++) { |
| 18 | + console.log(Array(a.length).fill(0).map((_, x) => a[x][y]).join("")) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function gcd(a: number, b: number) { |
| 23 | + if (a == 0) return abs(b) |
| 24 | + if (b == 0) return abs(a) |
| 25 | + |
| 26 | + let h: number |
| 27 | + do { |
| 28 | + h = a % b |
| 29 | + a = b |
| 30 | + b = h |
| 31 | + } while (b != 0) |
| 32 | + return abs(a) |
| 33 | +} |
| 34 | + |
| 35 | +function cancel(f: [number, number]) { |
| 36 | + let [a, b] = f |
| 37 | + let d: number |
| 38 | + do { |
| 39 | + d = gcd(a, b) |
| 40 | + a /= d |
| 41 | + b /= d |
| 42 | + } while (d !== 1) |
| 43 | + return [a, b] |
| 44 | +} |
| 45 | + |
| 46 | +//const input = ".#....#####...#..\n##...##.#####..##\n##...#...#.#####.\n..#.....X...###..\n..#.#.....#....##" |
| 47 | +const input = fs.readFileSync("input-day10.txt", "utf8") |
| 48 | + |
| 49 | +const image = flip(input |
| 50 | + .split("\n") |
| 51 | + .map(l => l.split(""))) |
| 52 | + |
| 53 | +const w = image.length |
| 54 | +const h = image[0].length |
| 55 | + |
| 56 | +const ds = new Set<string>() |
| 57 | +for (let x = -w + 1; x < w; x++) { |
| 58 | + for (let y = -h + 1; y < h; y++) { |
| 59 | + if (x === 0 && y === 0) { |
| 60 | + continue |
| 61 | + } |
| 62 | + ds.add(JSON.stringify(cancel([x, y]))) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const dirs = Array.from(ds).map(d => JSON.parse(d) as [number, number]) |
| 67 | + .map(d => [...d, Math.atan2(d[1], d[0]) * 180 / Math.PI - 270]) |
| 68 | + .map(d => [d[0], d[1], (2 * 360 + d[2]) % 360]) |
| 69 | + .sort((a, b) => a[2] - b[2]) |
| 70 | + .map(d => [d[0], d[1]] as const) |
| 71 | + |
| 72 | +function shoot(x: number, y: number, d: readonly [number, number]): [number, number] | undefined { |
| 73 | + do { |
| 74 | + x += d[0] |
| 75 | + y += d[1] |
| 76 | + if (x < 0 || x >= w) { |
| 77 | + return undefined |
| 78 | + } |
| 79 | + if (y < 0 || y >= h) { |
| 80 | + return undefined |
| 81 | + } |
| 82 | + if (image[x][y] == "#") { |
| 83 | + image[x][y] = "x" |
| 84 | + return [x, y] |
| 85 | + } |
| 86 | + } while (true) |
| 87 | +} |
| 88 | + |
| 89 | +let p = [17, 23] |
| 90 | +//let p = [8, 3] |
| 91 | +let count = 0 |
| 92 | +let i = 0 |
| 93 | + |
| 94 | +while (true) { |
| 95 | + const d = dirs[i++ % dirs.length] |
| 96 | + const shot = shoot(p[0], p[1], d) |
| 97 | + if (shot) { |
| 98 | + count++ |
| 99 | + print(image) |
| 100 | + } |
| 101 | + if (count == 200) { |
| 102 | + console.log(shot) |
| 103 | + console.log("---") |
| 104 | + break |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments