1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Dusa } from "https://unpkg.com/dusa@0.0.10/lib/client.js";
const INPUT = `
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
`
.trim()
.split("\n")
.map((line) => line.trim().split(""));
const PROGRAM = `
# AOC Day 11
#builtin NAT_SUCC s
#builtin INT_TIMES times
#builtin INT_PLUS plus
#builtin INT_MINUS minus
# Process input to get X, Y coordinates
charAt X Y is Ch :-
field _ "map" is List,
field List Y is Cols,
field Cols X is Ch.
width is W :- field _ "width" is W.
expansionFactor is F :- field _ "expansionFactor" is F.
# Use default reasoning to find out which rows/cols have stars
hasStar y Y is { false? } :- charAt _ Y is _.
hasStar y Y is true :- charAt _ Y is "#".
hasStar x X is { false? } :- charAt X _ is _.
hasStar x X is true :- charAt X _ is "#".
needsRemap x 0 0.
needsRemap y 0 0.
remap Axis Old is (minus (plus New expansionFactor) 1) :-
needsRemap Axis Old New,
hasStar Axis Old is false.
remap Axis Old is New :-
needsRemap Axis Old New,
hasStar Axis Old is true.
needsRemap Axis (s Old) (s New) :-
remap Axis Old is New.
starAt (remap x X) (remap y Y) :-
charAt X Y is "#".
starDist (tuple X YA X YB) is (minus YA YB) :-
starAt X YA,
starAt X YB,
YA > YB.
starDist (tuple XA YA XB YB) is (plus (minus XA XB) (minus YA YB)) :-
starAt XA YA,
starAt XB YB,
XA > XB, YA >= YB.
starDist (tuple XA YA XB YB) is (plus (minus XA XB) (minus YB YA)) :-
starAt XA YA,
starAt XB YB,
XA > XB, YA < YB.
`;
for (const expansionFactor of [2, 10, 100, 1000000]) {
const dusa = new Dusa(PROGRAM);
dusa.load({ map: INPUT, width: INPUT[0].length, expansionFactor }, "field");
const solution = dusa.sample();
let accum = 0n;
for (const [_, dist] of solution.lookup("starDist")) {
accum += dist;
}
console.log(accum);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 11, 2023