solution.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python3
  2. import sys
  3. import re
  4. from itertools import product
  5. # part 1
  6. def simulate_factory(costs, plan, time=24):
  7. robots = [1, 0, 0, 0]
  8. resources = [0, 0, 0, 0]
  9. limit = time
  10. pi = 0
  11. builds = len(plan)
  12. while time > 0:
  13. # if anything is left to be built
  14. #if pi < builds:
  15. next_robots = robots.copy()
  16. if plan[pi] == '3' and resources[0] >= costs[3][0] and resources[2] >= costs[3][2]:
  17. resources[0] -= costs[3][0]
  18. resources[2] -= costs[3][2]
  19. next_robots[3] += 1
  20. pi += 1
  21. elif plan[pi] == '2' and resources[0] >= costs[2][0] and resources[1] >= costs[2][1]:
  22. resources[0] -= costs[2][0]
  23. resources[1] -= costs[2][1]
  24. next_robots[2] += 1
  25. pi += 1
  26. elif plan[pi] == '1' and resources[0] >= costs[1][0]:
  27. resources[0] -= costs[1][0]
  28. next_robots[1] += 1
  29. pi += 1
  30. elif plan[pi] == '0' and resources[0] >= costs[0][0]:
  31. resources[0] -= costs[0][0]
  32. next_robots[0] += 1
  33. pi += 1
  34. for i in range(len(robots)):
  35. resources[i] += robots[i]
  36. #print(next_robots, resources, limit - time + 1)
  37. robots = next_robots
  38. time -= 1
  39. geodes = resources[3]
  40. return geodes
  41. def part1():
  42. blueprints = []
  43. for line in sys.stdin:
  44. res = re.match(r'Blueprint ([0-9]+): Each ore robot costs ([0-9]+) ore. Each clay robot costs ([0-9]+) ore. Each obsidian robot costs ([0-9]+) ore and ([0-9]+) clay. Each geode robot costs ([0-9]+) ore and ([0-9]+) obsidian.', line.strip())
  45. if not res: continue
  46. blueprint = res.groups()
  47. costs = [int(b) for b in blueprint[1:]]
  48. cost_matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
  49. cost_matrix[0][0] = costs[0]
  50. cost_matrix[1][0] = costs[1]
  51. cost_matrix[2][0] = costs[2]
  52. cost_matrix[2][1] = costs[3]
  53. cost_matrix[3][0] = costs[4]
  54. cost_matrix[3][2] = costs[5]
  55. blueprints.append((blueprint[0], cost_matrix))
  56. print(blueprints)
  57. qualities = []
  58. for blueprint in blueprints:
  59. print('simulating blueprint', blueprint)
  60. max_geodes = 0
  61. for plan in product('3210', repeat=24):
  62. if not ('0' in plan or '1' in plan or '2' in plan or '3' in plan): continue
  63. #print(plan)
  64. theoretical_max = 0
  65. for i, robot in enumerate(plan):
  66. if robot == '3':
  67. theoretical_max += (24 - i)
  68. if theoretical_max < max_geodes: continue
  69. geodes = simulate_factory(blueprint[1], plan)
  70. #if geodes > max_geodes: print(plan, geodes, max_geodes)
  71. max_geodes = max(max_geodes, geodes)
  72. qualities.append((int(blueprint[0]), max_geodes, int(blueprint[0]) * max_geodes))
  73. print(qualities)
  74. # part 2
  75. def part2():
  76. pass
  77. if sys.argv[1] in '1':
  78. part1()
  79. else:
  80. part2()