Upload dec2 code.
parent
b42c2a8852
commit
cb21b0e323
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,136 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
# # Puzzle 2 of AoC 2025
|
||||||
|
# Author: Victoria Ramírez López
|
||||||
|
#
|
||||||
|
# Date: Dec 2, 2025
|
||||||
|
|
||||||
|
# In[49]:
|
||||||
|
|
||||||
|
|
||||||
|
# Read product ID ranges file
|
||||||
|
|
||||||
|
import csv
|
||||||
|
input_file = open('product_id_ranges.txt', mode ='r') # Open in 'read only' mode
|
||||||
|
id_ranges_csv = csv.reader(input_file)
|
||||||
|
id_ranges_list = list(id_ranges_csv)
|
||||||
|
|
||||||
|
|
||||||
|
# In[52]:
|
||||||
|
|
||||||
|
|
||||||
|
# Get invalid ids and calculate answer
|
||||||
|
|
||||||
|
invalid_ids = []
|
||||||
|
id_ranges = []
|
||||||
|
|
||||||
|
for id_range in id_ranges_list[0]:
|
||||||
|
# invalid_ids_range = find_invalid_ids(id_range) # For part 1 answer
|
||||||
|
invalid_ids_range = find_all_invalid_ids(id_range) # For part 2 answer
|
||||||
|
invalid_ids.append(invalid_ids_range[:])
|
||||||
|
|
||||||
|
sum_invalid_ids = sum(sum(invalid_ids,[]))
|
||||||
|
# print(invalid_ids)
|
||||||
|
print(sum_invalid_ids)
|
||||||
|
|
||||||
|
|
||||||
|
# In[18]:
|
||||||
|
|
||||||
|
|
||||||
|
def find_invalid_ids(id_range=''):
|
||||||
|
# This function finds the invalid ids within the specified range (part 1)
|
||||||
|
# input: id_range = String
|
||||||
|
# output: invalid_ids = int []
|
||||||
|
|
||||||
|
invalid_ids = []
|
||||||
|
invalid_id = []
|
||||||
|
|
||||||
|
# Extract the upper and lower bounds of the range
|
||||||
|
lb = int(id_range.split('-')[0])
|
||||||
|
ub = int(id_range.split('-')[1]) + 1
|
||||||
|
|
||||||
|
# Generate the series of product ids withing the range
|
||||||
|
product_ids = range(lb,ub)
|
||||||
|
|
||||||
|
# Find invalid ids
|
||||||
|
for id in product_ids:
|
||||||
|
id_string = str(id) # Convert each product id to string
|
||||||
|
|
||||||
|
if len(id_string) % 2 == 0:
|
||||||
|
middle_point = len(id_string) // 2 # Find where to split the product id in 2
|
||||||
|
|
||||||
|
# Compare the two halves of the id to determine if the id is invalid
|
||||||
|
first_half = id_string[0:middle_point]
|
||||||
|
second_half = id_string[middle_point:]
|
||||||
|
|
||||||
|
if first_half == second_half: # If the two halves are equal, the id is invalid
|
||||||
|
invalid_id = int(id_string)
|
||||||
|
invalid_ids.append(invalid_id)
|
||||||
|
|
||||||
|
return invalid_ids
|
||||||
|
|
||||||
|
|
||||||
|
# In[51]:
|
||||||
|
|
||||||
|
|
||||||
|
def find_all_invalid_ids(id_range=''):
|
||||||
|
# This function finds all the invalid ids within the specified range (part 2)
|
||||||
|
# input: id_range = String
|
||||||
|
# output: invalid_ids = int [[]]
|
||||||
|
|
||||||
|
invalid_ids = []
|
||||||
|
invalid_id = []
|
||||||
|
|
||||||
|
# Extract the upper and lower bounds of the range
|
||||||
|
lb = int(id_range.split('-')[0])
|
||||||
|
ub = int(id_range.split('-')[1]) + 1
|
||||||
|
|
||||||
|
# Generate the series of product ids withing the range
|
||||||
|
product_ids = range(lb,ub)
|
||||||
|
|
||||||
|
# Find invalid ids
|
||||||
|
for id in product_ids:
|
||||||
|
id_string = str(id) # Convert each product id to string
|
||||||
|
|
||||||
|
chopped_ids = chop_product_id(id) # Chop each product id into equal-sized pieces
|
||||||
|
|
||||||
|
# Identify ids with a repeating pattern
|
||||||
|
for list in chopped_ids:
|
||||||
|
chunks_set = set(list) # Convert each group of equal-sized chunks into a set
|
||||||
|
|
||||||
|
if len(chunks_set) == 1: # If the resulting set has only 1 element, the id is invalid
|
||||||
|
invalid_id = int(id_string)
|
||||||
|
invalid_ids.append(invalid_id)
|
||||||
|
break
|
||||||
|
|
||||||
|
return(invalid_ids)
|
||||||
|
|
||||||
|
|
||||||
|
# In[33]:
|
||||||
|
|
||||||
|
|
||||||
|
def chop_product_id(id):
|
||||||
|
# This function chops a product id into chunks of equal sizes
|
||||||
|
# input: id = int
|
||||||
|
# output: chopped_id = [[]]
|
||||||
|
|
||||||
|
id_string = str(id)
|
||||||
|
chunks = []
|
||||||
|
chopped_id = []
|
||||||
|
|
||||||
|
# Find divisors to split the id into equal-sized chunks
|
||||||
|
# (aka: how many ways to split the id into equal-sized chunks are there?)
|
||||||
|
for divisor in range(2,len(id_string) + 1):
|
||||||
|
if len(id_string) % divisor == 0:
|
||||||
|
chunks = []
|
||||||
|
chunk_size = len(id_string) // divisor # Determine how big a chunk would be
|
||||||
|
|
||||||
|
for i in range(1,len(id_string)+1,chunk_size): # Chop the id
|
||||||
|
chunk = id_string[i-1:(i-1 + chunk_size)]
|
||||||
|
chunks.append(chunk)
|
||||||
|
|
||||||
|
chopped_id.append(chunks)
|
||||||
|
|
||||||
|
return chopped_id
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
959516-995437,389276443-389465477,683-1336,15687-26722,91613-136893,4-18,6736-12582,92850684-93066214,65-101,6868676926-6868700146,535033-570760,826141-957696,365650-534331,1502-2812,309789-352254,79110404-79172400,18286593-18485520,34376-65398,26-63,3333208697-3333457635,202007-307147,1859689-1936942,9959142-10053234,2318919-2420944,5142771457-5142940464,1036065-1206184,46314118-46413048,3367-6093,237-481,591751-793578
|
||||||
Loading…
Reference in New Issue