Skip to content

Commit

Permalink
[2022][day 19] add part 1 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IAjimi committed Dec 19, 2022
1 parent d5fa298 commit ab3b186
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
146 changes: 146 additions & 0 deletions 2022/AOC19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import functools
import parse
from typing import List, Tuple, Set

from _utils import read_input, timer, Solution


def process_input(filename: str):
_input = read_input(filename)
blueprints = []
for line in _input:
_id, o_o, c_o, obs_o, obs_c, geo_o, geo_obs = tuple(parse.parse("Blueprint {:d}: Each ore robot costs {:d} ore. Each clay robot costs {:d} ore. Each obsidian robot costs {:d} ore and {:d} clay. Each geode robot costs {:d} ore and {:d} obsidian.", line).fixed)
blueprints.append(((o_o,0,0),(c_o,0,0),(obs_o,obs_c,0),(geo_o,0,geo_obs)))
return blueprints

@functools.lru_cache(maxsize=None)
def collect_geodes(
t: int, ore_robot: int, clay_robot: int, obs_robot: int, geode_robot: int, ore: int, clay: int, obisidian: int, geode: int, blueprint
) -> int:
if t <= 0:
return geode

options = []
max_ore_spend = max(o for o,*_ in blueprint)
max_clay_spend = max(c for _,c,*_ in blueprint)
max_obs_spend = max(obs for _,_,obs in blueprint)

new_ore = min(ore + ore_robot, max_ore_spend * t) # most ore you could spend in rest of run
new_clay = min(clay + clay_robot, max_clay_spend * t)
new_obs = min(obisidian + obs_robot, max_obs_spend * t)
new_geode = geode + geode_robot

# always build geode robot
if (
blueprint[3][0] <= ore
and blueprint[3][2] <= obisidian
):
options.append(
collect_geodes(
t - 1,
ore_robot,
clay_robot,
obs_robot,
geode_robot + 1,
new_ore - blueprint[3][0],
new_clay,
new_obs - blueprint[3][2],
new_geode,
blueprint
)
)
else:
# build nothing
options.append(
collect_geodes(
t - 1,
ore_robot,
clay_robot,
obs_robot,
geode_robot,
new_ore,
new_clay,
new_obs,
new_geode,
blueprint
)
)

# build new ore robot
if blueprint[0][0] <= ore:
options.append(
collect_geodes(
t - 1,
ore_robot + 1,
clay_robot,
obs_robot,
geode_robot,
new_ore - blueprint[0][0],
new_clay,
new_obs,
new_geode,
blueprint
)
)

# build new clay robot
if blueprint[1][0] <= ore:
options.append(
collect_geodes(
t - 1,
ore_robot,
clay_robot + 1,
obs_robot,
geode_robot,
new_ore - blueprint[1][0],
new_clay,
new_obs,
new_geode,
blueprint
)
)

# build new obsidian robot
if (
blueprint[2][0] <= ore
and blueprint[2][1] <= clay
):
options.append(
collect_geodes(
t - 1,
ore_robot,
clay_robot,
obs_robot + 1,
geode_robot,
new_ore - blueprint[2][0],
new_clay - blueprint[2][1],
new_obs,
new_geode,
blueprint
)
)

return max(options)


def part1(blueprints) -> int:
# 43 seconds for 2 blueprints.. -> expected to run for 10 min
quality_levels = []
for i, bp in enumerate(blueprints, start=1):
geodes = collect_geodes(24, 1, 0, 0, 0, 0, 0, 0, 0, bp)
print(f"max geodes collected with blueprint {i} are {geodes}")
quality_levels.append((i)*geodes)
return sum(quality_levels)

@timer
def main(filename: str) -> Solution:
blueprints = process_input(filename)
part_1_solution = part1(blueprints)
part_2_solution = 0
return part_1_solution, part_2_solution


if __name__ == "__main__":
part_1_solution, part_2_solution = main("aoc19.txt")
print(f"PART 1: {part_1_solution}") # 1092
print(f"PART 2: {part_2_solution}") #
4 changes: 4 additions & 0 deletions 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ This repository contains Python solutions to the 2022 edition of [Advent of Code
| [13](https://adventofcode.com/2022/day/13) | **Distress Signal** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC13.py) |
| [14](https://adventofcode.com/2022/day/14) | **Regolith Reservoir** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC14.py) |
| [15](https://adventofcode.com/2022/day/15) | **Beacon Exclusion Zone** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC15.py) |
| [16](https://adventofcode.com/2022/day/16) | **Proboscidea Volcanium** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC16.py) |
| [17](https://adventofcode.com/2022/day/17) | **Pyroclastic Flow**: Tetris emulator | [:star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC17.py) |
| [18](https://adventofcode.com/2022/day/18) | **Boiling Boulders**: Exploring 3D grid | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC18.py) |
| [19](https://adventofcode.com/2022/day/19) | **Not Enough Minerals** | [:star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC19.py) |

0 comments on commit ab3b186

Please sign in to comment.