|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, math |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": 5, |
| 8 | + "expected": [3, 'Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test += 1 |
| 12 | +test_data[test] = {"input": 15, |
| 13 | + "expected": ['Unknown', 'Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test = 'real' |
| 17 | +test_data[test] = {"input": 3012210, |
| 18 | + "expected": ['1830117', '1417887'], |
| 19 | + } |
| 20 | + |
| 21 | +# -------------------------------- Control program execution -------------------------------- # |
| 22 | + |
| 23 | +case_to_test = 'real' |
| 24 | +part_to_test = 2 |
| 25 | +verbose_level = 1 |
| 26 | + |
| 27 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 28 | + |
| 29 | +puzzle_input = test_data[case_to_test]['input'] |
| 30 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 31 | +puzzle_actual_result = 'Unknown' |
| 32 | + |
| 33 | + |
| 34 | +# -------------------------------- Actual code execution -------------------------------- # |
| 35 | + |
| 36 | +if part_to_test == 1: |
| 37 | + elves = list(range(1, puzzle_input + 1)) |
| 38 | + |
| 39 | + while len(elves) > 1: |
| 40 | + if len(elves) %2 == 1: |
| 41 | + elves = elves[::2][1:] |
| 42 | + else: |
| 43 | + elves = elves[::2] |
| 44 | + |
| 45 | + puzzle_actual_result = elves[0] |
| 46 | + |
| 47 | +else: |
| 48 | + # For some reason, the value for any power of 3 is equal to itself |
| 49 | + # If X is a power of 3: |
| 50 | + # Numbers N from X+1 to 2*X have N-X as a result (it increases by 1) |
| 51 | + # Numbers N from 2*X to 3*X-1 have 2*N-3*X as a result (it increases by 2) |
| 52 | + |
| 53 | + # Find the power of 3 right below the puzzle input |
| 54 | + power_of_3 = 3**math.trunc(math.log(puzzle_input, 3)) |
| 55 | + |
| 56 | + if puzzle_input <= 2*power_of_3: |
| 57 | + puzzle_actual_result = puzzle_input - power_of_3 |
| 58 | + else: |
| 59 | + puzzle_actual_result = puzzle_input*2 - power_of_3*3 |
| 60 | + |
| 61 | + |
| 62 | +# -------------------------------- Outputs / results -------------------------------- # |
| 63 | + |
| 64 | +if verbose_level >= 3: |
| 65 | + print ('Input : ' + puzzle_input) |
| 66 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 67 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
0 commit comments