Finished solution for day 2!

This commit is contained in:
Ada Werefox 2025-12-02 12:53:17 -08:00
parent e014c806bd
commit 908f1dafd3
6 changed files with 159 additions and 4 deletions

View file

@ -91,6 +91,3 @@ Be careful: if the dial were pointing at 50, a single rotation like R1000 would
Using password method 0x434C49434B, what is the password to open the door?
## 1059
## 6305

View file

@ -23,7 +23,7 @@ def parse_input(input_filepath: str) -> list[int]:
return rotations_list
def simulate_rotations(rotations_list: list[int]) -> list[int]:
def simulate_rotations(rotations_list: list[int]) -> int:
ending_positions = []
current_position = 50
zero_count = 0

61
02/README.md Normal file
View file

@ -0,0 +1,61 @@
# --- Day 2: Gift Shop ---
You get inside and take the elevator to its only other stop: the gift shop. "Thank you for visiting the North Pole!" gleefully exclaims a nearby sign. You aren't sure who is even allowed to visit the North Pole, but you know you can access the lobby through here, and from there you can access the rest of the North Pole base.
As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for your help.
As it turns out, one of the younger Elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database! Surely, it would be no trouble for you to identify the invalid product IDs for them, right?
They've even checked most of the product ID ranges already; they only have a few product ID ranges (your puzzle input) that you'll need to check. For example:
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
824824821-824824827,2121212118-2121212124
(The ID ranges are wrapped here for legibility; in your input, they appear on a single long line.)
The ranges are separated by commas (,); each range gives its first ID and last ID separated by a dash (-).
Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.
None of the numbers have leading zeroes; 0101 isn't an ID at all. (101 is a valid ID that you would ignore.)
Your job is to find all of the invalid IDs that appear in the given ranges. In the above example:
- 11-22 has two invalid IDs, 11 and 22.
- 95-115 has one invalid ID, 99.
- 998-1012 has one invalid ID, 1010.
- 1188511880-1188511890 has one invalid ID, 1188511885.
- 222220-222224 has one invalid ID, 222222.
- 1698522-1698528 contains no invalid IDs.
- 446443-446449 has one invalid ID, 446446.
- 38593856-38593862 has one invalid ID, 38593859.
- The rest of the ranges contain no invalid IDs.
Adding up all the invalid IDs in this example produces `1227775554`.
What do you get if you add up all of the invalid IDs?
## --- Part Two ---
The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young Elf was doing other silly patterns as well?
Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs.
From the same example as before:
11-22 still has two invalid IDs, 11 and 22.
95-115 now has two invalid IDs, 99 and 111.
998-1012 now has two invalid IDs, 999 and 1010.
1188511880-1188511890 still has one invalid ID, 1188511885.
222220-222224 still has one invalid ID, 222222.
1698522-1698528 still contains no invalid IDs.
446443-446449 still has one invalid ID, 446446.
38593856-38593862 still has one invalid ID, 38593859.
565653-565659 now has one invalid ID, 565656.
824824821-824824827 now has one invalid ID, 824824824.
2121212118-2121212124 now has one invalid ID, 2121212121.
Adding up all the invalid IDs in this example produces 4174379265.
What do you get if you add up all of the invalid IDs using these new rules?

1
02/input/ranges.txt Normal file
View file

@ -0,0 +1 @@
269194394-269335492,62371645-62509655,958929250-958994165,1336-3155,723925-849457,4416182-4470506,1775759815-1775887457,44422705-44477011,7612653647-7612728309,235784-396818,751-1236,20-36,4-14,9971242-10046246,8796089-8943190,34266-99164,2931385381-2931511480,277-640,894249-1083306,648255-713763,19167863-19202443,62-92,534463-598755,93-196,2276873-2559254,123712-212673,31261442-31408224,421375-503954,8383763979-8383947043,17194-32288,941928989-941964298,3416-9716

1
02/input/test_ranges.txt Normal file
View file

@ -0,0 +1 @@
10-50

95
02/main.py Executable file
View file

@ -0,0 +1,95 @@
#!/bin/python
from logging import debug, DEBUG, basicConfig
from sys import argv
def parse_input(input_filepath: str) -> list[tuple[int, int]]:
ranges_list: list[tuple[int, int]] = []
with open(file=input_filepath, mode="r") as input_file:
input_data: list[str] = input_file.readlines()
debug(f"\n\nRAW INPUT: {input_data}\n\n")
# Should only be one line in the input.
# In theory, this could be condensed to one line, but how about we don't.
for line in input_data:
split_line = line.split(",")
for range in split_line:
split_range = range.split("-")
range_tuple: tuple[int, int] = (int(split_range[0]), int(split_range[1]))
ranges_list.append(range_tuple)
return ranges_list
def find_divisors_and_comparisons(
product_id: int, repeats: int
) -> list[tuple[int, int]]:
product_id_length = len(str(product_id))
divisors = []
if repeats != 0:
if product_id_length % repeats == 0:
divisor = int(product_id_length / repeats)
divisors.append((divisor, product_id % pow(10, divisor)))
return divisors
for i in range(1, int(product_id_length / 2) + 1):
if product_id_length % i == 0:
divisors.append((i, product_id % pow(10, i)))
return divisors
def select_bad_product_ids(
input_ranges: list[tuple[int, int]], repeats: int
) -> set[int]:
bad_product_ids = []
for input_range in input_ranges:
for product_id in range(input_range[0], input_range[1]):
product_id_length = len(str(product_id))
if repeats != 0:
if product_id_length % repeats != 0:
continue
divisors_and_comparisons = find_divisors_and_comparisons(
product_id, repeats
)
debug(f"PRODUCT ID: {product_id}")
debug(f"DIVISORS AND COMPARISONS: {divisors_and_comparisons}")
for divisor_and_comparison in divisors_and_comparisons:
current_product_id = product_id
debug(f"CURRENT DIVISOR: {divisor_and_comparison[0]}")
for i in range(int(product_id_length / divisor_and_comparison[0])):
debug(f"CURRENT PRODUCT ID ITERATION: {current_product_id}")
debug(f"CURRENT COMPARISON: {divisor_and_comparison[1]}")
iteration_modulus = current_product_id % pow(
10, divisor_and_comparison[0]
)
debug(f"ITERATION MODULUS: {iteration_modulus}")
if iteration_modulus != divisor_and_comparison[1]:
break
current_product_id = int(
current_product_id / pow(10, divisor_and_comparison[0])
)
if current_product_id == 0:
bad_product_ids.append(product_id)
debug(f"BAD PRODUCT IDS: {bad_product_ids}\n")
return set(bad_product_ids)
def main(repeats: int) -> None:
input_filepath = "input/ranges.txt"
input_ranges = parse_input(input_filepath)
bad_product_ids = select_bad_product_ids(input_ranges, repeats)
print(bad_product_ids)
print(sum(bad_product_ids))
return
if __name__ == "__main__":
if "-d" in argv or "--debug" in argv:
basicConfig(filename="debug.log", level=DEBUG)
if "-t" in argv or "--twice" in argv:
main(2)
else:
main(0)
exit(0)