r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


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 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/DrinkinBird Dec 08 '18

Wow, I really went down the wrong path here the first time... Tried to gradually fill the grid items with the values of the neighbors. Finally came up with something half simple though:

NIM

Part1:

import re, rdstdin, strutils, sequtils, algorithm, tables

type
  Coord = object of RootObj
    x, y: int

func present(s: string): bool = len(s) > 0
func toCoord(cs: seq[int]): Coord = Coord(x: cs[0], y: cs[1])

const coords = staticRead("input.txt")
  .splitLines()
  .filter(present)
  .map(proc(s: string): Coord =
    s.split(", ").map(parseInt).toCoord()
  )

const minX = coords.foldl(if a.x < b.x: a else: b).x
const maxX = coords.foldl(if a.x > b.x: a else: b).x
const minY = coords.foldl(if a.y < b.y: a else: b).y
const maxY = coords.foldl(if a.y > b.y: a else: b).y

const adjustedCoord = coords.map(proc(c: Coord): Coord =
  Coord(x: c.x - minX, y: c.y - minY)
)

type
  GridRow = array[maxY - minY + 1, int]
  Grid = array[maxX - minX + 1, GridRow]

var grid: Grid

func getDistance(c: Coord, x, y: int): int =
  abs(c.x - x) + abs(c.y - y)

for x in 0 ..< grid.len:
  for y in 0 ..< grid[x].len:
    var closest = -1
    var distance = int.high

    for i, coord in adjustedCoord:
      if getDistance(coord, x, y) < distance:
        distance = getDistance(coord, x, y)
        closest = i
      elif getDistance(coord, x, y) == distance:
        closest = -1

    grid[x][y] = closest

var infinites = newSeq[int]()
for x in 0 ..< grid.len:
  for y in @[0, grid[x].len - 1]:
    infinites.add(grid[x][y])
for x in @[0, grid.len - 1]:
  for y in 0 ..< grid[x].len:
    infinites.add(grid[x][y])
    
var count = newCountTable[int]()
for x in 0 ..< grid.len:
  for y in 0 ..< grid[x].len:
    let here = grid[x][y]

    if here >= 0 and not infinites.contains(here):
      count.inc(here)

echo count.largest[1]

Part2:

import re, rdstdin, strutils, sequtils, algorithm, tables

type
  Coord = object of RootObj
    x, y: int

func present(s: string): bool = len(s) > 0
func toCoord(cs: seq[int]): Coord = Coord(x: cs[0], y: cs[1])

const coords = staticRead("input.txt")
  .splitLines()
  .filter(present)
  .map(proc(s: string): Coord =
    s.split(", ").map(parseInt).toCoord()
  )

const minX = coords.foldl(if a.x < b.x: a else: b).x
const maxX = coords.foldl(if a.x > b.x: a else: b).x
const minY = coords.foldl(if a.y < b.y: a else: b).y
const maxY = coords.foldl(if a.y > b.y: a else: b).y

const adjustedCoord = coords.map(proc(c: Coord): Coord =
  Coord(x: c.x - minX, y: c.y - minY)
)

type
  GridRow = array[maxY - minY + 1, int]
  Grid = array[maxX - minX + 1, GridRow]

var grid: Grid

func getDistance(c: Coord, x, y: int): int =
  abs(c.x - x) + abs(c.y - y)

var regionSize = 0

for x in 0 ..< grid.len:
  for y in 0 ..< grid[x].len:
    var distanceAll = adjustedCoord
      .map(proc(c: Coord): int = getDistance(c, x, y))
      .foldl(a + b)

    if(distanceAll < 10000):
      inc regionSize

echo regionSize