My solutions to 2018's advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

day10.nim 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import math, sequtils, strscans, strutils
  2. type
  3. Point = ref object
  4. x, y, dx, dy: int
  5. var points: seq[Point]
  6. for line in readFile("data/10.txt").strip.splitlines:
  7. var point = new(Point)
  8. if not line.scanf("position=<$s$i,$s$i> velocity=<$s$i,$s$i>", point.x, point.y, point.dx, point.dy):
  9. raise newException(Defect, "Invalid input line: " & line)
  10. points.add(point)
  11. var
  12. t = 0
  13. last_distance = int.high
  14. while true:
  15. var
  16. minx = int.high
  17. maxx = int.low
  18. miny = int.high
  19. maxy = int.low
  20. for point in points:
  21. point.x += point.dx
  22. point.y += point.dy
  23. minx = min(minx, point.x)
  24. maxx = max(maxx, point.x)
  25. miny = min(miny, point.y)
  26. maxy = max(maxy, point.y)
  27. var distance = (maxx - minx) * (maxy - miny)
  28. if distance > last_distance:
  29. for y in miny..maxy:
  30. var grid = newSeq[bool](1 + maxx - minx)
  31. for point in points:
  32. if point.y - point.dy == y:
  33. grid[point.x - point.dx - minx] = true
  34. echo grid.map(proc(cell: bool): string =
  35. if cell:
  36. "█"
  37. else:
  38. " ").join
  39. echo t
  40. break
  41. else:
  42. last_distance = distance
  43. t.inc