Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Last active December 18, 2023 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluepichu/724cc3a54fe947c9cbccfb5664947035 to your computer and use it in GitHub Desktop.
Save bluepichu/724cc3a54fe947c9cbccfb5664947035 to your computer and use it in GitHub Desktop.
import { Advent, f, fm, chr, ord, arr } from "advent";
import { Set, Map } from "immutable";
const { compute, computeCheck } = await Advent({ day: 18 });
compute(2, async (input) => {
const data = input.parse(f.nl((s) => {
const [dir, dist, color] = s.split(" ");
// return { dir, dist: parseInt(dist), color };
return {
dist: parseInt(color.slice(2, 7), 16),
dir: (
color[7] === "0" ? "R" :
color[7] === "1" ? "D" :
color[7] === "2" ? "L" :
"U"
),
}
}));
let x = 0;
let y = 0;
let area = 0; // note: the value of this variable is actually _double_ the area
let perimeter = 0;
for (const { dir, dist } of data) {
const x1 = x;
const y1 = y;
switch (dir) {
case "R": x += dist; break;
case "D": y += dist; break;
case "L": x -= dist; break;
case "U": y -= dist; break;
}
area += x1 * y - x * y1;
perimeter += dist;
}
console.log(perimeter);
return Math.abs(area / 2) + perimeter / 2 + 1;
// leftovers from part 1...
const grid: (string | undefined)[][] = arr(1000, () => arr(1000, () => undefined));
let i = 500;
let j = 500;
for (const { dir, dist, color } of data) {
for (let k = 0; k < dist; k++) {
switch (dir) {
case "R": j++; break;
case "D": i++; break;
case "L": j--; break;
case "U": i--; break;
}
grid[i][j] = color;
}
}
let visited = Set<string>();
let queue: [number, number][] = [[0, 0]];
while (queue.length > 0) {
let [i, j] = queue.shift()!;
if (i < 0 || i >= grid.length || j < 0 || j >= grid[i].length) {
continue;
}
if (grid[i][j] !== undefined) {
continue;
}
const loc = `${i},${j}`;
if (visited.has(loc)) {
continue;
}
visited = visited.add(loc);
queue.push([i + 1, j]);
queue.push([i - 1, j]);
queue.push([i, j + 1]);
queue.push([i, j - 1]);
}
return grid.length * grid[0].length - visited.size;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment