|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +test = 'real' |
| 9 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 10 | +test_data[test] = {"input": open(input_file, "r+").read(), |
| 11 | + "expected": ['40', '241'], |
| 12 | + } |
| 13 | + |
| 14 | +# -------------------------------- Control program execution -------------------------------- # |
| 15 | + |
| 16 | +case_to_test = 'real' |
| 17 | +part_to_test = 2 |
| 18 | +verbose_level = 1 |
| 19 | + |
| 20 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 21 | + |
| 22 | +puzzle_input = test_data[case_to_test]['input'] |
| 23 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 24 | +puzzle_actual_result = 'Unknown' |
| 25 | + |
| 26 | + |
| 27 | +# -------------------------------- Actual code execution -------------------------------- # |
| 28 | + |
| 29 | +conditions = """children: 3 |
| 30 | +cats: 7 |
| 31 | +samoyeds: 2 |
| 32 | +pomeranians: 3 |
| 33 | +akitas: 0 |
| 34 | +vizslas: 0 |
| 35 | +goldfish: 5 |
| 36 | +trees: 3 |
| 37 | +cars: 2 |
| 38 | +perfumes: 1""".split('\n') |
| 39 | + |
| 40 | +if part_to_test == 1: |
| 41 | + for string in puzzle_input.split('\n'): |
| 42 | + if string == '': |
| 43 | + continue |
| 44 | + |
| 45 | + possible_Sue = True |
| 46 | + for condition in conditions: |
| 47 | + if condition + ',' in string or ', ' + condition in string: |
| 48 | + pass |
| 49 | + elif condition.split()[0] not in string: |
| 50 | + pass |
| 51 | + else: |
| 52 | + possible_Sue = False |
| 53 | + continue |
| 54 | + if possible_Sue == True: |
| 55 | + puzzle_actual_result = string.split()[1] |
| 56 | + break |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +else: |
| 61 | + for string in puzzle_input.split('\n'): |
| 62 | + if string == '': |
| 63 | + continue |
| 64 | + |
| 65 | + _, sue_id, k1, v1, k2, v2, k3, v3 = string.split() |
| 66 | + sue_data = dict(zip((k1, k2, k3), map(int, (v1[:-1], v2[:-1], v3)))) |
| 67 | + |
| 68 | + possible_Sue = True |
| 69 | + for condition in conditions: |
| 70 | + key, val = condition.split() |
| 71 | + val = int(val) |
| 72 | + |
| 73 | + if key not in sue_data: |
| 74 | + continue |
| 75 | + elif key in ('cats:', 'trees:'): |
| 76 | + if sue_data[key] <= val: |
| 77 | + possible_Sue = False |
| 78 | + break |
| 79 | + elif key in ('pomeranians:', 'goldfish:'): |
| 80 | + if sue_data[key] >= val: |
| 81 | + possible_Sue = False |
| 82 | + break |
| 83 | + else: |
| 84 | + if sue_data[key] != val: |
| 85 | + possible_Sue = False |
| 86 | + break |
| 87 | + |
| 88 | + #print (sue_data, possible_Sue) |
| 89 | + |
| 90 | + if possible_Sue == True: |
| 91 | + puzzle_actual_result = sue_id |
| 92 | + break |
| 93 | + |
| 94 | + |
| 95 | +# -------------------------------- Outputs / results -------------------------------- # |
| 96 | + |
| 97 | +if verbose_level >= 3: |
| 98 | + print ('Input : ' + puzzle_input) |
| 99 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 100 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments