aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

solve.py (2627B)


      1import sys
      2sys.path.append("../common")
      3import aoc
      4
      5data = aoc.data.split("\n")
      6
      7def remove_reqs(req, insts):
      8    for res in insts:
      9        if req in insts[res]:
     10            insts[res].remove(req)
     11
     12def parse_entry(l):
     13    split = l.split(" ")
     14    firstl = split[1]
     15    nextl = split[-3]
     16    return firstl, nextl
     17
     18def gen_insts():
     19    global data
     20    data = [parse_entry(l) for l in data if len(l) != 0]
     21
     22    insts = dict()
     23
     24    for ins in data:
     25        req = ins[0]
     26        res = ins[1]
     27        if res not in insts:
     28            insts[res] = [ req ]
     29        else:
     30            insts[res].append(req)
     31
     32    values = list(insts.values())[:]
     33    for reslist in values:
     34        for res in reslist:
     35            if res not in insts:
     36                insts[res] = []
     37
     38    return insts
     39
     40def overlap(arr1, arr2):
     41    for a1 in arr1:
     42        if a1 in arr2:
     43            return True
     44    return False
     45
     46def checkfail(workers):
     47    done = True
     48    for w in workers:
     49        if w[2]:
     50            done = False
     51            break
     52    return done
     53
     54def solve1(args):
     55    insts = gen_insts()
     56
     57    i = 0
     58    plan = list()
     59    while i < len(insts):
     60        res = sorted(insts.keys())[i] # alphabetically
     61        if len(insts[res]) == 0:
     62            plan.append(res)
     63            insts.pop(res, None)
     64            remove_reqs(res, insts)
     65            i = 0
     66        else:
     67            i += 1
     68
     69    return "".join(plan)
     70
     71def solve2(args):
     72    insts = gen_insts()
     73
     74    # worker: (task, time, done)
     75    workers = [["", 0, False] for x in range(5)]
     76
     77    time = -1
     78    stop = False
     79    while not stop:
     80        time += 1
     81        for i in range(len(workers)):
     82            w = workers[i]
     83            if time >= w[1]:
     84                if w[2]:
     85                    remove_reqs(w[0], insts)
     86                    aoc.debug("end : " + str(i) + " - " + str((w[0], w[1])))
     87                    w[2] = False
     88                    if len(insts) == 0 and checkfail(workers):
     89                        stop = True
     90                        break
     91
     92        for i in range(len(workers)):
     93            w = workers[i]
     94            if time >= w[1]:
     95                for res in sorted(insts.keys()):
     96                    if len(insts[res]) == 0:
     97                        wtime = time + ord(res)-65+1 + 60
     98                        aoc.debug("start : " + str(i) + " - " + str((res, time)))
     99                        w[:] = (res, wtime, True)
    100                        insts.pop(res, None)
    101                        break
    102
    103    return time
    104
    105aoc.run(solve1, solve2, sols=["JNOIKSYABEQRUVWXGTZFDMHLPC", 1099])