Skip to content

Commit

Permalink
day-17 (ts) wip
Browse files Browse the repository at this point in the history
  • Loading branch information
memark committed Nov 30, 2021
1 parent f25c5c2 commit 8ed0eca
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
64 changes: 64 additions & 0 deletions day-17/v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var sample_1 = "\n.#.\n..#\n###\n";
var data = sample_1;
var d = data.split("\n").map(function (s) { return s.split(""); });
function range(from, to) {
return Array.from(Array(Math.floor(to - from))).map(function (v, k) { return from + k; });
}
var actives = new Set();
for (var _i = 0, _a = range(0, d.length); _i < _a.length; _i++) {
var y = _a[_i];
for (var _b = 0, _c = range(0, d[y].length); _b < _c.length; _b++) {
var x = _c[_b];
if (d[y][x] == "#") {
actives.add({ x: x, y: y, z: 0, w: 0 });
}
}
}
console.log(actives);
var numCycles = 6;
var res = recurse(actives, numCycles);
console.log(res.size);
function recurse(actives, maxCycles, cycle) {
if (cycle === void 0) { cycle = 1; }
if (cycle > maxCycles)
return actives;
// coordsToCheck = {aa for a in actives for aa in [a, *getNeighbours(a)]}
// const coordsToCheck =
// newActives = {c for c in coordsToCheck if getNewState(c, actives)}
// return recurse(newActives, maxCycles, cycle+1)
}
function getNewState(coord, actives) {
var activeNeighbours = 0;
for (var _i = 0, _a = getNeighbours(coord); _i < _a.length; _i++) {
var n = _a[_i];
if (actives.has(n)) {
activeNeighbours++;
}
}
// If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active.
// Otherwise, the cube becomes inactive.
if (actives.has(coord))
return [2, 3].includes(activeNeighbours);
// If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active.
// Otherwise, the cube remains inactive.
else
return [3].includes(activeNeighbours);
}
function getNeighbours(coord) {
var res = new Set();
for (var _i = 0, _a = range(coord.w - 1, coord.w + 1 + 1); _i < _a.length; _i++) {
var w = _a[_i];
for (var _b = 0, _c = range(coord.z - 1, coord.z + 1 + 1); _b < _c.length; _b++) {
var z = _c[_b];
for (var _d = 0, _e = range(coord.y - 1, coord.y + 1 + 1); _d < _e.length; _d++) {
var y = _e[_d];
for (var _f = 0, _g = range(coord.x - 1, coord.x + 1 + 1); _f < _g.length; _f++) {
var x = _g[_f];
res.add({ x: x, y: y, z: z, w: w });
}
}
}
}
res["delete"](coord);
return res;
}
85 changes: 85 additions & 0 deletions day-17/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const sample_1 = `
.#.
..#
###
`;

const data = sample_1;

const d = data.split("\n").map((s) => s.split(""));

function range(from: number, to: number): number[] {
return Array.from(Array(Math.floor(to - from))).map((v, k) => from + k);
}

const actives = new Set<Coord>();

for (const y of range(0, d.length)) {
for (const x of range(0, d[y].length)) {
if (d[y][x] == "#") {
actives.add({ x, y, z: 0, w: 0 });
}
}
}

console.log(actives);

const numCycles = 1;

const res = recurse(actives, numCycles);

console.log(res.size);

type Coord = { x: number; y: number; z: number; w: number };

function recurse(actives: Set<Coord>, maxCycles: number, cycle: number = 1) {
console.log("actives", actives);
if (cycle > maxCycles) return actives;

const coordsToCheck = new Set<Coord>();
for (const a of Array.from(actives))
for (const aa of [a, ...Array.from(getNeighbours(a))])
coordsToCheck.add(aa);
// console.log("coordsToCheck", coordsToCheck);

const newActives = new Set<Coord>();
for (const c of Array.from(coordsToCheck))
if (getNewState(c, actives)) newActives.add(c);
console.log("newActives", newActives);

return recurse(newActives, maxCycles, cycle + 1);
}

function getNewState(coord: Coord, actives: Set<Coord>) {
let activeNeighbours = 0;
for (const n of Array.from(getNeighbours(coord))) {
if (actives.has(n)) {
if (coord.z == 0 && coord.w == 0) console.log(coord, activeNeighbours);
activeNeighbours++;
}
}

// if (coord.z == 0 && coord.w == 0) console.log(coord, activeNeighbours);

// If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active.
// Otherwise, the cube becomes inactive.
if (actives.has(coord)) return [2, 3].includes(activeNeighbours);
// If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active.
// Otherwise, the cube remains inactive.
else return [3].includes(activeNeighbours);
}

function getNeighbours(coord: Coord) {
const res = new Set<Coord>();
for (const w of range(coord.w - 1, coord.w + 1 + 1))
for (const z of range(coord.z - 1, coord.z + 1 + 1))
for (const y of range(coord.y - 1, coord.y + 1 + 1))
for (const x of range(coord.x - 1, coord.x + 1 + 1))
res.add({ x, y, z, w });
res.delete(coord);
return res;
}

const a = [1, 2];
const b = [1, 2];
console.log(a == b);
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"license": "MIT",
"dependencies": {
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"compilerOptions": { "lib": ["dom", "ESNext"]}}
63 changes: 63 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

ts-node@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"

typescript@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==

[email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 comments on commit 8ed0eca

Please sign in to comment.