import math # I had a hard time with part 1, but part 2 was quick. I first thought of # integer programming (it looked like a set of constraints) but quickly # realized it was the wrong path. I then got stuck for a while trying # to decide how to handle the situation where resources would be wasted # no matter what. Turns out that with the way the constraints for the # problem are laid out, there aren't any cycles it seems. If there were # we would have had trouble with the "still possible to produce" aspect # of things. But, it worked out. The binary search for part 2 on the # infinity of all numbers was interesting. I tried a couple deltas and # a million seemed to work fine. fn = "./in.txt" raw_eqns = util.filetolist(fn) eqs = {} for eq in raw_eqns: ins, outs = eq.split(" => ") lins = ins.split(", ") lin_pairs = [] for inv in lins: numin, s = inv.split(" ") lin_pairs.append((int(numin), s)) numout, s = outs.split(" ") eqs[s] = (int(numout), lin_pairs) def get_possible(chem): possible = set()
def ticket_invalid_sum(ticket, fields): total = 0 for num in ticket: passed = False for rpair in fields.values(): if in_range(num, rpair[0]) or in_range(num, rpair[1]): passed = True break if not passed: total += num return total fn = f"{os.path.dirname(__file__)}/in.txt" l = util.filetolist(fn) p = compile("{}: {:d}-{:d} or {:d}-{:d}") tickets = [] fields = {} for line in l: r = p.parse(line) if r: fields[r[0]] = ((r[1], r[2]), (r[3], r[4])) else: toks = line.split(",") if len(toks) > 1: tickets.append(list(map(lambda x: int(x), toks))) myticket = tickets[0]
dest_label -= 1 if dest_label < 1: dest_label = num_cups # Put 3 removed cups back in end = cups[dest_label] next_cup = dest_label for label in removed_cups: cups[next_cup] = label next_cup = label cups[next_cup] = end # Set new current cup value current_cup = cups[current_cup] return cups fn = f"{os.path.dirname(__file__)}/in.txt" input_string = util.filetolist(fn)[0] cups = play_cups(input_string, 9, 100) ans = "" current_cup = cups[1] while current_cup != 1: ans += str(current_cup) current_cup = cups[current_cup] print("Part 1 Solution:") print(ans) cups = play_cups(input_string, 1_000_000, 10_000_000) print("Part 2 Solution:") print(cups[1] * cups[cups[1]])