Skip to content

Commit 8ed0eca

Browse files
committed
day-17 (ts) wip
1 parent f25c5c2 commit 8ed0eca

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

day-17/v1.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var sample_1 = "\n.#.\n..#\n###\n";
2+
var data = sample_1;
3+
var d = data.split("\n").map(function (s) { return s.split(""); });
4+
function range(from, to) {
5+
return Array.from(Array(Math.floor(to - from))).map(function (v, k) { return from + k; });
6+
}
7+
var actives = new Set();
8+
for (var _i = 0, _a = range(0, d.length); _i < _a.length; _i++) {
9+
var y = _a[_i];
10+
for (var _b = 0, _c = range(0, d[y].length); _b < _c.length; _b++) {
11+
var x = _c[_b];
12+
if (d[y][x] == "#") {
13+
actives.add({ x: x, y: y, z: 0, w: 0 });
14+
}
15+
}
16+
}
17+
console.log(actives);
18+
var numCycles = 6;
19+
var res = recurse(actives, numCycles);
20+
console.log(res.size);
21+
function recurse(actives, maxCycles, cycle) {
22+
if (cycle === void 0) { cycle = 1; }
23+
if (cycle > maxCycles)
24+
return actives;
25+
// coordsToCheck = {aa for a in actives for aa in [a, *getNeighbours(a)]}
26+
// const coordsToCheck =
27+
// newActives = {c for c in coordsToCheck if getNewState(c, actives)}
28+
// return recurse(newActives, maxCycles, cycle+1)
29+
}
30+
function getNewState(coord, actives) {
31+
var activeNeighbours = 0;
32+
for (var _i = 0, _a = getNeighbours(coord); _i < _a.length; _i++) {
33+
var n = _a[_i];
34+
if (actives.has(n)) {
35+
activeNeighbours++;
36+
}
37+
}
38+
// If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active.
39+
// Otherwise, the cube becomes inactive.
40+
if (actives.has(coord))
41+
return [2, 3].includes(activeNeighbours);
42+
// If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active.
43+
// Otherwise, the cube remains inactive.
44+
else
45+
return [3].includes(activeNeighbours);
46+
}
47+
function getNeighbours(coord) {
48+
var res = new Set();
49+
for (var _i = 0, _a = range(coord.w - 1, coord.w + 1 + 1); _i < _a.length; _i++) {
50+
var w = _a[_i];
51+
for (var _b = 0, _c = range(coord.z - 1, coord.z + 1 + 1); _b < _c.length; _b++) {
52+
var z = _c[_b];
53+
for (var _d = 0, _e = range(coord.y - 1, coord.y + 1 + 1); _d < _e.length; _d++) {
54+
var y = _e[_d];
55+
for (var _f = 0, _g = range(coord.x - 1, coord.x + 1 + 1); _f < _g.length; _f++) {
56+
var x = _g[_f];
57+
res.add({ x: x, y: y, z: z, w: w });
58+
}
59+
}
60+
}
61+
}
62+
res["delete"](coord);
63+
return res;
64+
}

day-17/v1.ts

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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);

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"license": "MIT",
3+
"dependencies": {
4+
"ts-node": "^9.1.1",
5+
"typescript": "^4.1.3"
6+
}
7+
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"compilerOptions": { "lib": ["dom", "ESNext"]}}

yarn.lock

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
arg@^4.1.0:
6+
version "4.1.3"
7+
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
8+
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
9+
10+
buffer-from@^1.0.0:
11+
version "1.1.1"
12+
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
13+
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
14+
15+
create-require@^1.1.0:
16+
version "1.1.1"
17+
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
18+
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
19+
20+
diff@^4.0.1:
21+
version "4.0.2"
22+
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
23+
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
24+
25+
make-error@^1.1.1:
26+
version "1.3.6"
27+
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
28+
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
29+
30+
source-map-support@^0.5.17:
31+
version "0.5.19"
32+
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
33+
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
34+
dependencies:
35+
buffer-from "^1.0.0"
36+
source-map "^0.6.0"
37+
38+
source-map@^0.6.0:
39+
version "0.6.1"
40+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
41+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
42+
43+
ts-node@^9.1.1:
44+
version "9.1.1"
45+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
46+
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
47+
dependencies:
48+
arg "^4.1.0"
49+
create-require "^1.1.0"
50+
diff "^4.0.1"
51+
make-error "^1.1.1"
52+
source-map-support "^0.5.17"
53+
yn "3.1.1"
54+
55+
typescript@^4.1.3:
56+
version "4.1.3"
57+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
58+
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
59+
60+
61+
version "3.1.1"
62+
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
63+
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 commit comments

Comments
 (0)