renamed python files day* -> Day*

to have all files sorted by day in gitlab overview
This commit is contained in:
Stefan Forstenlechner 2020-12-16 20:29:43 +01:00
parent 9890182ea1
commit e12d28d9a4
6 changed files with 302 additions and 0 deletions

99
Day1.py Normal file
View File

@ -0,0 +1,99 @@
expense_report = []
with open('day1/input') as reader:
expense_report.extend([int(l) for l in reader.readlines()])
expense_report.sort()
def calc_two(sub_set, absolute):
first_i = 0
second_i = len(sub_set) - 1
first = sub_set[first_i]
second = sub_set[second_i]
while first_i < second_i and first + second + absolute != 2020:
if first + second + absolute > 2020:
second_i -= 1
second = sub_set[second_i]
else:
first_i += 1
first = sub_set[first_i]
if first_i < second_i:
return (first_i, second_i, first, second)
return None
# part one
# first_i = 0
# second_i = len(expense_report) - 1
# first = expense_report[first_i]
# second = expense_report[second_i]
#
# while first + second != 2020:
# if first + second > 2020:
# second_i -= 1
# second = expense_report[second_i]
# else:
# first_i += 1
# first = expense_report[first_i]
first_i, second_i, first, second = calc_two(expense_report, 0)
print(first, second)
print(first * second)
# second part
for i in range(len(expense_report) - 1):
res = calc_two(expense_report[i+1:], expense_report[i])
if res is not None:
print(res[2], res[3], expense_report[i])
print(res[2] * res[3] * expense_report[i])
break
# second part better?
# does not work! and probably never will!
# first_i = 0
# second_i = 1
# third_i = len(expense_report) - 1
# first = expense_report[first_i]
# second = expense_report[second_i]
# third = expense_report[third_i]
#
# up_with_second = first + second + third < 2020
#
# while first + second + third != 2020:
# if first + second + third > 2020:
# if up_with_second:
# if second_i+1 == third_i:
# second_i -= 1
# second = expense_report[second_i]
# third_i -= 1
# third = expense_report[third_i]
# else:
# if second_i-1 != first_i:
# second_i -= 1
# second = expense_report[second_i]
# else:
# third_i -= 1
# third = expense_report[third_i]
# up_with_second = True
# else:
# if up_with_second:
# if second_i+1 != third_i:
# second_i += 1
# second = expense_report[second_i]
# else:
# first_i += 1
# first = expense_report[first_i]
# up_with_second = False
# else:
# if second_i-1 == first_i:
# second_i += 1
# second = expense_report[second_i]
# first_i += 1
# first = expense_report[first_i]
#
#
# print(first, second, third)
# print(first * second * third)

32
Day2.py Normal file
View File

@ -0,0 +1,32 @@
pw_db = []
with open('day2/input') as reader:
for l in reader.readlines():
s = l.split()
split_restriction = s[0].index("-")
restriction_min = int(s[0][:split_restriction])
restriction_max = int(s[0][split_restriction+1:])
c = s[1][:-1]
p = s[2]
pw_db.append(((restriction_min, restriction_max), c, p))
valids = 0
for pw in pw_db:
count = pw[2].count(pw[1])
if pw[0][0] <= count <= pw[0][1]:
valids += 1
print(valids)
valids2 = 0
for pw in pw_db:
first = pw[0][0]
second = pw[0][1]
c = pw[1]
p = pw[2]
l = len(p)
if (first <= l and p[first-1] == c) is not (second <= l and p[second-1] == c):
valids2 += 1
print(valids2)

44
Day3.py Normal file
View File

@ -0,0 +1,44 @@
import functools
slope_map = []
with open('day3/input') as reader:
slope_map.extend(reader.read().splitlines())
def check_slope(right, down):
global slope_map
pos = 0
y = 1
trees = 0
for l in slope_map[1:]:
if y % down == 0:
pos = (pos + right) % len(l)
if l[pos] == '#':
trees += 1
y += 1
return trees
def check_slope_short(right, down):
global slope_map
trees = 0
for (i, l) in enumerate(slope_map):
if i % down == 0:
pos = (i // down * right) % len(l)
if l[pos] == '#':
trees += 1
return trees
def check_slope_even_short(right, down):
global slope_map
return len(list(filter(lambda b: b, map(lambda en: en[1][(en[0] // down * right) % len(en[1])] == '#', filter(lambda en: en[0] % down == 0, enumerate(slope_map))))))
slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
total_trees = functools.reduce(lambda prev, slope: prev * check_slope_even_short(*slope), slopes, 1)
print(total_trees)

70
Day4.py Normal file
View File

@ -0,0 +1,70 @@
import re
passports = []
with open('day4/input') as reader:
cur = ''
for line in reader.read().splitlines():
if line != '':
if cur == '':
cur += line
else:
cur += ' ' + line
else:
passports.append(cur)
cur = ''
if cur != '':
passports.append(cur)
def byr(s):
return re.match('^\d{4}$', s) is not None and (1920 <= int(s) <= 2002)
def iyr(s):
return re.match('^\d{4}$', s) is not None and (2010 <= int(s) <= 2020)
def eyr(s):
return re.match('^\d{4}$', s) is not None and (2020 <= int(s) <= 2030)
def hgt(s):
return (re.match('^\d+cm$', s) is not None and (150 <= int(s[:-2]) <= 193)) \
or (re.match('^\d+in$', s) is not None and (59 <= int(s[:-2]) <= 76))
def hcl(s):
return re.match('^#[0-9a-f]{6}$', s) is not None
def ecl(s):
return s in {'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'}
def pid(s):
return re.match('^[0-9]{9}$', s) is not None
verify = {'byr': byr,
'iyr': iyr,
'eyr': eyr,
'hgt': hgt,
'hcl': hcl,
'ecl': ecl,
'pid': pid,
'cid': lambda s: True
}
valid = set(verify.keys())
count_valids = 0
for p in passports:
props = set()
failed = False
for prop in p.split(' '):
key, value = prop.split(':')
props.add(key)
if not verify[key](value):
failed = True
break
if failed:
continue
missing = valid.difference(props)
if missing == set() or missing == {'cid'}:
count_valids += 1
print(count_valids)

26
Day5.py Normal file
View File

@ -0,0 +1,26 @@
boarding_passes = []
with open('day5/input') as reader:
boarding_passes.extend(reader.read().splitlines())
def seat_id(boarding):
binary = boarding \
.replace('F', '0') \
.replace('B', '1') \
.replace('L', '0') \
.replace('R', '1')
return int(binary, 2)
res = max(map(seat_id, boarding_passes))
print(res)
# second part
all_seats = sorted(map(seat_id, boarding_passes))
for i in range(len(all_seats)):
if all_seats[i] + 2 == all_seats[i + 1]:
print(all_seats[i] + 1)
break

31
Day_1.py Normal file
View File

@ -0,0 +1,31 @@
expense_report = []
with open('day1/input') as reader:
expense_report.extend([int(l) for l in reader.readlines()])
expense_report.sort()
def calc_two(sub_set, absolute):
first_i = 0
second_i = len(sub_set) - 1
first = sub_set[first_i]
second = sub_set[second_i]
while first_i < second_i and first + second + absolute != 2020:
if first + second + absolute > 2020:
second_i -= 1
second = sub_set[second_i]
else:
first_i += 1
first = sub_set[first_i]
if first_i < second_i:
return (first, second)
return None
for i in range(len(expense_report) - 1):
res = calc_two(expense_report[i+1:], expense_report[i])
if res is not None:
print(res[0], res[1], expense_report[i])
print(res[0] * res[1] * expense_report[i])
break