|
| 1 | +""" |
| 2 | +Each of your joltage adapters is rated for a specific output joltage (your puzzle input). Any given adapter |
| 3 | +can take an input 1, 2, or 3 jolts lower than its rating and still produce its rated output joltage. |
| 4 | +
|
| 5 | +In addition, your device has a built-in joltage adapter rated for 3 jolts higher than the highest-rated adapter |
| 6 | +in your bag. (If your adapter list were 3, 9, and 6, your device's built-in adapter would be rated for 12 jolts.) |
| 7 | +
|
| 8 | +Treat the charging outlet near your seat as having an effective joltage rating of 0. |
| 9 | +
|
| 10 | +Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort |
| 11 | +and realize you can't even charge your device! |
| 12 | +
|
| 13 | +If you use every adapter in your bag at once, what is the distribution of joltage differences between the charging |
| 14 | +outlet, the adapters, and your device? |
| 15 | +
|
| 16 | +1. |
| 17 | +Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count |
| 18 | +the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt |
| 19 | +differences multiplied by the number of 3-jolt differences? |
| 20 | +
|
| 21 | +2. |
| 22 | +What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device? |
| 23 | +
|
| 24 | +""" |
| 25 | +import numpy as np |
| 26 | + |
| 27 | + |
| 28 | +filepath = "data/input_10.txt" |
| 29 | + |
| 30 | + |
| 31 | +def part1(): |
| 32 | + with open(filepath) as fp: |
| 33 | + lines = [line.strip("\n") for line in fp] |
| 34 | + joltages = np.array([int(item) for item in lines]) |
| 35 | + joltages.sort() |
| 36 | + joltages = np.concatenate(([0], joltages, [joltages.max() + 3])) |
| 37 | + diffs = joltages[1:] - joltages[:(len(joltages)-1)] |
| 38 | + unique_elements, counts_elements = np.unique(diffs, return_counts=True) |
| 39 | + print(unique_elements) |
| 40 | + answer = counts_elements[0] * counts_elements[1] |
| 41 | + print("Answer %d." % answer) |
| 42 | + |
| 43 | + |
| 44 | +def get_number_of_paths(ones_group): |
| 45 | + """ For a set of ones [1]_k (k ones) the number of paths f([1]_k) = |
| 46 | + f([1]_(k-1)) + f([1]_(k-2)) + f([1]_(k-3)). |
| 47 | + """ |
| 48 | + assert set(ones_group) == {1} or len(ones_group) == 0 |
| 49 | + if len(ones_group) == 0: |
| 50 | + return 1 |
| 51 | + elif len(ones_group) == 1: |
| 52 | + return 1 |
| 53 | + elif len(ones_group) == 2: |
| 54 | + return get_number_of_paths(ones_group[1:]) + get_number_of_paths(ones_group[2:]) |
| 55 | + else: # len >= 3 |
| 56 | + return get_number_of_paths(ones_group[1:]) + get_number_of_paths(ones_group[2:]) + \ |
| 57 | + get_number_of_paths(ones_group[3:]) |
| 58 | + |
| 59 | + |
| 60 | +def part2(): |
| 61 | + with open(filepath) as fp: |
| 62 | + lines = [line.strip("\n") for line in fp] |
| 63 | + joltages = np.array([int(item) for item in lines]) |
| 64 | + joltages.sort() |
| 65 | + joltages = np.concatenate(([0], joltages, [joltages.max() + 3])) |
| 66 | + diffs = joltages[1:] - joltages[:(len(joltages)-1)] |
| 67 | + # in diffs there are only 1s and 3s |
| 68 | + # paths can be formed by combining different 1s. Each subset of 1s between 3s is an independent set of paths |
| 69 | + # total number of paths found by multiplying number of paths for each subset of ones: n1 * n2 * n3 ... nk |
| 70 | + diffs_string = "".join([str(i) for i in diffs]) |
| 71 | + ones_groups = diffs_string.split("3") |
| 72 | + ones_groups = [list(item) for item in ones_groups] |
| 73 | + ones_groups = [[int(i) for i in item] for item in ones_groups] |
| 74 | + numbers_of_paths = [get_number_of_paths(og) for og in ones_groups] |
| 75 | + total_number_paths = np.prod(np.array(numbers_of_paths)) |
| 76 | + print("Answer %d" % total_number_paths) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + part1() |
| 81 | + print("*****") |
| 82 | + part2() |
0 commit comments