Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glitch 14 again #28

Merged
merged 6 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Contributing to aoc2023

Contributions welcome! Feel free to ask @dieseltravis any questions. ☺
Forks welcome! Feel free to ask @dieseltravis any questions. ☺
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "branch-three-oviraptor",
"version": "2023.12.15",
"version": "2023.12.16",
"description": "Travis's Advent of Code 2023",
"author": "Travis Hardiman",
"homepage": "https://branch-three-oviraptor.glitch.me/",
Expand All @@ -14,7 +14,7 @@
},
"dependencies": {
"connect-timeout": "^1.9.0",
"eslint": "^8.55.0",
"eslint": "^8.56.0",
"express": "^4.18.2",
"express-rate-limit": "^7.1.5",
"semistandard": "^17.0.0",
Expand Down
236 changes: 126 additions & 110 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,9 +1306,8 @@
},
part2: (data) => {
const cycles = 1000000000;
// const cycles = 3;
const input = data.trim().split('\n').map(l => {
return l.split('').map(c => {
return l.trim().split('').map(c => {
return {
isEmpty: c === '.',
isCube: c === '#',
Expand All @@ -1323,133 +1322,148 @@
c.load = ymax - y;
});
});
const render = () => input.map(r => r.map(c => c.isRound ? 'O' : c.isCube ? '#' : '.').join('')).join('\n');
console.log(input.slice());
// progress
const pc = cycles / 100;
let p = 0;
let lastGrid = render();
console.log('first grid:\n' + lastGrid);
const render = (arr) => arr.map(r => r.map(c => c.isRound ? 'O' : c.isCube ? '#' : '.').join('')).join('\n');
const getLoad = (arr) => arr.reduce((acc, row) => {
return acc + row.reduce((acc2, c) => {
if (c.isRound) {
acc2 += c.load;
}
return acc2;
}, 0);
}, 0);
const lookup = {};
let id = 0;
const idtokey = {};
const keytoid = {};
const loads = {};
const pattern = [];
for (let i = 0; i < cycles; i++) {
// N
for (let x = 0; x < xmax; x++) {
for (let y = 0; y < ymax; y++) {
if (input[y][x].isRound) {
let newY = y;
let lastY = y;
while (newY > 0) {
newY = newY - 1;
if (input[newY][x].isEmpty) {
lastY = newY;
} else {
break;
let key = '';
let found = false;
let newInput = [];
key = render(input);
newInput = lookup[key];
found = !!newInput;
if (found) {
console.log('repeat at i:', i);
const prem = cycles - i;
const plen = pattern.length;
const pidx = pattern.indexOf(keytoid[key]);
// take the remaining less one, remainder of number of items in array that repeat, offset by index
const presult = ((prem - 1) % (plen - pidx)) + pidx;
console.log(pattern, 'prem', prem, 'plen', plen, 'pidx', pidx, 'plen - pidx', plen - pidx, 'presult', presult);
console.log(Object.values(loads));
const pkey = idtokey[presult];
const result = loads[pkey];
console.log(result);
return result;
} else {
// N
for (let x = 0; x < xmax; x++) {
for (let y = 0; y < ymax; y++) {
if (input[y][x].isRound) {
let newY = y;
let lastY = y;
while (newY > 0) {
newY = newY - 1;
if (input[newY][x].isEmpty) {
lastY = newY;
} else {
break;
}
}
if (lastY !== y) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[lastY][x].isRound = true;
input[lastY][x].isEmpty = false;
}
}
if (lastY !== y) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[lastY][x].isRound = true;
input[lastY][x].isEmpty = false;
}
}
}
}
// console.log('N\n' + render());
// W
for (let y = 0; y < ymax; y++) {
for (let x = 0; x < xmax; x++) {
if (input[y][x].isRound) {
let newX = x;
let lastX = x;
while (newX > 0) {
newX = newX - 1;
if (input[y][newX].isEmpty) {
lastX = newX;
} else {
break;
// W
for (let y = 0; y < ymax; y++) {
for (let x = 0; x < xmax; x++) {
if (input[y][x].isRound) {
let newX = x;
let lastX = x;
while (newX > 0) {
newX = newX - 1;
if (input[y][newX].isEmpty) {
lastX = newX;
} else {
break;
}
}
if (lastX !== x) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[y][lastX].isRound = true;
input[y][lastX].isEmpty = false;
}
}
if (lastX !== x) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[y][lastX].isRound = true;
input[y][lastX].isEmpty = false;
}
}
}
}
// console.log('W\n' + render());
// S
for (let x = 0; x < xmax; x++) {
for (let y = ymax; y--;) {
if (input[y][x].isRound) {
let newY = y;
let lastY = y;
while (newY < ymax - 1) {
newY = newY + 1;
if (input[newY][x].isEmpty) {
lastY = newY;
} else {
break;
// S
for (let x = 0; x < xmax; x++) {
for (let y = ymax; y--;) {
if (input[y][x].isRound) {
let newY = y;
let lastY = y;
while (newY < ymax - 1) {
newY = newY + 1;
if (input[newY][x].isEmpty) {
lastY = newY;
} else {
break;
}
}
if (lastY !== y) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[lastY][x].isRound = true;
input[lastY][x].isEmpty = false;
}
}
if (lastY !== y) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[lastY][x].isRound = true;
input[lastY][x].isEmpty = false;
}
}
}
}
// console.log('S\n' + render());
// E
for (let y = 0; y < ymax; y++) {
for (let x = xmax; x--;) {
if (input[y][x].isRound) {
let newX = x;
let lastX = x;
while (newX < xmax - 1) {
newX = newX + 1;
if (input[y][newX].isEmpty) {
lastX = newX;
} else {
break;
// E
for (let y = 0; y < ymax; y++) {
for (let x = xmax; x--;) {
if (input[y][x].isRound) {
let newX = x;
let lastX = x;
while (newX < xmax - 1) {
newX = newX + 1;
if (input[y][newX].isEmpty) {
lastX = newX;
} else {
break;
}
}
if (lastX !== x) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[y][lastX].isRound = true;
input[y][lastX].isEmpty = false;
}
}
if (lastX !== x) {
input[y][x].isRound = false;
input[y][x].isEmpty = true;
input[y][lastX].isRound = true;
input[y][lastX].isEmpty = false;
}
}
}
// i > 1000 &&
if (!found) {
lookup[key] = input.slice(0).map(row => row.slice(0));
idtokey[id] = key;
keytoid[key] = id;
loads[key] = getLoad(input);
pattern.push(id);
id++;
}
}
// console.log('E\n' + render());

// test (this doesn't seem to work):
const newGrid = render();
if (lastGrid === newGrid) {
console.log('grid repeating', i, '\n' + newGrid);
break;
}
lastGrid = newGrid;
if (i % pc === 0) {
console.log(p + '% ' + (new Date()).toISOString());
p++;
}
// console.log('last grid:\n' + lastGrid);
}
const load = input.reduce((acc, row) => {
return acc + row.reduce((acc2, c) => {
if (c.isRound) {
acc2 += c.load;
}
return acc2;
}, 0);
}, 0);
console.log(load);
const load = getLoad(input);
console.log('last:\n' + render(input) + '\nload:', load, '\ncached count: ', Object.keys(lookup).length);
// 102802 too low
return load;
}
},
Expand Down Expand Up @@ -1675,6 +1689,8 @@
console.log((i + 1) + ' of ' + startLen + ' ' + (new Date()).toISOString(), max);
});
console.log(max);
// 7965 too low
// 8079 too low
return max;
}
},
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const f = require('./public/funs');
const limiter = rateLimit({
// 30 seconds
windowMs: 30 * 1000,
// 100 requests in that time
max: 100,
// Return rate limit info in the `RateLimit-*` headers
standardHeaders: true,
Expand Down
Loading