day5.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import os
  2. with open(os.path.join(os.path.dirname(__file__), "example.txt")) as example:
  3. example_data = example.read().splitlines()
  4. with open(os.path.join(os.path.dirname(__file__), "input.txt")) as example:
  5. input_data = example.read().splitlines()
  6. def parse(example_data):
  7. ind = 0
  8. while ind < len(example_data):
  9. if example_data[ind].startswith("seeds:"):
  10. seeds = [int(n) for n in example_data[ind][7:].split()]
  11. elif example_data[ind].startswith("seed-to-soil map:"):
  12. ind += 1
  13. seed_to_soil = []
  14. while example_data[ind] != "":
  15. seed_to_soil.append([int(n) for n in example_data[ind].split()])
  16. ind += 1
  17. elif example_data[ind].startswith("soil-to-fertilizer map:"):
  18. ind += 1
  19. soil_to_fertilizer = []
  20. while example_data[ind] != "":
  21. soil_to_fertilizer.append([int(n) for n in example_data[ind].split()])
  22. ind += 1
  23. elif example_data[ind].startswith("fertilizer-to-water map:"):
  24. ind += 1
  25. fertilizer_to_water = []
  26. while example_data[ind] != "":
  27. fertilizer_to_water.append([int(n) for n in example_data[ind].split()])
  28. ind += 1
  29. elif example_data[ind].startswith("water-to-light map:"):
  30. ind += 1
  31. water_to_light = []
  32. while example_data[ind] != "":
  33. water_to_light.append([int(n) for n in example_data[ind].split()])
  34. ind += 1
  35. elif example_data[ind].startswith("light-to-temperature map:"):
  36. ind += 1
  37. light_to_temperature = []
  38. while example_data[ind] != "":
  39. light_to_temperature.append([int(n) for n in example_data[ind].split()])
  40. ind += 1
  41. elif example_data[ind].startswith("temperature-to-humidity map:"):
  42. ind += 1
  43. temperature_to_humidity = []
  44. while example_data[ind] != "":
  45. temperature_to_humidity.append(
  46. [int(n) for n in example_data[ind].split()]
  47. )
  48. ind += 1
  49. elif example_data[ind].startswith("humidity-to-location map:"):
  50. ind += 1
  51. humidity_to_location = []
  52. while ind < len(example_data):
  53. humidity_to_location.append([int(n) for n in example_data[ind].split()])
  54. ind += 1
  55. ind += 1
  56. return (
  57. seeds, # type: ignore
  58. seed_to_soil, # type: ignore
  59. soil_to_fertilizer, # type: ignore
  60. fertilizer_to_water, # type: ignore
  61. water_to_light, # type: ignore
  62. light_to_temperature, # type: ignore
  63. temperature_to_humidity, # type: ignore
  64. humidity_to_location, # type: ignore
  65. ) # type: ignore
  66. def get_maps(
  67. seeds,
  68. seed_to_soil,
  69. soil_to_fertilizer,
  70. fertilizer_to_water,
  71. water_to_light,
  72. light_to_temperature,
  73. temperature_to_humidity,
  74. humidity_to_location,
  75. ):
  76. mapping = {
  77. 0: seed_to_soil,
  78. 1: soil_to_fertilizer,
  79. 2: fertilizer_to_water,
  80. 3: water_to_light,
  81. 4: light_to_temperature,
  82. 5: temperature_to_humidity,
  83. 6: humidity_to_location,
  84. }
  85. location = None
  86. for seed in seeds:
  87. temp = seed
  88. for m in mapping.values():
  89. for d, s, r in m:
  90. if temp <= s + r - 1 and temp >= s:
  91. temp = temp + d - s
  92. break
  93. location = min(temp, location) if location else temp
  94. print(location)
  95. return location
  96. def get_ranges(
  97. seeds,
  98. seed_to_soil,
  99. soil_to_fertilizer,
  100. fertilizer_to_water,
  101. water_to_light,
  102. light_to_temperature,
  103. temperature_to_humidity,
  104. humidity_to_location,
  105. ):
  106. mapping = {
  107. 0: seed_to_soil,
  108. 1: soil_to_fertilizer,
  109. 2: fertilizer_to_water,
  110. 3: water_to_light,
  111. 4: light_to_temperature,
  112. 5: temperature_to_humidity,
  113. 6: humidity_to_location,
  114. }
  115. location = None
  116. for seed, ran in list(zip(seeds[0::2], seeds[1::2])):
  117. seed_intervals = [(seed, seed + ran - 1)]
  118. for m in mapping.values():
  119. temp = []
  120. for start, end in seed_intervals:
  121. for d, s, r in m:
  122. if s <= start and end < s + r:
  123. temp.append((start - s + d, end - s + d))
  124. break
  125. elif s > start and s <= end and end < s + r:
  126. seed_intervals.append((start, s - 1))
  127. temp.append((d, d + end - s))
  128. break
  129. elif start < s + r and end >= s + r and start >= s:
  130. seed_intervals.append((s + r, end))
  131. temp.append((d + start - s, d + r - 1))
  132. break
  133. elif start < s and end >= s + r:
  134. seed_intervals.append((start, s - 1))
  135. temp.append((d, d + r - 1))
  136. seed_intervals.append((s + r, end))
  137. break
  138. else:
  139. temp.append((start, end))
  140. seed_intervals = temp
  141. location = (
  142. min(min(seed_intervals)[0], location)
  143. if location
  144. else min(seed_intervals)[0]
  145. )
  146. print(location)
  147. return location
  148. get_maps(*parse(example_data))
  149. get_maps(*parse(input_data))
  150. get_ranges(*parse(example_data))
  151. get_ranges(*parse(input_data))