day4 so far + formatz
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
venv/
|
||||
@@ -1,4 +1,2 @@
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Hoi Day 1!")
|
||||
print("Hoi Day 1!")
|
||||
|
||||
27
day2/day2.py
27
day2/day2.py
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def validate_password(line):
|
||||
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
|
||||
lower = int(parts[1])
|
||||
@@ -8,11 +9,12 @@ def validate_password(line):
|
||||
char = parts[3]
|
||||
|
||||
password = parts[4]
|
||||
password = re.sub('[^%s]'% char, '', parts[4])
|
||||
password = re.sub('[^%s]' % char, '', parts[4])
|
||||
length = len(password)
|
||||
if(length >= lower and length <= upper):
|
||||
if (length >= lower and length <= upper):
|
||||
return True
|
||||
|
||||
|
||||
def validate_password_2(line):
|
||||
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
|
||||
lower = int(parts[1]) - 1
|
||||
@@ -21,25 +23,28 @@ def validate_password_2(line):
|
||||
|
||||
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
|
||||
|
||||
|
||||
def part1():
|
||||
count = 0
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
if validate_password(line) == True:
|
||||
count += 1
|
||||
print("Day 1: %d"% count)
|
||||
|
||||
print("Day 1: %d" % count)
|
||||
|
||||
|
||||
def part2():
|
||||
count = 0
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
if validate_password_2(line) == True:
|
||||
count += 1
|
||||
print("Day 2: %d"% count)
|
||||
print("Day 2: %d" % count)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part1();
|
||||
part2();
|
||||
part2();
|
||||
|
||||
13
day3/day3.py
13
day3/day3.py
@@ -1,10 +1,12 @@
|
||||
lines = []
|
||||
|
||||
|
||||
def load():
|
||||
global lines
|
||||
file = open("day3/input.txt")
|
||||
lines = file.readlines()
|
||||
|
||||
|
||||
def trees(right, down):
|
||||
width = 0
|
||||
height = 0
|
||||
@@ -13,21 +15,24 @@ def trees(right, down):
|
||||
|
||||
while height < len(lines):
|
||||
line = lines[height]
|
||||
if(line[width % line_width] == "#"):
|
||||
if (line[width % line_width] == "#"):
|
||||
count += 1
|
||||
width += right
|
||||
height += down
|
||||
|
||||
return count
|
||||
|
||||
|
||||
def part1():
|
||||
print("Part 1: %d"% trees(3,1))
|
||||
print("Part 1: %d" % trees(3, 1))
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
load()
|
||||
part1()
|
||||
part2()
|
||||
part2()
|
||||
|
||||
117
day4/day4.py
Normal file
117
day4/day4.py
Normal 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
1104
day4/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user