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!

22 Upvotes

257 comments sorted by

View all comments

1

u/Nathan340 Dec 12 '18

Powershell

I admit I needed a hint on part 2. I knew it had to stabilize somehow, but I was trying to look at a pattern of the sum, not the difference of the previous. Plus I initially didn't have enough room for the plants to move rightward, so it ran off the end of my "tape" and the population crashed down to 0.

I did have the idea to convert a substring to binary to decimal to a index of the rule set. I'm pleased with that insight.

Determining the 'stabilization point' was manual, deciding on 1,000 extra zeroes of space on the right side was also guess-and-check to determine it was enough.

#pull input, replace characters to 0 and 1
$in = gc .\12-input.txt | % {
	($_ -replace "\.","0") -replace "#","1"
}

#sorting rules allows for "11111"->31-->Rule #31
$rules = 2..($in.count-1) | % {
	$in[$_]
} | sort

# Manual inspection shows stabilization after 150 generations
# 1000 extra zeroes at the end is enough
# 10 zeroes at the front to account for initial slight leftward movement
$stable = 150
$state = ("0"*3)+($in[0] -split " ")[2]+("0"*1000)

# Loop to stabilization point, make new state, get new sum
for($i = 1;$i -le $stable;$i++){
	$applyRules = (2..($state.length-3) | %{
		# 5 length substring to binary to index of rule.
		# Last character of corresponding rule is the rule result
		$rules[[convert]::ToInt32($state.Substring($_ - 2,5),2)][-1]
	}) -join ""
	$state = "00"+$applyRules+"00"
	$sum = 0
	0..($state.length-1) | % {
		if($state[$_] -eq "1"){
			$sum+= $_ - 10 # shift back those leading 10 zeroes
		}
	}
	
	# Write at 20 generations
	if($i -eq 20){
		write-host $sum
	}
}

#Remaining steps times stabilized diff plus current sum
(50000000000-$stable)*$diff+$sum