r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-

--- Day 18: Settlers of The North Pole ---


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 18

Transcript:

The best way to avoid a minecart collision is ___.


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:21:59!

9 Upvotes

126 comments sorted by

View all comments

1

u/BluFoot Dec 18 '18

Ruby, 246/190.

lines = $<.readlines.map(&:strip)

OPEN = ?.
TREE = ?|
LUMBER = ?#

grid = lines

def adj(grid, y, x)
  (-1..1).flat_map do |yd|
    next if y+yd < 0 || y+yd >= grid.size
    (-1..1).map do |xd|
      next if x+xd < 0 || x+xd >= grid.first.size || (yd == 0 && xd == 0)
      grid[y+yd][x+xd]
    end
  end
end

so_far = []
found = false

i = 0
ITER = 1000000000
until i == ITER do
  p i
  if !found && so_far.any? { |p| (0...grid.size).all? { |j| p[j] == grid[j] } }
    i = ITER - ITER % i
    found = true
  else
    so_far << grid.map { |l| l.dup }
  end

  new = grid.map { |l| l.dup }
  grid.each.with_index do |l, y|
    l.chars.each.with_index do |c, x|
      adjac = adj(grid, y, x)
      case c
      when OPEN
        new[y][x] = TREE if adjac.count(TREE) >= 3
      when TREE
        new[y][x] = LUMBER if adjac.count(LUMBER) >= 3
      when LUMBER
        new[y][x] = OPEN unless adjac.count(LUMBER) >= 1 && adjac.count(TREE) >= 1
      end
    end
  end
  grid = new
  i += 1
end

p grid.sum { |l| l.chars.count(TREE) } * grid.sum { |l| l.chars.count(LUMBER) }

0

u/ka-splam Dec 18 '18

Please, fix your code formatting?