Skip to content

Commit

Permalink
Merge pull request #27 from dieseltravis/glitch
Browse files Browse the repository at this point in the history
πŸ˜ƒβ›ͺ Updated with Glitch: day 16
  • Loading branch information
dieseltravis authored Dec 16, 2023
2 parents c9f9333 + 87bd422 commit 3cca157
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ also on github: [dieseltravis/aoc2023](https://github.com/dieseltravis/aoc2023)
13. [day thirteen](https://branch-three-oviraptor.glitch.me/day/13)
14. [day fourteen](https://branch-three-oviraptor.glitch.me/day/14) (part 2 should finish in a few days...)
15. [day fifteen](https://branch-three-oviraptor.glitch.me/day/15)
<!--
16. [day sixteen](https://branch-three-oviraptor.glitch.me/day/16)
<!--
17. [day seventeen](https://branch-three-oviraptor.glitch.me/day/17)
18. [day eighteen](https://branch-three-oviraptor.glitch.me/day/18)
19. [day nineteen](https://branch-three-oviraptor.glitch.me/day/19)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "branch-three-oviraptor",
"version": "2023.12.14",
"version": "2023.12.15",
"description": "Travis's Advent of Code 2023",
"author": "Travis Hardiman",
"homepage": "https://branch-three-oviraptor.glitch.me/",
Expand Down
161 changes: 159 additions & 2 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1518,8 +1518,165 @@
}
},
day16: {
part1: d => d,
part2: d => d
part1: (data) => {
const mirrors = {
'.': (dy, dx) => [[dy, dx]],
'/': (dy, dx) => [[-dx, -dy]],
'\\': (dy, dx) => [[dx, dy]],
'|': (dy, dx) => {
const beams = [];
if (dx === 0) {
beams.push([dy, dx]);
} else {
beams.push([-dx, dy]);
beams.push([dx, dy]);
}
return beams;
},
'-': (dy, dx) => {
const beams = [];
if (dy === 0) {
beams.push([dy, dx]);
} else {
beams.push([dx, -dy]);
beams.push([dx, dy]);
}
return beams;
}
};
const input = data.trim().split('\n').map(l => l.split(''));
const ymax = input.length;
const xmax = input[0].length;
console.log(input, ymax, xmax);
const tileDirs = { '0,0,0,1': 1 };
const tiles = { '0,0': 1 };
let beams = [{ y: 0, x: 0, dy: 0, dx: 1 }];
let safety = 1000;
let lastDirs = 0;
let lastEnergy = 0;
while (safety-- > 0 && beams.length > 0) {
const newBeams = [];
for (let l = beams.length; l--;) {
const beam = beams[l];
beam.y += beam.dy;
beam.x += beam.dx;
if (beam.y >= 0 && beam.x >= 0 && beam.y < ymax && beam.x < xmax) {
tileDirs[beam.y + ',' + beam.x + ',' + beam.dy + ',' + beam.dx] = 1;
tiles[beam.y + ',' + beam.x] = 1;
const mirror = mirrors[input[beam.y][beam.x]];
const newDeltas = mirror(beam.dy, beam.dx);
newBeams.push(...newDeltas.map(dd => {
return {
y: beam.y,
x: beam.x,
dy: dd[0],
dx: dd[1]
};
}));
}
}
beams = newBeams;
const dirs = Object.values(tileDirs).length;
const energy = Object.values(tiles).length;
if (dirs === lastDirs) {
break;
}
lastEnergy = energy;
lastDirs = dirs;
}
if (safety <= 0) {
console.warn('safety.');
}
const result = lastEnergy;
console.log(result);
return result;
},
part2: (data) => {
const mirrors = {
'.': (dy, dx) => [[dy, dx]],
'/': (dy, dx) => [[-dx, -dy]],
'\\': (dy, dx) => [[dx, dy]],
'|': (dy, dx) => {
const beams = [];
if (dx === 0) {
beams.push([dy, dx]);
} else {
beams.push([-dx, dy]);
beams.push([dx, dy]);
}
return beams;
},
'-': (dy, dx) => {
const beams = [];
if (dy === 0) {
beams.push([dy, dx]);
} else {
beams.push([dx, -dy]);
beams.push([dx, dy]);
}
return beams;
}
};
const input = data.trim().split('\n').map(l => l.split(''));
const ymax = input.length;
const xmax = input[0].length;
const starters = [];
for (let x = xmax; x--;) {
starters.push({ y: 0, x, dy: 1, dx: 0 });
starters.push({ y: ymax - 1, x, dy: -1, dx: 0 });
}
for (let y = ymax; y--;) {
starters.push({ y, x: 0, dy: 0, dx: 1 });
starters.push({ y, x: xmax - 1, dy: 0, dx: -1 });
}
console.log(input, ymax, xmax, starters);
let max = 0;
// progress
const startLen = starters.length;
starters.forEach((start, i) => {
const tileDirs = {};
tileDirs[start.y + ',' + start.x + ',' + start.dy + ',' + start.dx] = 1;
const tiles = {};
tiles[start.y + ',' + start.x] = 1;
let beams = [start];
let lastDirs = 0;
let lastEnergy = 0;
while (beams.length > 0) {
const newBeams = [];
for (let l = beams.length; l--;) {
const beam = beams[l];
beam.y += beam.dy;
beam.x += beam.dx;
if (beam.y >= 0 && beam.x >= 0 && beam.y < ymax && beam.x < xmax) {
tileDirs[beam.y + ',' + beam.x + ',' + beam.dy + ',' + beam.dx] = 1;
tiles[beam.y + ',' + beam.x] = 1;
const mirror = mirrors[input[beam.y][beam.x]];
const newDeltas = mirror(beam.dy, beam.dx);
newBeams.push(...newDeltas.map(dd => {
return {
y: beam.y,
x: beam.x,
dy: dd[0],
dx: dd[1]
};
}));
}
}
beams = newBeams;
const dirs = Object.values(tileDirs).length;
const energy = Object.values(tiles).length;
if (dirs === lastDirs) {
break;
}
lastEnergy = energy;
lastDirs = dirs;
}
max = Math.max(lastEnergy, max);
console.log((i + 1) + ' of ' + startLen + ' ' + (new Date()).toISOString(), max);
});
console.log(max);
return max;
}
},
day17: {
part1: d => d,
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ <h2>solutions:</h2>
<li><a href="/day/13">day 13</a></li>
<li><a href="/day/14">day 14</a> (part 2 should finish in a few days...)</li>
<li><a href="/day/15">day 15</a></li>
<!--
<li><a href="/day/16">day 16</a></li>
<!--
<li><a href="/day/17">day 17</a></li>
<li><a href="/day/18">day 18</a></li>
<li><a href="/day/19">day 19</a></li>
Expand Down

0 comments on commit 3cca157

Please # to comment.