Let's see how far I get this year.
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.

day05-1.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from helpers import Helper
  2. helper = Helper(debug=True)
  3. load_input = helper.load_input
  4. debug = helper.debug
  5. def main():
  6. lines = load_input(5)
  7. maps_dict = {
  8. "seeds": [],
  9. "seed-to-soil": [],
  10. "soil-to-fertilizer": [],
  11. "fertilizer-to-water": [],
  12. "water-to-light": [],
  13. "light-to-temperature": [],
  14. "temperature-to-humidity": [],
  15. "humidity-to-location": [],
  16. }
  17. maps_keys = list(maps_dict.keys())
  18. maps_dict["seeds"] = [int(n) for n in lines[0].split()[1:]]
  19. seeds_list = [{"seed": seed} for seed in maps_dict["seeds"]]
  20. current_key = ""
  21. for line in lines[1:]:
  22. if line == "":
  23. continue
  24. split_line = line.split()
  25. if split_line[0] in maps_keys:
  26. current_key = split_line[0]
  27. else:
  28. maps_dict[current_key].append({
  29. x: int(a)
  30. for (x, a)
  31. in zip(["destination", "source", "length"], split_line)}
  32. )
  33. for seed in seeds_list:
  34. for key in maps_keys[1:]:
  35. source, _, destination = key.split("-")
  36. i = 0
  37. t_list = maps_dict[key]
  38. while i < len(t_list):
  39. s_map = t_list[i]
  40. if seed[source] in range(s_map["source"], (s_map["source"] + s_map["length"])):
  41. source_distance = seed[source] - s_map["source"]
  42. seed[destination] = s_map["destination"] + source_distance
  43. debug("{}: {} found in {}+{}, {} is {} + {}".format(
  44. source,
  45. seed[source],
  46. s_map["source"],
  47. s_map["length"],
  48. seed[destination],
  49. s_map["destination"],
  50. source_distance
  51. ))
  52. i = len(t_list)
  53. else:
  54. debug("{}: {} not found in {}+{}, using original".format(
  55. source,
  56. seed[source],
  57. s_map["source"],
  58. s_map["length"]
  59. ))
  60. i += 1
  61. if not seed.get(destination, None):
  62. seed[destination] = seed[source]
  63. print(seeds_list)
  64. print(min(seeds_list, key=lambda x: x["location"]))
  65. if __name__ == "__main__":
  66. main()