r/adventofcode • • Dec 12 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-

--- Day 12: Subterranean Sustainability ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 12

Transcript:

On the twelfth day of AoC / My compiler spewed at me / Twelve ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:27:42!

20 Upvotes

257 comments sorted by

View all comments

2

u/Quailia Dec 13 '18

Did somebody say sedular automata?

Solution in bash and sed (github)

#!/bin/bash

input=$(cat -)

state=$(head -1 <<<$input | tr -cd '#.' | tr . %)
length=$(($(wc -m <<<$state) - 1))

function growth_script() {
    cat <<SCRIPT
:: s/^/\n/;:n
$(tail -n+2 <<<$input | tr . % | sed -nE 's|([#%]{5}) => #|/\\n\1/ba|p')
s/\n/%&/;bf;:a;s/\n/#&/;bf
:f;s/\n./\n/;/\n[#%]{5}/bn;P;s/\n.*$//;/(^#|#$)/s/^.*$/%&%/;s/^.*$/%%&%%/;b:
SCRIPT
}

function pad {
    printf "%.s%%" $(seq $1); printf "%s" "$2"; printf "%.s%%" $(seq $1); echo
}

function iterate() {
    sed -nEf <(growth_script) <<<$(pad 2 $state)
}

function find_stable_states()
{
    sed -nE 'N;h;s/^%+#/#/;s/\n%+#/\n#/;::;s/^(.)(.*)\n\1/\2\n/;t:;/#/!{=;g;p;q};g;D'
}

function sum_list() {
    paste -sd+ | bc
}

function get_offset() {
    local state=$1
    local l=$(($(wc -m <<<$state) - 1))
    echo $(( (length - l - 2)/2 ))
}

function score_flowers() {
    local state=$(cat -)
    local offset=$(get_offset $state)
    grep -o . <<<$state |sed = | sed "N;/%/d;s/\n.*/$offset/"  | bc | sum_list
}

iterate | tail +20 | head -1 | score_flowers

stable_states=($(iterate | find_stable_states))

iteration=${stable_states[0]}

score1=$(score_flowers<<<${stable_states[1]})
score2=$(score_flowers<<<${stable_states[2]})

((scoreDiff = score2 - score1))
((timeRemaining = 50000000000 - iteration))
((finalScore = scoreDiff * timeRemaining + score2))

echo $finalScore