-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha13.tt
149 lines (137 loc) · 5.25 KB
/
a13.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
include 'lib/list'
data left <=1>
data straight <=2>
data right <=3>
data turn <left|straight|right>
data direction <='^'|='<'|='>'|='v'>
data movingRight <{cart: <cart>, rail: <rail>}?($ <{cart: <{direction: <='^'>}>, rail: <='/'>}
| {cart: <{direction: <='>'>}>, rail: <='-'>}
| {cart: <{direction: <='v'>}>, rail: <='\'>}
| {cart: <{turn: <right>, direction: <='^'>}>, rail: <='+'>}
| {cart: <{turn: <straight>, direction: <='>'>}>, rail: <='+'>}
| {cart: <{turn: <left>, direction: <='v'>}>, rail: <='+'>}
>)>
data movingLeft <{cart: <cart>, rail: <rail>}?($ <{cart: <{direction: <='^'>}>, rail: <='\'>}
| {cart: <{direction: <='<'>}>, rail: <='-'>}
| {cart: <{direction: <='v'>}>, rail: <='/'>}
| {cart: <{turn: <left>, direction: <='^'>}>, rail: <='+'>}
| {cart: <{turn: <straight>, direction: <='<'>}>, rail: <='+'>}
| {cart: <{turn: <right>, direction: <='v'>}>, rail: <='+'>}
>)>
data movingUp <{cart: <cart>, rail: <rail>}?($ <{cart: <{direction: <='^'>}>, rail: <='|'>}
| {cart: <{direction: <='>'>}>, rail: <='/'>}
| {cart: <{direction: <='<'>}>, rail: <='\'>}
| {cart: <{turn: <straight>, direction: <='^'>}>, rail: <='+'>}
| {cart: <{turn: <left>, direction: <='>'>}>, rail: <='+'>}
| {cart: <{turn: <right>, direction: <='<'>}>, rail: <='+'>}
>)>
data movingDown <{cart: <cart>, rail: <rail>}?($ <{cart: <{direction: <='<'>}>, rail: <='/'>}
| {cart: <{direction: <='>'>}>, rail: <='\'>}
| {cart: <{direction: <='v'>}>, rail: <='|'>}
| {cart: <{turn: <left>, direction: <='<'>}>, rail: <='+'>}
| {cart: <{turn: <right>, direction: <='>'>}>, rail: <='+'>}
| {cart: <{turn: <straight>, direction: <='v'>}>, rail: <='+'>}
>)>
composer tracks
[<terrain|cart|=' '>+]
rule terrain: {rail: <'[-|/\\+]'>}
rule cart: <'[<>^v]'> -> {cart: {direction: $, turn: 1}, rail: $ -> \(
<='^'|='v'> '|' !
<='<'|='>'> '-' !
\)}
end tracks
def map: [$IN::lines -> \(<~=''> $!\) -> tracks];
// part 1
templates firstCrash
@: $;
[$@ -> \[i;j](<{cart:<>}> [$i,$j] !\)... ...] -> #
when <[<[](3)>]> do $... -> \(<[](3)> $! \) !
when <=[]> !VOID
otherwise [$... -> \(
def i: $(1);
def j: $(2);
templates nextTurnChoice
when <?($@firstCrash($i;$j) <{rail: <='+'>}>)> do $::raw mod 3 + 1 ! otherwise $!
end nextTurnChoice
$@firstCrash($i; $j) -> #
when <movingRight> do
[$i,$j+1, $@firstCrash($i;$j+1) -> \(<{cart:<>}> 'X'!\)]!
^@firstCrash($i; $j).cart -> {direction: '>', turn: $.turn -> nextTurnChoice}
-> ..|@firstCrash($i;$j+1): (cart:$);
when <movingLeft> do
[$i,$j-1, $@firstCrash($i;$j-1) -> \(<{cart:<>}> 'X'!\)]!
^@firstCrash($i; $j).cart -> {direction: '<', turn: $.turn -> nextTurnChoice}
-> ..|@firstCrash($i;$j-1): (cart:$);
when <movingUp> do
[$i-1,$j, $@firstCrash($i-1;$j) -> \(<{cart:<>}> 'X'!\)]!
^@firstCrash($i; $j).cart -> {direction: '^', turn: $.turn -> nextTurnChoice}
-> ..|@firstCrash($i-1;$j): (cart:$);
when <movingDown> do
[$i+1,$j, $@firstCrash($i+1;$j) -> \(<{cart:<>}> 'X'!\)]!
^@firstCrash($i; $j).cart -> {direction: 'v', turn: $.turn -> nextTurnChoice}
-> ..|@firstCrash($i+1;$j): (cart:$);
\)] -> list/bubblesort&{compare: elementwise} -> #
end firstCrash
$map -> firstCrash -> '$(2) -> $-1;,$(1) -> $-1;
' -> !OUT::write
//part 2
operator (a elementwise b)
1 -> #
when <$a::length~..?($ <$b::length~..>)> 0 !
when <$a::length~..> -1 !
when <$b::length~..> 1 !
when <?($a($) <..~$b($)>)> -1 !
when <?($a($) <$b($)~..>)> 1 !
otherwise $ + 1 -> #
end elementwise
templates lastCart
@: $;
[$@ -> \[i;j](<{cart:<>}> [$i,$j] !\)... ...] -> #
when <[](..1)> do $(1)!
otherwise [$... -> \(
def i: $(1);
def j: $(2);
templates nextTurnChoice
when <?($@lastCart($i;$j) <{rail: <='+'>}>)> do $::raw mod 3 + 1 ! otherwise $!
end nextTurnChoice
$@lastCart($i; $j) -> #
when <movingRight> do
def next: ^@lastCart($i; $j).cart -> {direction: '>', turn: $.turn -> nextTurnChoice}
-> (cart:$);
$@lastCart($i;$j+1) -> \(
when <{cart:<>}> do ^@lastCart($i;$j+1).cart -> !VOID
otherwise
..|@lastCart($i;$j+1): $next;
[$i, $j+1] !
\)!
when <movingLeft> do
def next: ^@lastCart($i; $j).cart -> {direction: '<', turn: $.turn -> nextTurnChoice}
-> (cart:$);
$@lastCart($i;$j-1) -> \(
when <{cart:<>}> do ^@lastCart($i;$j-1).cart -> !VOID
otherwise
..|@lastCart($i;$j-1): $next;
[$i, $j-1] !
\)!
when <movingUp> do
def next: ^@lastCart($i; $j).cart -> {direction: '^', turn: $.turn -> nextTurnChoice}
-> (cart:$);
$@lastCart($i-1;$j) -> \(
when <{cart:<>}> do ^@lastCart($i-1;$j).cart -> !VOID
otherwise
..|@lastCart($i-1;$j): $next;
[$i-1, $j] !
\)!
when <movingDown> do
def next: ^@lastCart($i; $j).cart -> {direction: 'v', turn: $.turn -> nextTurnChoice}
-> (cart:$);
$@lastCart($i+1;$j) -> \(
when <{cart:<>}> do ^@lastCart($i+1;$j).cart -> !VOID
otherwise
..|@lastCart($i+1;$j): $next;
[$i+1, $j] !
\)!
\)] -> [$... -> \(<?($@lastCart($(1);$(2)) <{cart:<>}>)> $!\)] -> list/bubblesort&{compare: elementwise} -> #
end lastCart
$map -> lastCart -> '$(2) -> $-1;,$(1) -> $-1;
' -> !OUT::write