Let's see how far I get this year.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

day05-2.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from helpers import Helper
  2. helper = Helper(debug=True)
  3. load_input = helper.load_input
  4. debug = helper.debug
  5. def within(target, start, end):
  6. if start == end:
  7. return target == start
  8. if start > end:
  9. start, end = end, start
  10. return target >= start and end >= target
  11. def main():
  12. lines = load_input(5)
  13. maps_dict = {
  14. "seeds": [],
  15. "seed-to-soil": [],
  16. "soil-to-fertilizer": [],
  17. "fertilizer-to-water": [],
  18. "water-to-light": [],
  19. "light-to-temperature": [],
  20. "temperature-to-humidity": [],
  21. "humidity-to-location": [],
  22. }
  23. maps_keys = list(maps_dict.keys())
  24. maps_dict["seeds"] = [int(n) for n in lines[0].split()[1:]]
  25. real_seeds = []
  26. i, j = 0, 1
  27. while j < len(maps_dict["seeds"]):
  28. real_seeds.append((maps_dict["seeds"][i], maps_dict["seeds"][i] + maps_dict["seeds"][j]))
  29. # new_start = maps_dict["seeds"][i]
  30. # new_end = new_start + maps_dict["seeds"][j]
  31. # debug(f"Adding seeds in range {new_start}-{new_end}")
  32. # new_seeds = range(new_start, new_end)
  33. # debug(new_seeds)
  34. # real_seeds.append(new_seeds)
  35. # debug(real_seeds)
  36. i += 2
  37. j += 2
  38. seeds_list = [{"seed": seed[1]} for seed in real_seeds]
  39. current_key = ""
  40. for line in lines[1:]:
  41. if line == "":
  42. continue
  43. split_line = line.split()
  44. if split_line[0] in maps_keys:
  45. current_key = split_line[0]
  46. else:
  47. maps_dict[current_key].append({
  48. x: int(a)
  49. for (x, a)
  50. in zip(["destination", "source", "length"], split_line)}
  51. )
  52. for seed in seeds_list:
  53. for key in maps_keys[1:]:
  54. source, _, destination = key.split("-")
  55. i = 0
  56. t_list = maps_dict[key]
  57. while i < len(t_list):
  58. s_map = t_list[i]
  59. # debug(f"Checking {seed[source]} against {s_map['source']}, {s_map['source'] + s_map['length']}")
  60. if within(seed[source], s_map["source"], (s_map["source"] + s_map["length"])):
  61. source_distance = seed[source] - s_map["source"]
  62. seed[destination] = s_map["destination"] + source_distance
  63. # debug("{}: {} found in {}+{}, {} is {} + {}".format(
  64. # source,
  65. # seed[source],
  66. # s_map["source"],
  67. # s_map["length"],
  68. # seed[destination],
  69. # s_map["destination"],
  70. # source_distance
  71. # ))
  72. i = len(t_list)
  73. else:
  74. # debug("{}: {} not found in {}+{}, using original".format(
  75. # source,
  76. # seed[source],
  77. # s_map["source"],
  78. # s_map["length"]
  79. # ))
  80. i += 1
  81. if not seed.get(destination, None):
  82. seed[destination] = seed[source]
  83. debug("\n".join([f"{k}: {v}" for k, v in seed.items() for seed in seeds_list]))
  84. print(min(seeds_list, key=lambda x: x["location"]))
  85. if __name__ == "__main__":
  86. main()