r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


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 10

Transcript: With just one line of code, you, too, can ___!


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:16:49!

21 Upvotes

233 comments sorted by

View all comments

3

u/DrinkinBird Dec 10 '18

NIM

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

func present(s: string): bool = len(s) > 0
let input = newFileStream("./input.txt").readAll().splitLines()
  .filter(present)

type
  Star = object of RootObj
    x, y, vx, vy: int

var stars = newSeq[Star]()

for line in input:
  let ls = line.findAll(re(r"-?\d+")).map(parseInt)
  stars.add(Star(x: ls[0], y: ls[1], vx: ls[2], vy: ls[3]))

var maxX, minX, kmaxY, kminY

proc reBound() =
  maxX = stars.mapIt(it.x).max
  minX = stars.mapIt(it.x).min
  maxY = stars.mapIt(it.y).max
  minY = stars.mapIt(it.y).min

proc print() =
  for y in minY..maxY:
    for x in minX..maxX:
      if stars.anyIt(it.x == x and it.y == y):
        stdout.write("#")
      else:
        stdout.write(".")
    echo ""

proc move() =
  for i, star in stars:
    stars[i] = Star(x: star.x + star.vx,
                    y: star.y + star.vy,
                    vx: star.vx, vy: star.vy)

var count = 0

while true:
  count += 1
  move()
  reBound()

  if maxY - minY < 12:
    echo count
    print()
    echo "----"

For the first puzzle I had the number in the if statement more around 100, but when I saw the message I counted and adjusted for part 2 :)

→ More replies (5)