|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | + |
| 6 | +test_data = {} |
| 7 | + |
| 8 | +test = 1 |
| 9 | +test_data[test] = { |
| 10 | + "input": ( |
| 11 | + """deal into new stack |
| 12 | +cut -2 |
| 13 | +deal with increment 7 |
| 14 | +cut 8 |
| 15 | +cut -4 |
| 16 | +deal with increment 7 |
| 17 | +cut 3 |
| 18 | +deal with increment 9 |
| 19 | +deal with increment 3 |
| 20 | +cut -1""", |
| 21 | + 10, |
| 22 | + ), |
| 23 | + "expected": ["9 2 5 8 1 4 7 0 3 6", "9 2 5 8 1 4 7 0 3 6"], |
| 24 | +} |
| 25 | + |
| 26 | +test += 1 |
| 27 | +test_data[test] = { |
| 28 | + "input": ( |
| 29 | + """cut 6 |
| 30 | +deal with increment 7 |
| 31 | +deal into new stack""", |
| 32 | + 10, |
| 33 | + ), |
| 34 | + "expected": ["3 0 7 4 1 8 5 2 9 6", "3 0 7 4 1 8 5 2 9 6"], |
| 35 | +} |
| 36 | + |
| 37 | +test += 1 |
| 38 | +test_data[test] = { |
| 39 | + "input": ( |
| 40 | + """deal with increment 7 |
| 41 | +cut 3 |
| 42 | +deal into new stack""", |
| 43 | + 10, |
| 44 | + ), |
| 45 | + "expected": ["Unknown", "Unknown"], |
| 46 | +} |
| 47 | + |
| 48 | +test = "real" |
| 49 | +input_file = os.path.join( |
| 50 | + os.path.dirname(__file__), |
| 51 | + "Inputs", |
| 52 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 53 | +) |
| 54 | +test_data[test] = { |
| 55 | + "input": (open(input_file, "r+").read(), 119315717514047), |
| 56 | + "expected": ["2480", "62416301438548"], |
| 57 | +} |
| 58 | + |
| 59 | +# -------------------------------- Control program execution ------------------------- # |
| 60 | + |
| 61 | +case_to_test = "real" |
| 62 | +part_to_test = 2 |
| 63 | + |
| 64 | +# -------------------------------- Initialize some variables ------------------------- # |
| 65 | + |
| 66 | +puzzle_input = test_data[case_to_test]["input"] |
| 67 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 68 | +puzzle_actual_result = "Unknown" |
| 69 | + |
| 70 | + |
| 71 | +# -------------------------------- Actual code execution ----------------------------- # |
| 72 | + |
| 73 | +nb_cards = puzzle_input[1] |
| 74 | + |
| 75 | +if part_to_test == 1: |
| 76 | + deck = [x for x in range(nb_cards)] |
| 77 | + |
| 78 | + for string in puzzle_input[0].split("\n"): |
| 79 | + if string == "": |
| 80 | + continue |
| 81 | + if string == "deal into new stack": |
| 82 | + deck = deck[::-1] |
| 83 | + elif string[0:4] == "deal": |
| 84 | + number = int(string.split(" ")[-1]) |
| 85 | + new_deck = [0] * nb_cards |
| 86 | + for i in range(0, nb_cards * number, number): |
| 87 | + new_deck[i % nb_cards] = deck[i // number] |
| 88 | + deck = new_deck[:] |
| 89 | + else: |
| 90 | + number = int(string.split(" ")[-1]) |
| 91 | + deck = deck[number:] + deck[:number] |
| 92 | + |
| 93 | + # print (string, deck) |
| 94 | + |
| 95 | + print(deck) |
| 96 | + puzzle_actual_result = deck.index(2019) |
| 97 | + |
| 98 | + |
| 99 | +else: |
| 100 | + nb_shuffles = 101741582076661 |
| 101 | + # Then the goal is to find a, b and x so that after 1 deal means: |
| 102 | + # a*initial_position + b = [output] % nb_cards |
| 103 | + # a and b can be found by analyzing the movements done |
| 104 | + a, b = 1, 0 |
| 105 | + for string in puzzle_input[0].split("\n")[::-1]: |
| 106 | + if string == "": |
| 107 | + continue |
| 108 | + if string == "deal into new stack": |
| 109 | + a *= -1 |
| 110 | + b *= -1 |
| 111 | + b -= 1 # Not sure why it's needed... |
| 112 | + elif string[0:4] == "deal": |
| 113 | + number = int(string.split(" ")[-1]) |
| 114 | + a *= pow(number, -1, nb_cards) |
| 115 | + b *= pow(number, -1, nb_cards) |
| 116 | + else: |
| 117 | + number = int(string.split(" ")[-1]) |
| 118 | + b += number |
| 119 | + |
| 120 | + a, b = a % nb_cards, b % nb_cards |
| 121 | + |
| 122 | + # This function applies the shuffles nb_shuffles times |
| 123 | + # This is the equation a^nb_shuffles * position + sum[a^k * b for k in range(0, nb_shuffles-1)] % nb_cards |
| 124 | + # This translated to a^nb_shuffles * position + b * (1-a^nb_shuffles) / (1-a) % nb_cards |
| 125 | + |
| 126 | + def shuffles(a, b, position, nb_shuffles, nb_cards): |
| 127 | + value = pow(a, nb_shuffles, nb_cards) * position |
| 128 | + value += b * (1 - pow(a, nb_shuffles, nb_cards)) * pow(1 - a, -1, nb_cards) |
| 129 | + value %= nb_cards |
| 130 | + return value |
| 131 | + |
| 132 | + puzzle_actual_result = shuffles(a, b, 2020, nb_shuffles, nb_cards) |
| 133 | + |
| 134 | + |
| 135 | +# -------------------------------- Outputs / results --------------------------------- # |
| 136 | + |
| 137 | +print("Expected result : " + str(puzzle_expected_result)) |
| 138 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments