Add advent code for 2020 days 1-4
This commit is contained in:
parent
2c466792a4
commit
948009891a
12 changed files with 256 additions and 0 deletions
1
day_1/info.md
Normal file
1
day_1/info.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
https://adventofcode.com/2020/day/1
|
||||||
12
day_1/part_1.py
Normal file
12
day_1/part_1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
filename = input("Filename: ")
|
||||||
|
|
||||||
|
with open(filename) as file:
|
||||||
|
data = file.read().split("\n")
|
||||||
|
|
||||||
|
for number_1 in data:
|
||||||
|
n1 = int(number_1)
|
||||||
|
for number_2 in data:
|
||||||
|
n2 = int(number_2)
|
||||||
|
if number_1 != number_2:
|
||||||
|
if n1 + n2 == 2020:
|
||||||
|
print(f"{n1} * {n2} = {n1 * n2}")
|
||||||
14
day_1/part_2.py
Normal file
14
day_1/part_2.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
filename = input("Filename: ")
|
||||||
|
|
||||||
|
with open(filename) as file:
|
||||||
|
data = file.read().split("\n")
|
||||||
|
|
||||||
|
for number_1 in data:
|
||||||
|
n1 = int(number_1)
|
||||||
|
for number_2 in data:
|
||||||
|
n2 = int(number_2)
|
||||||
|
for number_3 in data:
|
||||||
|
n3 = int(number_3)
|
||||||
|
if number_1 != number_2 and number_2 != number_3:
|
||||||
|
if n1 + n2 + n3 == 2020:
|
||||||
|
print(f"{n1} * {n2} * {n3} = {n1 * n2 * n3}")
|
||||||
1
day_2/info.md
Normal file
1
day_2/info.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
https://adventofcode.com/2020/day/2
|
||||||
18
day_2/part_1.py
Normal file
18
day_2/part_1.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
with open("input") as file:
|
||||||
|
data = file.read().split("\n")
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
valid_passwords = 0
|
||||||
|
|
||||||
|
|
||||||
|
for password_data in data:
|
||||||
|
|
||||||
|
rule, password = password_data.split(": ")
|
||||||
|
rules = rule.split(" ")
|
||||||
|
min_len, max_len = rules[0].split("-")
|
||||||
|
|
||||||
|
if int(min_len) <= len(re.findall(rules[1], password)) <= int(max_len):
|
||||||
|
valid_passwords += 1
|
||||||
|
|
||||||
|
print(valid_passwords)
|
||||||
21
day_2/part_2.py
Normal file
21
day_2/part_2.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
with open("input") as file:
|
||||||
|
data = file.read().split("\n")
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
valid_passwords = 0
|
||||||
|
|
||||||
|
|
||||||
|
for password_data in data:
|
||||||
|
|
||||||
|
rule, password = password_data.split(": ")
|
||||||
|
rules = rule.split(" ")
|
||||||
|
char = rules[1]
|
||||||
|
pos1, pos2 = rules[0].split("-")
|
||||||
|
|
||||||
|
if password[int(pos1)-1] == char and not password[int(pos2)-1] == char:
|
||||||
|
valid_passwords += 1
|
||||||
|
if not password[int(pos1)-1] == char and password[int(pos2)-1] == char:
|
||||||
|
valid_passwords += 1
|
||||||
|
|
||||||
|
print(valid_passwords)
|
||||||
1
day_3/info.md
Normal file
1
day_3/info.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
https://adventofcode.com/2020/day/3
|
||||||
30
day_3/part_1.py
Normal file
30
day_3/part_1.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
with open("input") as f:
|
||||||
|
hill = f.read().split("\n")
|
||||||
|
|
||||||
|
# Index deltas
|
||||||
|
slope = [
|
||||||
|
1, # the change in vertical height of the toboggan
|
||||||
|
3 # the change in horizontal distance of the toboggan
|
||||||
|
]
|
||||||
|
|
||||||
|
# The starting position of the toboggan
|
||||||
|
position = [0, 0]
|
||||||
|
|
||||||
|
tree_count = 0
|
||||||
|
|
||||||
|
|
||||||
|
while position[0] < len(hill):
|
||||||
|
|
||||||
|
# Wrap the pattern as needed
|
||||||
|
if position[1] >= len(hill[position[0]]):
|
||||||
|
position[1] -= len(hill[position[0]])
|
||||||
|
|
||||||
|
# Check for trees
|
||||||
|
if hill[position[0]][position[1]] == "#":
|
||||||
|
tree_count += 1
|
||||||
|
|
||||||
|
|
||||||
|
position[0] += slope[0]
|
||||||
|
position[1] += slope[1]
|
||||||
|
|
||||||
|
print(f"Trees Hit: {tree_count}")
|
||||||
41
day_3/part_2.py
Normal file
41
day_3/part_2.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
with open("input") as f:
|
||||||
|
hill = f.read().split("\n")
|
||||||
|
|
||||||
|
# Index deltas
|
||||||
|
slopes = [
|
||||||
|
[1, 1], # right 1, down 1
|
||||||
|
[1, 3], # right 3, down 1
|
||||||
|
[1, 5], # right 5, down 1
|
||||||
|
[1, 7], # right 7, down 1
|
||||||
|
[2, 1] # right 1, down 2
|
||||||
|
]
|
||||||
|
|
||||||
|
# The starting position of the toboggan
|
||||||
|
|
||||||
|
tree_counts = []
|
||||||
|
|
||||||
|
for slope in slopes:
|
||||||
|
|
||||||
|
position = [0, 0]
|
||||||
|
tree_count = 0
|
||||||
|
|
||||||
|
while position[0] < len(hill):
|
||||||
|
|
||||||
|
# Wrap the pattern as needed
|
||||||
|
if position[1] >= len(hill[position[0]]):
|
||||||
|
position[1] -= len(hill[position[0]])
|
||||||
|
|
||||||
|
# Check for trees
|
||||||
|
if hill[position[0]][position[1]] == "#":
|
||||||
|
tree_count += 1
|
||||||
|
|
||||||
|
|
||||||
|
position[0] += slope[0]
|
||||||
|
position[1] += slope[1]
|
||||||
|
|
||||||
|
tree_counts.append(tree_count)
|
||||||
|
|
||||||
|
total = 1
|
||||||
|
for x in tree_counts:
|
||||||
|
total *= x
|
||||||
|
print(tree_counts, total)
|
||||||
8
day_4/info.md
Normal file
8
day_4/info.md
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
https://adventofcode.com/2020/day/4
|
||||||
|
|
||||||
|
|
||||||
|
Part 1 Wrong Answers:
|
||||||
|
- `260` (too low)
|
||||||
|
|
||||||
|
Part 2 Wrong Answers:
|
||||||
|
- `225` (too high)
|
||||||
25
day_4/part_1.py
Normal file
25
day_4/part_1.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
with open("input") as f:
|
||||||
|
data = [x.replace("\n", " ") for x in f.read().split("\n\n")]
|
||||||
|
|
||||||
|
# The fields that passports have, if True, it's required, if False, it's an
|
||||||
|
# optional field
|
||||||
|
fields = {
|
||||||
|
"byr": True,
|
||||||
|
"iyr": True,
|
||||||
|
"eyr": True,
|
||||||
|
"hgt": True,
|
||||||
|
"hcl": True,
|
||||||
|
"ecl": True,
|
||||||
|
"pid": True,
|
||||||
|
"cid": False
|
||||||
|
}
|
||||||
|
|
||||||
|
valid_passports = len(data)
|
||||||
|
|
||||||
|
for passport in data:
|
||||||
|
for field, required in fields.items():
|
||||||
|
if field not in passport and required:
|
||||||
|
valid_passports -= 1
|
||||||
|
break
|
||||||
|
|
||||||
|
print(f"Valid Passports: {valid_passports}")
|
||||||
84
day_4/part_2.py
Normal file
84
day_4/part_2.py
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
with open("input") as f:
|
||||||
|
data = [x.replace("\n", " ") for x in f.read().split("\n\n")]
|
||||||
|
|
||||||
|
# The fields that passports have
|
||||||
|
fields = [
|
||||||
|
"byr",
|
||||||
|
"iyr",
|
||||||
|
"eyr",
|
||||||
|
"hgt",
|
||||||
|
"hcl",
|
||||||
|
"ecl",
|
||||||
|
"pid"
|
||||||
|
]
|
||||||
|
|
||||||
|
def year_validate(min, provided, max):
|
||||||
|
return int(min) <= int(provided) <= int(max)
|
||||||
|
|
||||||
|
def size_validate(height):
|
||||||
|
if height.endswith("cm"):
|
||||||
|
return 150 <= int(height[:-2]) <= 193
|
||||||
|
elif height.endswith("in"):
|
||||||
|
return 59 <= int(height[:-2]) <= 76
|
||||||
|
return False
|
||||||
|
|
||||||
|
def hair_validate(colour):
|
||||||
|
return re.match(r"#[\w]{6}", str(colour)) != None
|
||||||
|
|
||||||
|
def eye_validate(colour):
|
||||||
|
return colour in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth")
|
||||||
|
|
||||||
|
def pid_validate(id):
|
||||||
|
return re.match(r"^[0-9]{9}$", str(id)) != None
|
||||||
|
|
||||||
|
valid_passports = 0
|
||||||
|
|
||||||
|
# Check all the passports for valid data
|
||||||
|
for passport in data:
|
||||||
|
valid = True
|
||||||
|
|
||||||
|
# Check each required field
|
||||||
|
for field in fields:
|
||||||
|
|
||||||
|
# Ensure passport has field
|
||||||
|
if (i := passport.find(field)) != -1:
|
||||||
|
value = passport[i:].split(" ")[0].split(":")[1]
|
||||||
|
|
||||||
|
if field == "byr":
|
||||||
|
if not year_validate(1920, value, 2002):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "iyr":
|
||||||
|
if not year_validate(2010, value, 2020):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "eyr":
|
||||||
|
if not year_validate(2020, value, 2030):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "hgt":
|
||||||
|
if not size_validate(value):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "hcl":
|
||||||
|
if not hair_validate(value):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "ecl":
|
||||||
|
if not eye_validate(value):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
elif field == "pid":
|
||||||
|
if not pid_validate(value):
|
||||||
|
valid = False
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
valid = False
|
||||||
|
|
||||||
|
if valid:
|
||||||
|
valid_passports += 1
|
||||||
|
|
||||||
|
|
||||||
|
print(f"Valid Passports: {valid_passports}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue