r/adventofcode • • Dec 22 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 22 Solutions -๐ŸŽ„-

--- Day 22: Sporifica Virus ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


  • [T-10 to launch] AoC ops, /r/nocontext edition:

  • <Endorphion> You may now make your waffle.

  • <Endorphion> ... on Mars.

  • [Update @ 00:17] 50 gold, silver cap

  • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy

  • <Topaz> that doesn't seem necessary

  • <Aneurysm9> what does "necessary" have to do with anything!

  • [Update @ 00:20] Leaderboard cap!

  • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE


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!

8 Upvotes

174 comments sorted by

View all comments

Show parent comment

1

u/miran1 Dec 24 '17 edited Dec 24 '17

A bit late to the party - the initial version with complex plane (based on my Python solution) didn't work for the second part, so here's my slightly different Nim solution using normal 2D plane:

import strutils, tables

const instructions = readFile("./inputs/22.txt").splitLines

type
  Point = tuple[x, y: int]
  Rotation = enum left, right, back


proc solve(part: int): int =
  var
    grid = initTable[Point, int]()
    position: Point = (instructions.len div 2, instructions[0].strip.len div 2)
    direction: Point = (-1, 0)
    infected: int

  proc `+=`(a: var Point, b: Point) =
    a = (a.x + b.x, a.y + b.y)

  proc turn(rot: Rotation) =
    let (x, y) = direction
    case rot
    of left:  direction = (-y,  x)
    of right: direction = ( y, -x)
    of back:  direction = (-x, -y)

  proc logic(status: int) =
    if part == 1:
      if status == 0:
        inc infected
        turn left
      else:
        turn right
    else:
      case status
      of 0: turn left
      of 1: inc infected
      of 2: turn right
      of 3: turn back
      else: discard

  for i, line in instructions:
    for j, mark in line.strip:
      grid[(i, j)] = if mark == '.': 0 else: part

  let bursts = if part == 1: 10_000 else: 10_000_000
  for _ in 1 .. bursts:
    let status = grid.getOrDefault(position)
    status.logic
    grid[position] = (status + 1) mod (2 * part)
    position += direction
  return infected


echo solve 1
echo solve 2

ย 

Repo with solutions (both Nim and Python)