/
a24.tt
62 lines (53 loc) · 1.56 KB
/
a24.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
use 'hash' stand-alone
use 'list' stand-alone
composer hexDirections
[<nw|ne|e|se|sw|w>+]
rule nw: <='nw'> -> [0, -1]
rule ne: <='ne'> -> [1, -1]
rule e: <='e'> -> [1, 0]
rule se: <='se'> -> [0, 1]
rule sw: <='sw'> -> [-1, 1]
rule w: <='w'> -> [-1, 0]
end hexDirections
def directions: [$IN::lines -> hexDirections];
templates layFloor
def blackTiles: [] -> hash/Set;
$... -> \(
@:[0,0];
$... -> @: [$@(1) + $(1), $@(2) + $(2)];
$@!
\) -> \(
def tileToFlip: $;
$tileToFlip -> blackTiles::add -> \(<=0> 0!\) -> $tileToFlip -> blackTiles::remove -> !VOID
\) -> !VOID
$blackTiles !
end layFloor
// part1
$directions -> layFloor -> \( @: 0; $::values -> @: $@+1; $@!\) -> '$;
' -> !OUT::write
def ALL_DIRECTIONS: 'nwneesesww' -> hexDirections;
templates reconfigure
def current: $;
def visited: [] -> hash/Set;
[$current::values -> \getChangingTiles(
def tile: $;
$ALL_DIRECTIONS... -> [$(1) + $tile(1), $(2) + $tile(2)] !
$tile !
\getChangingTiles) -> \notVisited(<?($ -> visited::add <=1>)> $! \notVisited)
-> \ifBlack(
def tile: $;
def blackNeighbours: [$ALL_DIRECTIONS... -> [$(1) + $tile(1), $(2) + $tile(2)]
-> current::isMember] -> list/sum;
$tile -> current::isMember -> #
when <=0 ?($blackNeighbours <=2>)
|=1 ?($blackNeighbours <1..2>)> $tile !
\ifBlack)] -> hash/Set !
end reconfigure
templates do&{days:}
@:$;
1..$days -> @: $@ -> reconfigure;
$@!
end do
// part2
$directions -> layFloor -> do&{days: 100} -> \( @: 0; $::values -> @: $@+1; $@!\) -> '$;
' -> !OUT::write