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
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
**/.idea
|
|
@ -1,7 +1,14 @@
|
||||||
target = 2020
|
target = 2020
|
||||||
input_file = "/tmp/aocinput"
|
input_file = "input.txt"
|
||||||
|
|
||||||
|
|
||||||
def find_sum_of_two():
|
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 = []
|
large_n = []
|
||||||
small_n = []
|
small_n = []
|
||||||
|
|
||||||
|
@ -14,6 +21,7 @@ def find_sum_of_two():
|
||||||
if number > target/2:
|
if number > target/2:
|
||||||
large_n.append(number)
|
large_n.append(number)
|
||||||
elif number == target/2:
|
elif number == target/2:
|
||||||
|
# Used for "early" exit if we find twice the half
|
||||||
halftarget_count += 1
|
halftarget_count += 1
|
||||||
else:
|
else:
|
||||||
small_n.append(number)
|
small_n.append(number)
|
||||||
|
@ -24,6 +32,7 @@ def find_sum_of_two():
|
||||||
|
|
||||||
line = file.readline()
|
line = file.readline()
|
||||||
|
|
||||||
|
# This was chosen in this order because of input I got : mainly large numbers
|
||||||
for big_one in large_n:
|
for big_one in large_n:
|
||||||
complement = target-big_one
|
complement = target-big_one
|
||||||
if complement in small_n:
|
if complement in small_n:
|
||||||
|
@ -32,7 +41,14 @@ def find_sum_of_two():
|
||||||
|
|
||||||
print("No sum found that can reach {target}")
|
print("No sum found that can reach {target}")
|
||||||
|
|
||||||
|
|
||||||
def sum_search(target_sum, inputs):
|
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 = []
|
lower = []
|
||||||
upper = []
|
upper = []
|
||||||
|
|
||||||
|
@ -42,6 +58,7 @@ def sum_search(target_sum, inputs):
|
||||||
else:
|
else:
|
||||||
upper.append(num)
|
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
|
search_list = lower if len(lower) < len(upper) else upper
|
||||||
iter_list = upper if len(lower) < len(upper) else lower
|
iter_list = upper if len(lower) < len(upper) else lower
|
||||||
|
|
||||||
|
@ -53,6 +70,12 @@ def sum_search(target_sum, inputs):
|
||||||
|
|
||||||
|
|
||||||
def find_sum_of_three():
|
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 = []
|
data = []
|
||||||
subtracted_to_target = []
|
subtracted_to_target = []
|
||||||
with open(input_file) as file:
|
with open(input_file) as file:
|
||||||
|
@ -63,6 +86,7 @@ def find_sum_of_three():
|
||||||
|
|
||||||
line = file.readline()
|
line = file.readline()
|
||||||
|
|
||||||
|
# Honestly not sure if it helps
|
||||||
data.sort()
|
data.sort()
|
||||||
|
|
||||||
for sub_target in subtracted_to_target:
|
for sub_target in subtracted_to_target:
|
||||||
|
@ -72,5 +96,3 @@ def find_sum_of_three():
|
||||||
return
|
return
|
||||||
|
|
||||||
print("No sum found")
|
print("No sum found")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
input_file = "/tmp/aocinput"
|
input_file = "input.txt"
|
||||||
|
|
||||||
|
|
||||||
def letter_count():
|
def letter_count():
|
||||||
valid_passwords = 0
|
valid_passwords = 0
|
||||||
|
@ -16,6 +17,7 @@ def letter_count():
|
||||||
|
|
||||||
print(f"There are {valid_passwords} valid passwords")
|
print(f"There are {valid_passwords} valid passwords")
|
||||||
|
|
||||||
|
|
||||||
def letter_position():
|
def letter_position():
|
||||||
valid_passwords = 0
|
valid_passwords = 0
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
input_file = "/tmp/aocinput"
|
input_file = "input.txt"
|
||||||
|
|
||||||
map_table = []
|
map_table = []
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ with open(input_file) as slope_map:
|
||||||
map_table.append(line)
|
map_table.append(line)
|
||||||
line = slope_map.readline()
|
line = slope_map.readline()
|
||||||
|
|
||||||
|
|
||||||
def check_for_trees(direction=(3, 1)):
|
def check_for_trees(direction=(3, 1)):
|
||||||
x_pos = 0
|
x_pos = 0
|
||||||
y_pos = 0
|
y_pos = 0
|
||||||
|
@ -17,7 +18,9 @@ def check_for_trees(direction=(3,1)):
|
||||||
|
|
||||||
tree_count = 0
|
tree_count = 0
|
||||||
|
|
||||||
|
# Might pose an issue for y speed different than a divisor of the number of lines ?
|
||||||
while y_pos < height-1:
|
while y_pos < height-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])
|
x_pos, y_pos = ((x_pos+direction[0]) % (width-1), y_pos+direction[1])
|
||||||
if map_table[y_pos][x_pos] == "#":
|
if map_table[y_pos][x_pos] == "#":
|
||||||
tree_count += 1
|
tree_count += 1
|
||||||
|
@ -26,6 +29,7 @@ def check_for_trees(direction=(3,1)):
|
||||||
|
|
||||||
return tree_count
|
return tree_count
|
||||||
|
|
||||||
|
|
||||||
def check_multiple_directions(directions):
|
def check_multiple_directions(directions):
|
||||||
result = 1
|
result = 1
|
||||||
for direction in directions:
|
for direction in directions:
|
||||||
|
@ -33,4 +37,5 @@ def check_multiple_directions(directions):
|
||||||
|
|
||||||
print(f"Result is {result}.")
|
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
|
import re
|
||||||
|
|
||||||
input_file = "/tmp/aocinput"
|
input_file = "input.txt"
|
||||||
|
|
||||||
height_re = re.compile(r'^([0-9]+)(in|cm)$')
|
height_re = re.compile(r'^([0-9]+)(in|cm)$')
|
||||||
passport_id_re = re.compile(r'^[0-9]{9}$')
|
passport_id_re = re.compile(r'^[0-9]{9}$')
|
||||||
color_re = re.compile(r'^#[0-9a-f]{6}$')
|
color_re = re.compile(r'^#[0-9a-f]{6}$')
|
||||||
|
|
||||||
|
|
||||||
def check_limits(to_test, min_valid, max_valid):
|
def check_limits(to_test, min_valid, max_valid):
|
||||||
return False if (to_test < min_valid or to_test > max_valid) else True
|
return False if (to_test < min_valid or to_test > max_valid) else True
|
||||||
|
|
||||||
|
|
||||||
def validate_passport(record):
|
def validate_passport(record):
|
||||||
for field in record:
|
for field in record:
|
||||||
if field[0] == "byr":
|
if field[0] == "byr":
|
||||||
|
@ -57,7 +59,9 @@ with open(input_file) as passports:
|
||||||
line = passports.readline()
|
line = passports.readline()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# rstrip() strips the string from end of line characters et alia.
|
||||||
for raw_entry in line.rstrip().split(" "):
|
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(":")))
|
records[current_index].append(tuple(raw_entry.split(":")))
|
||||||
|
|
||||||
line = passports.readline()
|
line = passports.readline()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Advent of Code - trotFunky's pot O' code
|
# Advent of Code - trotFunky's pot O' code
|
||||||
|
|
||||||
This is the repository where I'll be storing my solutions for the [Advent of Code](https://adventofcode.com) event run by [Eric Wastl](http://was.tl/).
|
This is the repository where I'll be storing my solutions for the [Advent of Code](https://adventofcode.com) event run by [Eric Wastl](http://was.tl/).
|
||||||
The directory structure is simple : `year/day`, each day containing the input file and at least one file containing my solution(s).
|
The directory structure is simple : `year/day`, each day containing the input file and at least one file containing my solution(s). The prompts of each puzzles will not be repeated here, so go check them on the site !
|
||||||
|
|
||||||
## Depencies
|
## Depencies
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue