#!/bin/bash rulesdir=~/.golly/Rules rulename=AoC2018Day18 title='Advent of Code 2018 - Day 18: Settlers of The North Pole' mkdir -p "$rulesdir" cat <"$rulesdir/$rulename.rule" @RULE $rulename --- $title --- On the outskirts of the North Pole base construction project, many Elves are collecting lumber. The lumber collection area is 50 acres by 50 acres; each acre can be either open ground (.), trees (|), or a lumberyard (#). You take a scan of the area (your puzzle input). Strange magic is at work here: each minute, the landscape looks entirely different. In exactly one minute, an open acre can fill with trees, a wooded acre can be converted to a lumberyard, or a lumberyard can be cleared to open ground (the lumber having been sent to other projects). The change to each acre is based entirely on the contents of that acre as well as the number of open, wooded, or lumberyard acres adjacent to it at the start of each minute. Here, "adjacent" means any of the eight acres surrounding that acre. (Acres on the edges of the lumber collection area might have fewer than eight adjacent acres; the missing acres aren't counted.) In particular: - An open acre will become filled with trees if three or more adjacent acres contained trees. Otherwise, nothing happens. - An acre filled with trees will become a lumberyard if three or more adjacent acres were lumberyards. Otherwise, nothing happens. - An acre containing a lumberyard will remain a lumberyard if it was adjacent to at least one other lumberyard and at least one acre containing trees. Otherwise, it becomes open. These changes happen across all acres simultaneously, each of them using the state of all acres at the beginning of the minute and changing to their new form by the end of that same minute. Changes that happen during the minute don't affect each other. @TABLE n_states:3 neighborhood:Moore symmetries:permute # 0 : open # 1 : trees # 2 : lumberyard # don't cares # (multiple vars defined with the same value allows input states # described by those vars not to need to match each other) var a={0,1,2} var b={a} var c={a} var d={a} var e={a} var f={a} var g={a} var h={a} # not trees var i={0,2} var j={i} var k={i} var l={i} var m={i} var n={i} var o={i} var p={i} # not lumberyard var q={0,1} var r={q} var s={q} var t={q} var u={q} var v={q} var w={q} var x={q} # Open with three or more treed neighbors: becomes trees 0,a,b,c,d,e,1,1,1,1 # Trees with three or more lumberyard neigbors: becomes lumberyard 1,a,b,c,d,e,2,2,2,2 # Lumberyard with no treed *and* lumberyard neighbor: becomes open 2,i,j,k,l,m,n,o,2,0 # No trees, one lumberyard 2,q,r,s,t,u,v,w,x,0 # No lumberyards @COLORS 0 51 51 51 dark grey 1 0 102 0 dark green 2 102 51 0 dark brown EOF input=${1:-input.txt} rle=${input%.txt}.rle rle=/tmp/${rle%%*/} set -- $(wc -lL <"$input") sed -f- "$input" <"$rle" 1i\ # $title\n\ \n\ x = $2, y = $1, rule = $rulename:P$2,$1\n s/|/A/g s/#/B/g s/$/$/ EOF golly "$rle" >/dev/null 2>&1 sed "$rle" -Ee ' 1,3d s/[^0-9AB.]*//g y/.AB/otl/ s/([0-9]*)([otl])/\1\2\n/g ' | sed -Ee ' s/^([^0-9])/1\1/ s/([0-9]+)([otl])/\2 += \1/ $a t * l ' | bc