Some cleanup, added a few comments all around
This commit is contained in:
parent
ad04dffb69
commit
f44d5b9e66
6 changed files with 51 additions and 17 deletions
|
@ -1,7 +1,14 @@
|
|||
target = 2020
|
||||
input_file = "/tmp/aocinput"
|
||||
input_file = "input.txt"
|
||||
|
||||
|
||||
def find_sum_of_two():
|
||||
"""
|
||||
Find two numbers from a list that add to a target.
|
||||
This is done by separating the numbers in a smaller than half and bigger than half of the target,
|
||||
and trying to find a match that would complement the sum, rather than adding everything together.
|
||||
:return: None
|
||||
"""
|
||||
large_n = []
|
||||
small_n = []
|
||||
|
||||
|
@ -14,6 +21,7 @@ def find_sum_of_two():
|
|||
if number > target/2:
|
||||
large_n.append(number)
|
||||
elif number == target/2:
|
||||
# Used for "early" exit if we find twice the half
|
||||
halftarget_count += 1
|
||||
else:
|
||||
small_n.append(number)
|
||||
|
@ -24,6 +32,7 @@ def find_sum_of_two():
|
|||
|
||||
line = file.readline()
|
||||
|
||||
# This was chosen in this order because of input I got : mainly large numbers
|
||||
for big_one in large_n:
|
||||
complement = target-big_one
|
||||
if complement in small_n:
|
||||
|
@ -32,7 +41,14 @@ def find_sum_of_two():
|
|||
|
||||
print("No sum found that can reach {target}")
|
||||
|
||||
|
||||
def sum_search(target_sum, inputs):
|
||||
"""
|
||||
Basically the same as above but for an arbitrary target and list of numbers.
|
||||
:param target_sum: Sum that we are looking numbers to reach
|
||||
:param inputs: List of numbers to search for a pair adding to target_sum
|
||||
:return: The pair if found, False otherwise
|
||||
"""
|
||||
lower = []
|
||||
upper = []
|
||||
|
||||
|
@ -42,6 +58,7 @@ def sum_search(target_sum, inputs):
|
|||
else:
|
||||
upper.append(num)
|
||||
|
||||
# As those list might be of a different repartition, search the smaller one while iterating on the larger one
|
||||
search_list = lower if len(lower) < len(upper) else upper
|
||||
iter_list = upper if len(lower) < len(upper) else lower
|
||||
|
||||
|
@ -53,24 +70,29 @@ def sum_search(target_sum, inputs):
|
|||
|
||||
|
||||
def find_sum_of_three():
|
||||
"""
|
||||
Find a triplet of number that reach a target when added together. This time, constructing a list by subtracting each
|
||||
number to the target, and using this list to check if the we can find two other numbers that add up to it.
|
||||
To put it another way, instead of searching for a,b and c in a+b+c = t, search for a,b in a+b = t-c for every c.
|
||||
:return: None
|
||||
"""
|
||||
data = []
|
||||
subtracted_to_target = []
|
||||
with open(input_file) as file:
|
||||
line = file.readline()
|
||||
while line and line != "\n":
|
||||
data.append(int(line))
|
||||
subtracted_to_target.append((target-data[-1],data[-1]))
|
||||
subtracted_to_target.append((target-data[-1], data[-1]))
|
||||
|
||||
line = file.readline()
|
||||
|
||||
# Honestly not sure if it helps
|
||||
data.sort()
|
||||
|
||||
for sub_target in subtracted_to_target:
|
||||
result = sum_search(sub_target[0],data)
|
||||
result = sum_search(sub_target[0], data)
|
||||
if result:
|
||||
print(f"Sum found : {result[0]}+{result[1]}+{sub_target[1]} = {target}\nResult is {result[0]*result[1]*sub_target[1]}")
|
||||
return
|
||||
|
||||
print("No sum found")
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
input_file = "/tmp/aocinput"
|
||||
input_file = "input.txt"
|
||||
|
||||
|
||||
def letter_count():
|
||||
valid_passwords = 0
|
||||
|
@ -16,6 +17,7 @@ def letter_count():
|
|||
|
||||
print(f"There are {valid_passwords} valid passwords")
|
||||
|
||||
|
||||
def letter_position():
|
||||
valid_passwords = 0
|
||||
|
||||
|
@ -29,4 +31,4 @@ def letter_position():
|
|||
|
||||
line = passwords.readline()
|
||||
|
||||
print(f"There are {valid_passwords} valid passwords")
|
||||
print(f"There are {valid_passwords} valid passwords")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
input_file = "/tmp/aocinput"
|
||||
input_file = "input.txt"
|
||||
|
||||
map_table = []
|
||||
|
||||
|
@ -8,7 +8,8 @@ with open(input_file) as slope_map:
|
|||
map_table.append(line)
|
||||
line = slope_map.readline()
|
||||
|
||||
def check_for_trees(direction=(3,1)):
|
||||
|
||||
def check_for_trees(direction=(3, 1)):
|
||||
x_pos = 0
|
||||
y_pos = 0
|
||||
|
||||
|
@ -17,8 +18,10 @@ def check_for_trees(direction=(3,1)):
|
|||
|
||||
tree_count = 0
|
||||
|
||||
# Might pose an issue for y speed different than a divisor of the number of lines ?
|
||||
while y_pos < height-1:
|
||||
x_pos,y_pos = ((x_pos+direction[0])%(width-1),y_pos+direction[1])
|
||||
# x wraps around as the map is repeated on the right indefinitely
|
||||
x_pos, y_pos = ((x_pos+direction[0]) % (width-1), y_pos+direction[1])
|
||||
if map_table[y_pos][x_pos] == "#":
|
||||
tree_count += 1
|
||||
|
||||
|
@ -26,6 +29,7 @@ def check_for_trees(direction=(3,1)):
|
|||
|
||||
return tree_count
|
||||
|
||||
|
||||
def check_multiple_directions(directions):
|
||||
result = 1
|
||||
for direction in directions:
|
||||
|
@ -33,4 +37,5 @@ def check_multiple_directions(directions):
|
|||
|
||||
print(f"Result is {result}.")
|
||||
|
||||
check_multiple_directions([(1,1),(3,1),(5,1),(7,1),(1,2)])
|
||||
|
||||
check_multiple_directions([(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)])
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
import re
|
||||
|
||||
input_file = "/tmp/aocinput"
|
||||
input_file = "input.txt"
|
||||
|
||||
height_re = re.compile(r'^([0-9]+)(in|cm)$')
|
||||
passport_id_re = re.compile(r'^[0-9]{9}$')
|
||||
color_re = re.compile(r'^#[0-9a-f]{6}$')
|
||||
|
||||
|
||||
def check_limits(to_test, min_valid, max_valid):
|
||||
return False if (to_test < min_valid or to_test > max_valid) else True
|
||||
|
||||
|
||||
def validate_passport(record):
|
||||
for field in record:
|
||||
if field[0] == "byr":
|
||||
|
@ -24,16 +26,16 @@ def validate_passport(record):
|
|||
regex_match = height_re.match(field[1])
|
||||
if not regex_match:
|
||||
return False
|
||||
if regex_match.groups()[1] == "cm" and not check_limits(int(regex_match.groups()[0]), 150,193):
|
||||
if regex_match.groups()[1] == "cm" and not check_limits(int(regex_match.groups()[0]), 150, 193):
|
||||
return False
|
||||
elif regex_match.groups()[1] == "in" and not check_limits(int(regex_match.groups()[0]),59,76):
|
||||
elif regex_match.groups()[1] == "in" and not check_limits(int(regex_match.groups()[0]), 59, 76):
|
||||
return False
|
||||
elif field[0] == "hcl":
|
||||
regex_match = color_re.match(field[1])
|
||||
if not regex_match:
|
||||
return False
|
||||
elif field[0] == "ecl":
|
||||
if field[1] not in ["amb","blu","brn","gry","grn","hzl","oth"]:
|
||||
if field[1] not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
|
||||
return False
|
||||
elif field[0] == "pid":
|
||||
regex_match = passport_id_re.match(field[1])
|
||||
|
@ -57,7 +59,9 @@ with open(input_file) as passports:
|
|||
line = passports.readline()
|
||||
continue
|
||||
|
||||
# rstrip() strips the string from end of line characters et alia.
|
||||
for raw_entry in line.rstrip().split(" "):
|
||||
# Split the different fields, then split them in (key,value) tuples
|
||||
records[current_index].append(tuple(raw_entry.split(":")))
|
||||
|
||||
line = passports.readline()
|
||||
|
@ -75,4 +79,4 @@ for record in records:
|
|||
if is_north_pole_credential and validate_passport(record):
|
||||
valid_passports += 1
|
||||
|
||||
print(f"There are {valid_passports} valid passports out of {len(records)}")
|
||||
print(f"There are {valid_passports} valid passports out of {len(records)}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue