Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
81
82
83
84
85
86
87
88
89
90
91
92
import { Dusa } from "https://unpkg.com/dusa@0.0.10/lib/client.js";
const INPUT = `
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ
`
.trim()
.split("\n")
.map((line) => line.trim().split(""));
const dusa = new Dusa(`
# AOC Day 10, Part 1
#builtin INT_PLUS plus
#builtin INT_MINUS minus
# Process input to get X, Y coordinates
charAt (pair X Y) is Ch :-
field _ "map" is List,
field List Y is Cols,
field Cols X is Ch.
# Which connections does a character imply?
connectX "L" 1. connectY "L" -1.
connectX "J" -1. connectY "J" -1.
connectX "F" 1. connectY "F" 1.
connectX "7" -1. connectY "7" 1.
connectY "|" -1. connectY "|" 1.
connectX "-" -1. connectX "-" 1.
# Edges require agreement between the source and destination
# about there being a connection
# Horizontal edges (only X changes)
edge A B :-
charAt A is Ch, A == pair X1 Y,
connectX Ch Delta, X2 == plus X1 Delta,
B == pair X2 Y, charAt B is Ch2,
connectX Ch2 (minus 0 Delta).
# Vertical edges (only Y changes)
edge A B :-
charAt A is Ch, A == pair X Y1,
connectY Ch Delta, Y2 == plus Y1 Delta,
B == pair X Y2, charAt B is Ch2,
connectY Ch2 (minus 0 Delta).
# The start is handled specially, with its connections
# being implied by the neighbors
start is A :- charAt A is "S".
# Horizontal neighbor(s) of start
edge start (pair X Y) :-
charAt (pair X Y) is Ch,
connectX Ch Delta,
start == pair (plus X Delta) Y.
# Vertical neighbor(s) of start
edge start (pair X Y) :-
charAt (pair X Y) is Ch,
connectY Ch Delta,
start == pair X (plus Y Delta).
edge A B :- edge B A.
# We want to trace our path around the loop,
# but only in one of the two possible directions
after start is { A? } :- edge start A.
after B is C :-
after A is B,
edge B C,
C != A.
distance A is 1 :- after start is A.
distance B is (plus N 1) :-
distance A is N,
A != start,
after A is B.
# The goal value is half the length, which we calculate in a
# very cheesy manner
goal is D :-
distance _ is D,
plus D D == distance start.
`);
dusa.load({ map: INPUT }, "field");
console.log([...dusa.sample().lookup("goal")]);
December 11, 2023