Compare commits
No commits in common. "71e6dcd7dc45a68438f75f8f5944f46dbd45f230" and "2db8edb0870e5c7f9281f91ea3b6587b92bf7831" have entirely different histories.
71e6dcd7dc
...
2db8edb087
|
@ -3,8 +3,6 @@
|
|||
inherit (lib.strings) toIntBase10;
|
||||
in input: rec {
|
||||
|
||||
sum = foldl' add 0;
|
||||
|
||||
blinkStone = stone: count:
|
||||
if toIntBase10 stone == 0 then [{ "1" = count; }]
|
||||
else let
|
||||
|
@ -23,17 +21,19 @@ in input: rec {
|
|||
;
|
||||
|
||||
bfast = stones: stones
|
||||
|> builtins.mapAttrs blinkStone
|
||||
|> (builtins.mapAttrs (stone: blinkStone stone))
|
||||
|> attrValues
|
||||
|> concatLists
|
||||
|> builtins.zipAttrsWith (stone: sum)
|
||||
|> addStones
|
||||
;
|
||||
|
||||
bfastt = times: stones:
|
||||
if times == 0 then stones |> attrValues |> sum
|
||||
if times == 0 then stones |> attrValues |> foldl' add 0
|
||||
else bfastt (times - 1) (bfast stones)
|
||||
;
|
||||
|
||||
addStones = builtins.zipAttrsWith (stone: foldl' add 0);
|
||||
|
||||
initialStones = input
|
||||
|> lib.trim
|
||||
|> lib.splitString " "
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
AAAA
|
||||
BBCD
|
||||
BBCC
|
||||
EEEC
|
|
@ -1,5 +0,0 @@
|
|||
OOOOO
|
||||
OXOXO
|
||||
OOOOO
|
||||
OXOXO
|
||||
OOOOO
|
|
@ -1,10 +0,0 @@
|
|||
RRRRIICCFF
|
||||
RRRRIICCCF
|
||||
VVRRRCCFFF
|
||||
VVRCCCJFFF
|
||||
VVVVCJJCFE
|
||||
VVIVCCJJEE
|
||||
VVIIICJJEE
|
||||
MIIIIIJJEE
|
||||
MIIISIJEEE
|
||||
MMMISSJEEE
|
|
@ -1,5 +0,0 @@
|
|||
EEEEE
|
||||
EXXXX
|
||||
EEEEE
|
||||
EXXXX
|
||||
EEEEE
|
|
@ -1,6 +0,0 @@
|
|||
AAAAAA
|
||||
AAABBA
|
||||
AAABBA
|
||||
ABBAAA
|
||||
ABBAAA
|
||||
AAAAAA
|
|
@ -1,50 +0,0 @@
|
|||
|
||||
const input = (await Bun.file(Bun.argv[2]).text()).trim()
|
||||
|
||||
let map = input.split("\n")
|
||||
|
||||
const dirs = [[-1,0], [1,0], [0, -1], [0, 1]];
|
||||
const height = map.length;
|
||||
const width = height;
|
||||
|
||||
const key = (x,y) => x + y*width;
|
||||
const visited = new Set();
|
||||
|
||||
function search(x,y) {
|
||||
if (visited.has(key(x,y))) {
|
||||
return {area: 0, perimeter: 0}
|
||||
}
|
||||
visited.add(key(x,y))
|
||||
const letter = map[y][x]
|
||||
let perimeter = 0
|
||||
let area = 1
|
||||
for (const [dx, dy] of dirs) {
|
||||
const nletter = map[y + dy]?.[x + dx];
|
||||
if (letter !== nletter) {
|
||||
perimeter += 1
|
||||
continue
|
||||
}
|
||||
const n = search(x + dx, y + dy)
|
||||
area += n.area
|
||||
perimeter += n.perimeter
|
||||
}
|
||||
|
||||
return {area, perimeter}
|
||||
}
|
||||
|
||||
const regions = [];
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
if (visited.has(key(x,y))) continue
|
||||
regions.push(search(x,y))
|
||||
}
|
||||
}
|
||||
|
||||
let cost = 0
|
||||
for (const {area, perimeter} of regions) {
|
||||
cost += area * perimeter
|
||||
}
|
||||
console.log(cost)
|
||||
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
|
||||
const input = (await Bun.file(Bun.argv[2]).text()).trim()
|
||||
|
||||
let map = input.split("\n")
|
||||
|
||||
const dirs = [[-1,0], [0,-1], [1,0], [0, 1]];
|
||||
const height = map.length;
|
||||
const width = height;
|
||||
|
||||
const key = (x,y) => x + y*width;
|
||||
const vkey = (x,y) => y + x*height;
|
||||
const visited = new Set();
|
||||
|
||||
function search(x,y) {
|
||||
if (visited.has(key(x,y))) {
|
||||
return {area: 0, hsides: new Set()}
|
||||
}
|
||||
visited.add(key(x,y))
|
||||
const letter = map[y][x]
|
||||
let area = 1
|
||||
let hsides = new Set()
|
||||
for (const [dx, dy] of dirs) {
|
||||
const nletter = map[y + dy]?.[x + dx];
|
||||
if (letter !== nletter) {
|
||||
if (dx === 0) {
|
||||
hsides.add({y: y+dy/2, key: key(x,y)})
|
||||
}
|
||||
continue
|
||||
}
|
||||
const n = search(x + dx, y + dy)
|
||||
area += n.area
|
||||
hsides = hsides.union(n.hsides)
|
||||
}
|
||||
|
||||
return {area, hsides}
|
||||
}
|
||||
|
||||
const regions = [];
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
if (visited.has(key(x,y))) continue
|
||||
regions.push(search(x,y))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let cost = 0;
|
||||
for (const {area, hsides} of regions) {
|
||||
// hacky filter of all perimeter nodes to extract adjacent
|
||||
const horizontal = Array.from(hsides)
|
||||
.sort((a, b) => a.key - b.key)
|
||||
.sort((a, b) => a.y - b.y)
|
||||
.filter(({y, key}, i, a) => a[i-1]?.key + 1 !== key || a[i-1]?.y !== y)
|
||||
.length
|
||||
|
||||
// there's always an equal amount of horizontal and vertical sides
|
||||
cost += area * (horizontal * 2)
|
||||
}
|
||||
|
||||
console.log(cost)
|
||||
|
||||
|
Loading…
Reference in a new issue