Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2022-12-19 - Not Enough Minerals

(original .ipynb)
from pprint import pprint

puzzle_input_str = open("./puzzle_input/day19.txt").read()
test_input_str = open("./test_input/day19.txt").read()

# Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 4 ore. 
# Each obsidian robot costs 2 ore and 7 clay. Each geode robot costs 4 ore and 13 obsidian.
def parse_blueprint(line):
    tokens = line.split(" ")
    return (
        int(tokens[1].rstrip(":")), 
        [
            (0, int(tokens[30]), 0, int(tokens[27])), # geode robot
            (0, 0, int(tokens[21]), int(tokens[18])), # obsidian robot
            (0, 0,               0, int(tokens[12])), # clay robot 
            (0, 0,               0, int(tokens[6]))   # ore robot
        ]
    )

def can_build(resources, blueprint):
    possible_bots = []
    for have, required in zip(resources, blueprint):
        if required > 0:
            possible_bots.append(have // required)
    
    print("can build", possible_bots)
    return min(possible_bots) if len(possible_bots) > 0 else 0


def foo(blueprint_id, blueprints, minutes=24):
    # all of these collections are (blueprints, robots, resources) have the
    # following order:
    #     geode -> obsidian -> clay -> ore
    robots    = [0, 0, 0, 1]
    resources = [0, 0, 0, 0]
    
    # do some mining
    while minutes > 0:
        print()
        pprint(robots)
        pprint(resources)
        for i, robot in enumerate(robots):
            resources[i] += robot

        for i, blueprint in enumerate(blueprints):
            count = can_build(resources, blueprint)
            if count > 0:
                print(f"makin {count} robots of type", i)
                for r, _ in enumerate(resources):
                    resources[r] -= count * blueprint[i]
                robots[i] += count

        minutes -= 1
    
    print("HERE WE GO:")
    pprint(robots)
    pprint(resources)
    return robots[0]
    
def part_one(input_str):
    blueprints = [parse_blueprint(line) for line in input_str.splitlines()]

    max_n = 0
    for blueprint in blueprints:
        pprint(blueprint)
        n = foo(*blueprint)
        max_n = max(max_n, n)
        break
    
    
    print(max_n)
    #print(input_str)
    return 0


print(part_one(test_input_str))

#print("part one:", part_one(puzzle_input_str))
(1, [(0, 7, 0, 2), (0, 0, 14, 3), (0, 0, 0, 2), (0, 0, 0, 4)])

[0, 0, 0, 1]
[0, 0, 0, 0]
can build [0, 0]
can build [0, 0]
can build [0]
can build [0]

[0, 0, 0, 1]
[0, 0, 0, 1]
can build [0, 1]
can build [0, 0]
can build [1]
makin 1 robots of type 2
can build [0]

[0, 0, 1, 1]
[0, 0, 0, 2]
can build [0, 1]
can build [0, 1]
can build [1]
makin 1 robots of type 2
can build [0]

[0, 0, 2, 1]
[0, 0, 1, 3]
can build [0, 2]
can build [0, 1]
can build [2]
makin 2 robots of type 2
can build [1]
makin 1 robots of type 3

[0, 0, 4, 2]
[-4, -4, -1, 0]
can build [-1, 1]
can build [0, 0]
can build [1]
makin 1 robots of type 2
can build [0]

[0, 0, 5, 2]
[-4, -4, 3, 2]
can build [-1, 2]
can build [0, 1]
can build [2]
makin 2 robots of type 2
can build [1]
makin 1 robots of type 3

[0, 0, 7, 3]
[-8, -8, 4, 0]
can build [-2, 1]
can build [0, 1]
can build [1]
makin 1 robots of type 2
can build [0]

[0, 0, 8, 3]
[-8, -8, 11, 3]
can build [-2, 3]
can build [1, 2]
makin 1 robots of type 1
can build [3]
makin 3 robots of type 2
can build [1]
makin 1 robots of type 3

[0, 1, 11, 4]
[-12, -12, 15, 2]
can build [-2, 3]
can build [1, 2]
makin 1 robots of type 1
can build [3]
makin 3 robots of type 2
can build [1]
makin 1 robots of type 3

[0, 2, 14, 5]
[-16, -15, 22, 2]
can build [-2, 3]
can build [2, 2]
makin 2 robots of type 1
can build [3]
makin 3 robots of type 2
can build [1]
makin 1 robots of type 3

[0, 4, 17, 6]
[-20, -17, 32, 3]
can build [-2, 4]
can build [3, 3]
makin 3 robots of type 1
can build [4]
makin 4 robots of type 2
can build [2]
makin 2 robots of type 3

[0, 7, 21, 8]
[-28, -21, 41, 1]
can build [-2, 4]
can build [4, 3]
makin 3 robots of type 1
can build [4]
makin 4 robots of type 2
can build [2]
makin 2 robots of type 3

[0, 10, 25, 10]
[-36, -22, 54, 1]
can build [-2, 5]
can build [5, 3]
makin 3 robots of type 1
can build [5]
makin 5 robots of type 2
can build [2]
makin 2 robots of type 3

[0, 13, 30, 12]
[-44, -20, 71, 3]
can build [-1, 7]
can build [7, 5]
makin 5 robots of type 1
can build [7]
makin 7 robots of type 2
can build [3]
makin 3 robots of type 3

[0, 18, 37, 15]
[-56, -19, 89, 3]
can build [-1, 9]
can build [9, 6]
makin 6 robots of type 1
can build [9]
makin 9 robots of type 2
can build [4]
makin 4 robots of type 3

[0, 24, 46, 19]
[-72, -17, 110, 2]
can build [1, 10]
makin 1 robots of type 0
can build [11, 7]
makin 7 robots of type 1
can build [10]
makin 10 robots of type 2
can build [5]
makin 5 robots of type 3

[1, 31, 56, 24]
[-92, -13, 136, 1]
can build [2, 12]
makin 2 robots of type 0
can build [13, 8]
makin 8 robots of type 1
can build [12]
makin 12 robots of type 2
can build [6]
makin 6 robots of type 3

[3, 39, 68, 30]
[-115, -6, 168, 1]
can build [4, 15]
makin 4 robots of type 0
can build [16, 10]
makin 10 robots of type 1
can build [15]
makin 15 robots of type 2
can build [7]
makin 7 robots of type 3

[7, 49, 83, 37]
[-140, 5, 208, 3]
can build [7, 20]
makin 7 robots of type 0
can build [20, 13]
makin 13 robots of type 1
can build [20]
makin 20 robots of type 2
can build [10]
makin 10 robots of type 3

[14, 62, 103, 47]
[-173, 14, 251, 0]
can build [10, 23]
makin 10 robots of type 0
can build [25, 15]
makin 15 robots of type 1
can build [23]
makin 23 robots of type 2
can build [11]
makin 11 robots of type 3

[24, 77, 126, 58]
[-203, 32, 310, 3]
can build [15, 30]
makin 15 robots of type 0
can build [31, 20]
makin 20 robots of type 1
can build [30]
makin 30 robots of type 2
can build [15]
makin 15 robots of type 3

[39, 97, 156, 73]
[-239, 49, 376, 1]
can build [20, 37]
makin 20 robots of type 0
can build [38, 24]
makin 24 robots of type 1
can build [37]
makin 37 robots of type 2
can build [18]
makin 18 robots of type 3

[59, 121, 193, 91]
[-272, 74, 460, 2]
can build [27, 46]
makin 27 robots of type 0
can build [46, 31]
makin 31 robots of type 1
can build [46]
makin 46 robots of type 2
can build [23]
makin 23 robots of type 3

[86, 152, 239, 114]
[-305, 103, 561, 1]
can build [36, 57]
makin 36 robots of type 0
can build [57, 38]
makin 38 robots of type 1
can build [57]
makin 57 robots of type 2
can build [28]
makin 28 robots of type 3
HERE WE GO:
[122, 190, 296, 142]
[-331, 143, 688, 3]
122
0
bar = [10, 0, 0]

n = bar[0]

n -= 1

print(n)
print(bar)
9
[10, 0, 0]