My solutions to 2018's advent of code
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

day10.nim 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import math, sequtils, strscans, strutils
  2. # Expected height of our letters
  3. const LetterHeight = 10
  4. type
  5. Point = ref object
  6. sx, sy, x, y, dx, dy: int
  7. var
  8. points: seq[Point]
  9. miny = int.high
  10. maxy = int.low
  11. mindy, maxdy = 0
  12. for line in readFile("data/10.txt").strip.splitlines:
  13. var point = new(Point)
  14. if not line.scanf("position=<$s$i,$s$i> velocity=<$s$i,$s$i>", point.sx, point.sy, point.dx, point.dy):
  15. raise newException(Defect, "Invalid input line: " & line)
  16. if point.sy < miny:
  17. miny = point.sy
  18. mindy = point.dy
  19. if point.sy > maxy:
  20. maxy = point.sy
  21. maxdy = point.dy
  22. points.add(point)
  23. # Compute the first time at which the most outlying stars will be within
  24. # letter-height of each other. It may take a few more iterations after
  25. # this for them to get into their final place.
  26. var t = (maxy - miny - LetterHeight) div (mindy - maxdy)
  27. while true:
  28. var
  29. minx = int.high
  30. maxx = int.low
  31. miny = int.high
  32. maxy = int.low
  33. for point in points:
  34. point.x = point.sx + point.dx * t
  35. point.y = point.sy + point.dy * t
  36. minx = min(minx, point.x)
  37. maxx = max(maxx, point.x)
  38. miny = min(miny, point.y)
  39. maxy = max(maxy, point.y)
  40. if maxy - miny == LetterHeight - 1:
  41. for y in miny..maxy:
  42. var grid = newSeq[bool](1 + maxx - minx)
  43. for point in points:
  44. if point.y == y:
  45. grid[point.x - minx] = true
  46. echo grid.map(proc(cell: bool): string =
  47. if cell:
  48. "█"
  49. else:
  50. " ").join
  51. echo t
  52. break
  53. t.inc