-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha6ra.tt
53 lines (45 loc) · 1.72 KB
/
a6ra.tt
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
// Re-implementation of day 6 using Tailspin relational algebra and other new features
composer coord
{ x: <"x">, (<', '>) y: <"y"> }
end coord
def coordinates: {| [ $IN::lines -> coord ] -> \[i]({ $..., id: $i } !\) ... |};
def bounding_box: $coordinates(collect
{left: Min&{by: :(x:), select: :(x:)},
top: Min&{by: :(y:), select: :(y:)},
bottom: Max&{by: :(y:), select: :(y:)},
right: Max&{by: :(x:), select: :(x:)}} by {|{}|}) ...;
templates abs
when <?($::raw <0..>)> do $!
otherwise -$!
end abs
processor UniqueMinAndSum
@: {sum: 0"1"};
sink accumulate
@UniqueMinAndSum.sum: [email protected] + $.dist;
$ -> #
when <?($@UniqueMinAndSum <{coord: VOID}>)> do ..|@UniqueMinAndSum: $;
when <{dist: <[email protected]>}> do @UniqueMinAndSum.coord: 0;
when <{dist: <[email protected]>}> do ..|@UniqueMinAndSum: $;
end accumulate
source result
$@UniqueMinAndSum!
end result
end UniqueMinAndSum
def closest: {|
{by $bounding_box.left..$bounding_box.right -> (x: $), by $bounding_box.top..$bounding_box.bottom -> (y:$)}
-> {x: $.x, y: $.y,
$coordinates({coord: §.id, dist: ((§.x - $.x -> abs) + (§.y - $.y -> abs))"1"})... -> ..=UniqueMinAndSum
}
|};
def infinites: {| $closest... -> \(
when <{x: <=$bounding_box.left>}
|{x: <=$bounding_box.right>}
|{y: <=$bounding_box.top>}
|{y: <=$bounding_box.bottom>}> do $! \) -> {coord: $.coord} |};
// part 1
($closest notMatching ($infinites union {|{coord: 0}|})) -> $(collect {size: Count} by $coordinates({coord: §.id}))
-> $(collect {largest: Max&{by: :(size:), select: :(size:)}} by {|{}|}) -> !OUT::write
'
' -> !OUT::write
// part 2
$closest... -> \(<{sum: <..~10000>}> $!\) -> ..=Count -> !OUT::write