|
| 1 | +const sample_1 = ` |
| 2 | +.#. |
| 3 | +..# |
| 4 | +### |
| 5 | +`; |
| 6 | + |
| 7 | +const data = sample_1; |
| 8 | + |
| 9 | +const d = data.split("\n").map((s) => s.split("")); |
| 10 | + |
| 11 | +function range(from: number, to: number): number[] { |
| 12 | + return Array.from(Array(Math.floor(to - from))).map((v, k) => from + k); |
| 13 | +} |
| 14 | + |
| 15 | +const actives = new Set<Coord>(); |
| 16 | + |
| 17 | +for (const y of range(0, d.length)) { |
| 18 | + for (const x of range(0, d[y].length)) { |
| 19 | + if (d[y][x] == "#") { |
| 20 | + actives.add({ x, y, z: 0, w: 0 }); |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +console.log(actives); |
| 26 | + |
| 27 | +const numCycles = 1; |
| 28 | + |
| 29 | +const res = recurse(actives, numCycles); |
| 30 | + |
| 31 | +console.log(res.size); |
| 32 | + |
| 33 | +type Coord = { x: number; y: number; z: number; w: number }; |
| 34 | + |
| 35 | +function recurse(actives: Set<Coord>, maxCycles: number, cycle: number = 1) { |
| 36 | + console.log("actives", actives); |
| 37 | + if (cycle > maxCycles) return actives; |
| 38 | + |
| 39 | + const coordsToCheck = new Set<Coord>(); |
| 40 | + for (const a of Array.from(actives)) |
| 41 | + for (const aa of [a, ...Array.from(getNeighbours(a))]) |
| 42 | + coordsToCheck.add(aa); |
| 43 | + // console.log("coordsToCheck", coordsToCheck); |
| 44 | + |
| 45 | + const newActives = new Set<Coord>(); |
| 46 | + for (const c of Array.from(coordsToCheck)) |
| 47 | + if (getNewState(c, actives)) newActives.add(c); |
| 48 | + console.log("newActives", newActives); |
| 49 | + |
| 50 | + return recurse(newActives, maxCycles, cycle + 1); |
| 51 | +} |
| 52 | + |
| 53 | +function getNewState(coord: Coord, actives: Set<Coord>) { |
| 54 | + let activeNeighbours = 0; |
| 55 | + for (const n of Array.from(getNeighbours(coord))) { |
| 56 | + if (actives.has(n)) { |
| 57 | + if (coord.z == 0 && coord.w == 0) console.log(coord, activeNeighbours); |
| 58 | + activeNeighbours++; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // if (coord.z == 0 && coord.w == 0) console.log(coord, activeNeighbours); |
| 63 | + |
| 64 | + // If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active. |
| 65 | + // Otherwise, the cube becomes inactive. |
| 66 | + if (actives.has(coord)) return [2, 3].includes(activeNeighbours); |
| 67 | + // If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active. |
| 68 | + // Otherwise, the cube remains inactive. |
| 69 | + else return [3].includes(activeNeighbours); |
| 70 | +} |
| 71 | + |
| 72 | +function getNeighbours(coord: Coord) { |
| 73 | + const res = new Set<Coord>(); |
| 74 | + for (const w of range(coord.w - 1, coord.w + 1 + 1)) |
| 75 | + for (const z of range(coord.z - 1, coord.z + 1 + 1)) |
| 76 | + for (const y of range(coord.y - 1, coord.y + 1 + 1)) |
| 77 | + for (const x of range(coord.x - 1, coord.x + 1 + 1)) |
| 78 | + res.add({ x, y, z, w }); |
| 79 | + res.delete(coord); |
| 80 | + return res; |
| 81 | +} |
| 82 | + |
| 83 | +const a = [1, 2]; |
| 84 | +const b = [1, 2]; |
| 85 | +console.log(a == b); |
0 commit comments