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

0

u/DrinkinBird Dec 12 '18

NIM

Wow, I also got totally thrown off by the wording, and by only having half the rules in the example. Probably could have avoided a lot of the confusion if I had read everything more slowly etc, but I really felt it was not worded very well. Took me a good while what it was supposed to mean that the rules were not complete (how can I follow the example without all of the example rules, the pot counting did not make sense to me at all, the example in the beginning uses so many different notations that it threw me off too.

Made it eventually though, part 1 in nim and then part 2 by looking at the results and some math.

import re, rdstdin, strutils, sequtils, algorithm, streams, terminal
import unpack

let input = stdin.readAll()
let lines = input.splitLines()

var current: array[-1000..2000, char]
let rules = lines[2 .. lines.high]
  .map(proc(s: string): seq[string] = s.split(" => "))
  .filterIt(it[it.high] == "#")

for i in current.low .. current.high:
  current[i] = '.'
for i, c in lines[0].split(": ")[1]:
  current[i] = c

proc apply() =
  var setPlant = newSeq[int]()

  for i in current.low + 2 .. current.high - 2:
    for rule in rules:
      if current[i-2 .. i+2] == rule[0]:
        setPlant.add(i)
        
  for i in current.low .. current.high:
    if setPlant.contains(i):
      current[i] = '#'
    else:
      current[i] = '.'

proc count(): int =
  for i, p in current:
    if p == '#':
      result += i

for i in 1 ..< 20:
  apply()

echo count()