day4 so far + formatz

This commit is contained in:
2020-12-06 23:06:44 +01:00
parent 4d05cc0bb9
commit d9c12bc074
6 changed files with 1249 additions and 18 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.idea/
venv/

View File

@@ -1,4 +1,2 @@
if __name__ == "__main__": if __name__ == "__main__":
print("Hoi Day 1!") print("Hoi Day 1!")

View File

@@ -1,6 +1,7 @@
import os import os
import re import re
def validate_password(line): def validate_password(line):
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line) parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
lower = int(parts[1]) lower = int(parts[1])
@@ -8,11 +9,12 @@ def validate_password(line):
char = parts[3] char = parts[3]
password = parts[4] password = parts[4]
password = re.sub('[^%s]'% char, '', parts[4]) password = re.sub('[^%s]' % char, '', parts[4])
length = len(password) length = len(password)
if(length >= lower and length <= upper): if (length >= lower and length <= upper):
return True return True
def validate_password_2(line): def validate_password_2(line):
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line) parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
lower = int(parts[1]) - 1 lower = int(parts[1]) - 1
@@ -21,16 +23,18 @@ def validate_password_2(line):
password = parts[4] password = parts[4]
if((password[lower] is char) is not (password[upper] is char)): if ((password[lower] is char) is not (password[upper] is char)):
return True return True
def part1(): def part1():
count = 0 count = 0
with open("day2/input.txt") as input: with open("day2/input.txt") as input:
for line in input: for line in input:
if validate_password(line) == True: if validate_password(line) == True:
count += 1 count += 1
print("Day 1: %d"% count) print("Day 1: %d" % count)
def part2(): def part2():
count = 0 count = 0
@@ -38,7 +42,8 @@ def part2():
for line in input: for line in input:
if validate_password_2(line) == True: if validate_password_2(line) == True:
count += 1 count += 1
print("Day 2: %d"% count) print("Day 2: %d" % count)
if __name__ == "__main__": if __name__ == "__main__":
part1(); part1();

View File

@@ -1,10 +1,12 @@
lines = [] lines = []
def load(): def load():
global lines global lines
file = open("day3/input.txt") file = open("day3/input.txt")
lines = file.readlines() lines = file.readlines()
def trees(right, down): def trees(right, down):
width = 0 width = 0
height = 0 height = 0
@@ -13,20 +15,23 @@ def trees(right, down):
while height < len(lines): while height < len(lines):
line = lines[height] line = lines[height]
if(line[width % line_width] == "#"): if (line[width % line_width] == "#"):
count += 1 count += 1
width += right width += right
height += down height += down
return count return count
def part1(): def part1():
print("Part 1: %d"% trees(3,1)) print("Part 1: %d" % trees(3, 1))
def part2(): def part2():
count = trees(1,1) * trees(3,1) * trees(5,1) * trees(7,1) * trees(1,2) count = trees(1, 1) * trees(3, 1) * trees(5, 1) * trees(7, 1) * trees(1, 2)
print("Part 2: %d" % count) print("Part 2: %d" % count)
if __name__ == "__main__": if __name__ == "__main__":
load() load()
part1() part1()

117
day4/day4.py Normal file
View File

@@ -0,0 +1,117 @@
import re
lines = []
def load():
global lines
file = open("input_test.txt")
lines = file.readlines()
def resetCheck():
return {
'byr': False,
'iyr': False,
'eyr': False,
'hgt': False,
'hcl': False,
'ecl': False,
'pid': False,
'cid': False
}
def valid(password):
if not password['byr']:
return False
if not password['iyr']:
return False
if not password['eyr']:
return False
if not password['hgt']:
return False
if not password['hcl']:
return False
if not password['ecl']:
return False
if not password['pid']:
return False
return True
def validate_field(field, value):
if field == 'byr':
return 1920 <= int(value) <= 2002
elif field == 'iyr':
return 2010 <= int(value) <= 2020
elif field == 'eyr':
return 2020 <= int(value) <= 2030
elif field == 'hgt':
height = re.split('([0-9]+)([a-z]+)', value)
if len(height) < 3:
return False
if height[2] == 'cm':
return 150 <= int(height[1]) <= 193
elif height[2] == 'in':
return 59 <= int(height[1]) <= 76
else:
return False
elif field == 'hcl':
if re.match('#([0-9a-f]{6})', value):
return True
elif field == 'ecl':
return value == "amb" or value == "blu" or value == "brn" or value == "gry" or value == "grn" or value == "hzl" or value == "oth"
elif field == 'pid':
if re.match('([0-9]{9})', value):
return True
def part1():
file = open("input.txt")
count = 0
passport = resetCheck()
for line in file:
if line == "\n":
if valid(passport):
count += 1
passport = resetCheck()
passport_line = line.split(" ")
for field in passport_line:
field = field.split(':')[0]
passport[field] = True
if valid(passport):
count += 1
print("Part 1: %d" % count)
def part2():
file = open("input.txt")
count = 0
passport = resetCheck()
for line in file:
if line == "\n":
if valid(passport):
count += 1
passport = resetCheck()
else:
passport_line = line.split(" ")
for field in passport_line:
field = field.split(':')
passport[field[0]] = validate_field(field[0], field[1])
if valid(passport):
count += 1
print("Part 2: %d" % count)
if __name__ == "__main__":
load()
part1()
part2()

1104
day4/input.txt Normal file

File diff suppressed because it is too large Load Diff