-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha18.tt
79 lines (69 loc) · 1.95 KB
/
a18.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
include 'lib/hash'
def scan: [$IN::lines -> [$...]];
templates count_tiles
@: {open: 0"1", trees: 0"1", lumberyards: 0"1"};
$... ... -> #
$@ !
<='.'> @.open: [email protected] + 1;
<='|'> @.trees: [email protected] + 1;
<='#'> @.lumberyards: [email protected] + 1;
end count_tiles
templates evolve
def start: $;
$ -> \[i;j](
when <='.' ?($start($i-1..$i+1; $j-1..$j+1) -> count_tiles <{trees: <3..>}>)> '|' !
when <='|' ?($start($i-1..$i+1; $j-1..$j+1) -> count_tiles <{lumberyards: <3..>}>)> '#' !
when <='#' ?($start($i-1..$i+1; $j-1..$j+1) -> count_tiles <{trees: <=0>}|{lumberyards: <=1>}>)> '.' !
otherwise $!
\) !
end evolve
templates resource_after
@: $scan;
$ -> #
when <=0> do $@ -> count_tiles -> $.trees * $.lumberyards !
otherwise
@: $@ -> evolve;
$-1 -> #
end resource_after
test 'example'
modify program
def scan: [['.#.#...|#.',
'.....#|##|',
'.|..|...#.',
'..|#.....#',
'#.#|||#|#|',
'...#.||...',
'.|....|...',
'||...#|.#|',
'|.||||..|.',
'...#.|..|.']... -> [$...]];
end program
assert 0 -> resource_after <=17*27> 'counts 0'
assert 1 -> resource_after <=12*40> 'counts 1'
assert 10 -> resource_after <=1147> 'counts resource'
end 'example'
// part 1
10 -> resource_after -> '$;
' -> !OUT::write
// part 2
templates find_cycle
def steps: [] -> hash/Map&{key: :(map:), value: :(step:)};
@: {map: $, step: 0"1"};
[$@ -> steps::put] -> #
when <=[]> do
@: {map: [email protected] -> evolve, step: [email protected] + 1};
[$@ -> steps::put] -> #
otherwise
{lead: $(1).step, cycle: [email protected] - $(1).step, steps: $steps} !
$(1) -> steps::put -> !VOID
end find_cycle
def repetition: $scan -> find_cycle;
'$repetition;
' -> !OUT::write
//part2
((1000000000 - $repetition.lead) mod $repetition.cycle) + $repetition.lead -> \(
def step_id: $;
$repetition.steps::entries -> #
when <{step: <=$step_id>}> do
$.map !
\) -> count_tiles -> $.trees * $.lumberyards -> !OUT::write