-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha2.tt
67 lines (52 loc) · 1.29 KB
/
a2.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
composer tag
<'[a-z]+'>
end tag
def tags: [ $IN::lines -> tag ];
templates twosAndThrees
templates countOne
@: 1;
def one: $(1);
$(2..last) -> \[i](<=$one> @countOne: 1 + $@countOne; <> $ !\) ->
{ rest: $ } !
$@ -> \(<=2> { two: 1"1" }! <=3> { three: 1"1" }!\) !
end countOne
[ $... -> # ] !
when <'.*'> do
def chars: [ $... ];
@: { two: 0"1", three: 0"1", rest: $chars };
$@ -> #
when <{rest:<[](2..)>}> do
..|@: $.rest -> countOne;
$@ -> #
otherwise
{ two: $.two, three: $.three } !
end twosAndThrees
templates sumTwosAndThrees
@: $(1);
$(2..last)... -> #
$@ !
<{}>
@: { two: $.two + [email protected], three: $.three + [email protected] };
end sumTwosAndThrees
// solution to part 1, phew!!!
$tags -> twosAndThrees -> sumTwosAndThrees -> ($.two * $.three) -> !OUT::write
'
' -> !OUT::write
// part 2
templates onediff
[ $ -> # ] !
<[](2..)>
@: [ $(1)... ];
templates diffs
@: 0;
[ $... ] -> \[i](<~=$@onediff($i)> @diffs: 1 + $@diffs;\) -> !VOID
$ -> \(<?($@diffs<=1>)> '$@onediff...;' ! $ !\) !
end diffs
$(2..last)... -> diffs !
$(2..last) -> #
end onediff
templates collate
@: [ $(1)... ];
'$:[ $(2)... ] -> \[i](<=$@collate($i)> $ !\)...;' !
end collate
$tags -> onediff -> collate -> !OUT::write