My solutions to 2018's advent of code
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

day06.nim 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import math, sequtils, strutils
  2. type
  3. Point = array[2, int]
  4. func read_coords(text: string): seq[Point] =
  5. for line in text.strip.splitlines:
  6. let parts = line.split(", ").map(parseInt)
  7. result &= [parts[0], parts[1]]
  8. func bounds(coords: seq[Point]): array[4, int] =
  9. let
  10. xs = coords.map(proc(coord: Point): int = coord[0])
  11. ys = coords.map(proc(coord: Point): int = coord[1])
  12. result = [min(xs), max(xs), min(ys), max(ys)]
  13. func distance(point1: Point, point2: Point): int =
  14. abs(point1[0] - point2[0]) + abs(point1[1] - point2[1])
  15. func nearest(coords: seq[Point], point: Point): int =
  16. let
  17. distances = coords.map(proc(coord: Point): int = coord.distance(point))
  18. lowest = min(distances)
  19. lowest_count = distances.filter(proc (distance: int): bool = distance == lowest).len
  20. if lowest_count == 1:
  21. distances.find(lowest)
  22. else:
  23. -1
  24. func inrange(coords: seq[Point], point: Point): bool =
  25. coords.map(proc (coord: Point): int = coord.distance(point)).sum < 10000
  26. func part1(coords: seq[Point], extents: array[4, int]): int =
  27. var counts = newSeqWith(coords.len, 0)
  28. for x in extents[0] .. extents[1]:
  29. for y in extents[2] .. extents[3]:
  30. let coord = coords.nearest([x, y])
  31. if coord != -1 and counts[coord] > -1:
  32. counts[coord].inc
  33. # If the area reaches the edge of the grid, it will continue infinitely.
  34. # Mark that area as excluded.
  35. if x == extents[0] or x == extents[1] or y == extents[2] or y == extents[3]:
  36. counts[coord] = -1
  37. counts.max
  38. func part2(coords: seq[Point], extents: array[4, int]): int =
  39. var
  40. size = 0
  41. found = false
  42. let
  43. extension = int(10000 / coords.len)
  44. y_start = extents[2] - extension
  45. y_finish = extents[3] + extension
  46. y_range = y_finish - y_start
  47. # The region we're in is going to be contiguous so once we've found some
  48. # 'safe' areas and subsequently hit a row/column with none in, we can abort
  49. for x in extents[0] - extension .. extents[1] + extension:
  50. var
  51. min_y = int.high
  52. max_y = int.high
  53. for y in y_start .. y_finish:
  54. if coords.inrange([x,y]):
  55. if min_y == int.high:
  56. min_y = y
  57. max_y = y
  58. elif max_y != int.high:
  59. break
  60. if min_y != int.high:
  61. size += abs(max_y - min_y) + 1
  62. found = true
  63. elif found:
  64. break
  65. size
  66. let
  67. coords = read_coords(readFile("data/06.txt"))
  68. extents = coords.bounds
  69. echo part1(coords, extents)
  70. echo part2(coords, extents)