-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha6.tt
117 lines (98 loc) · 2.62 KB
/
a6.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
composer coord
{ x: <INT> -> ($)"x", (<', '>) y: <INT> -> ($)"y"}
end coord
def coordinates: [ $IN::lines -> coord ] -> \[i]({ $..., id: $i } !\);
templates min
<?($(1)<..$(2)>)> $(1) !
<> $(2) !
end min
templates max
<?($(1)<$(2)..>)> $(1) !
<> $(2) !
end max
templates boundingbox
@: { left: $(1).x, top: $(1).y, bottom: $(1).y, right: $(1).x };
$(2..last)... -> @boundingbox: { left: [$.x, [email protected]] -> min,
top: [$.y, [email protected]] -> min,
bottom: [$.y, [email protected]] -> max,
right: [$.x, [email protected]] -> max };
$@ !
end boundingbox
def bb: $coordinates -> boundingbox;
templates abs
<0..> $ !
<> -$ !
end abs
templates quicksort&{comparator:}
@: [];
$ -> #
when <[](2..)> do
def pivot: $(1);
[ [ $(2..last)... -> \(
when <?([$, $pivot] -> comparator <..0>)> do
$ !
otherwise
..|@quicksort: $;
\)] -> quicksort&{comparator:comparator}..., $pivot, $@ -> quicksort&{comparator:comparator}... ] !
otherwise
$ !
end quicksort
templates byDist
def second: $(2).dist;
$(1).dist -> #
<=$second> 0 !
<..$second> -1 !
<> 1 !
end byDist
templates sum
@: 0;
$... -> @sum: $@sum + $;
$@ !
end sum
def field: [$bb.left..$bb.right -> \(def x: $;
[ $bb.top..$bb.bottom -> \(def y: $;
[ $coordinates... -> { id: $.id, dist: (($.x - $x -> $::raw -> abs) + ($.y - $y -> $::raw -> abs))"md" } ]
-> quicksort&{comparator: byDist}
-> { closest: $ -> \(<?($(1).dist <=$(2).dist>)> (id:0) -> $::value ! <> $(1).id !\),
sum: [ $... -> $.dist ] -> sum } !
\)
] !\) ];
// part 1
templates natural
def second: $(2);
$(1) -> #
<=$second> 0 !
<..$second> -1 !
<> 1 !
end natural
templates unique
@: $(1);
[ $(2..last)... -> #, $@ ] !
<~=$@>
$@ !
@: $;
end unique
def infinites: [ $field(1;1..last)..., $field(1..last;1)..., $field(last;1..last)..., $field(1..last;last)...]
-> [ $... -> $.closest ] -> quicksort&{comparator: natural} -> unique;
templates flatten
when <[]> do
$... -> #
otherwise
$!
end flatten
templates reduce&{operator:}
@: $(1);
$(2..last)... -> @reduce: [$@reduce, $] -> operator;
$@ !
end reduce
templates count&{low:,high:}
@: [ $low..$high -> 0 ];
$... -> @count($-$low+1): $@count($-$low+1) + 1;
$@ !
end count
[ $field -> flatten -> $.closest -> \(<1.. ?($infinites <~[<=$>]>)> $ !\) ]
-> count&{low: 1, high: $coordinates::length} -> reduce&{operator: max} -> !OUT::write
'
' -> !OUT::write
// part 2
[ $field -> flatten -> $.sum -> \(<..~10000"md"> $ !\) ] -> $::length -> !OUT::write