|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools |
| 3 | +from collections import Counter, deque, defaultdict |
| 4 | + |
| 5 | +from compass import * |
| 6 | + |
| 7 | +# from simply_linked_list import * |
| 8 | + |
| 9 | + |
| 10 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 11 | +def lmap(func, *iterables): |
| 12 | + return list(map(func, *iterables)) |
| 13 | + |
| 14 | + |
| 15 | +def ints(s: str): |
| 16 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 17 | + |
| 18 | + |
| 19 | +def positive_ints(s: str): |
| 20 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 21 | + |
| 22 | + |
| 23 | +def floats(s: str): |
| 24 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 25 | + |
| 26 | + |
| 27 | +def positive_floats(s: str): |
| 28 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 29 | + |
| 30 | + |
| 31 | +def words(s: str): |
| 32 | + return re.findall(r"[a-zA-Z]+", s) |
| 33 | + |
| 34 | + |
| 35 | +test_data = {} |
| 36 | + |
| 37 | +test = 1 |
| 38 | +test_data[test] = { |
| 39 | + "input": """389125467""", |
| 40 | + "expected": ["92658374 after 10 moves, 67384529 after 100 moves", "149245887792"], |
| 41 | +} |
| 42 | + |
| 43 | +test = "real" |
| 44 | +input_file = os.path.join( |
| 45 | + os.path.dirname(__file__), |
| 46 | + "Inputs", |
| 47 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 48 | +) |
| 49 | +test_data[test] = { |
| 50 | + "input": open(input_file, "r+").read(), |
| 51 | + "expected": ["45286397", "836763710"], |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +# -------------------------------- Control program execution ------------------------- # |
| 56 | + |
| 57 | +case_to_test = 1 |
| 58 | +part_to_test = 2 |
| 59 | + |
| 60 | +# -------------------------------- Initialize some variables ------------------------- # |
| 61 | + |
| 62 | +puzzle_input = test_data[case_to_test]["input"] |
| 63 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 64 | +puzzle_actual_result = "Unknown" |
| 65 | + |
| 66 | + |
| 67 | +# -------------------------------- Actual code execution ----------------------------- # |
| 68 | + |
| 69 | + |
| 70 | +if part_to_test == 1: |
| 71 | + moves = 100 |
| 72 | + for string in puzzle_input.split("\n"): |
| 73 | + cups = [int(x) for x in string] |
| 74 | + |
| 75 | + for i in range(moves): |
| 76 | + cur_cup = cups[0] |
| 77 | + pickup = cups[1:4] |
| 78 | + del cups[0:4] |
| 79 | + |
| 80 | + try: |
| 81 | + dest_cup = max([x for x in cups if x < cur_cup]) |
| 82 | + except: |
| 83 | + dest_cup = max([x for x in cups]) |
| 84 | + cups[cups.index(dest_cup) + 1 : cups.index(dest_cup) + 1] = pickup |
| 85 | + cups.append(cur_cup) |
| 86 | + |
| 87 | + print(cups) |
| 88 | + |
| 89 | + pos1 = cups.index(1) |
| 90 | + puzzle_actual_result = "".join(map(str, cups[pos1 + 1 :] + cups[:pos1])) |
| 91 | + |
| 92 | +else: |
| 93 | + moves = 10 ** 7 |
| 94 | + nb_cups = 10 ** 6 |
| 95 | + |
| 96 | + class Cup: |
| 97 | + def __init__(self, val, next_cup=None): |
| 98 | + self.val = val |
| 99 | + self.next_cup = next_cup |
| 100 | + |
| 101 | + string = puzzle_input.split("\n")[0] |
| 102 | + next_cup = None |
| 103 | + cups = {} |
| 104 | + for x in string[::-1]: |
| 105 | + cups[x] = Cup(x, next_cup) |
| 106 | + next_cup = cups[x] |
| 107 | + |
| 108 | + next_cup = cups[string[0]] |
| 109 | + for x in range(nb_cups, 9, -1): |
| 110 | + cups[str(x)] = Cup(str(x), next_cup) |
| 111 | + next_cup = cups[str(x)] |
| 112 | + |
| 113 | + cups[string[-1]].next_cup = cups["10"] |
| 114 | + |
| 115 | + cur_cup = cups[string[0]] |
| 116 | + for i in range(1, moves + 1): |
| 117 | + # #print ('----- Move', i) |
| 118 | + # #print ('Current', cur_cup.val) |
| 119 | + |
| 120 | + cups_moved = [ |
| 121 | + cur_cup.next_cup, |
| 122 | + cur_cup.next_cup.next_cup, |
| 123 | + cur_cup.next_cup.next_cup.next_cup, |
| 124 | + ] |
| 125 | + cups_moved_val = [cup.val for cup in cups_moved] |
| 126 | + # #print ('Moved cups', cups_moved_val) |
| 127 | + |
| 128 | + cur_cup.next_cup = cups_moved[-1].next_cup |
| 129 | + |
| 130 | + dest_cup_nr = int(cur_cup.val) - 1 |
| 131 | + while str(dest_cup_nr) in cups_moved_val or dest_cup_nr <= 0: |
| 132 | + dest_cup_nr -= 1 |
| 133 | + if dest_cup_nr <= 0: |
| 134 | + dest_cup_nr = nb_cups |
| 135 | + dest_cup = cups[str(dest_cup_nr)] |
| 136 | + |
| 137 | + # #print ("Destination", dest_cup_nr) |
| 138 | + |
| 139 | + cups_moved[-1].next_cup = dest_cup.next_cup |
| 140 | + dest_cup.next_cup = cups_moved[0] |
| 141 | + |
| 142 | + cur_cup = cur_cup.next_cup |
| 143 | + |
| 144 | + puzzle_actual_result = int(cups["1"].next_cup.val) * int( |
| 145 | + cups["1"].next_cup.next_cup.val |
| 146 | + ) |
| 147 | + # #puzzle_actual_result = cups[(pos1+1)%len(cups)] * cups[(pos1+2)%len(cups)] |
| 148 | + |
| 149 | +# -------------------------------- Outputs / results --------------------------------- # |
| 150 | + |
| 151 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 152 | +print("Expected result : " + str(puzzle_expected_result)) |
| 153 | +print("Actual result : " + str(puzzle_actual_result)) |
| 154 | +# Date created: 2020-12-23 06:25:17.546310 |
| 155 | +# Part 1: 2020-12-23 06:36:18 |
| 156 | +# Part 2: 2020-12-23 15:21:48 |
0 commit comments