feat(users/Profpatsch): add advent of code 2020 day 1 2 3

Change-Id: I99d2882ac9ef5ede85032132f6727e7bad8f24eb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2564
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2021-02-27 21:46:31 +01:00
parent f3d00b84bb
commit ace8c656be
3 changed files with 165 additions and 0 deletions

View file

@ -0,0 +1,22 @@
import sys
l = []
with open('./input', 'r') as f:
for line in f:
l.append(int(line))
s = set(l)
res=None
for el in s:
for el2 in s:
if (2020-(el+el2)) in s:
res=(el, el2, 2020-(el+el2))
break
if res == None:
sys.exit("could not find a number that adds to 2020")
print(res)
print(res[0] * res[1] * res[2])

View file

@ -0,0 +1,77 @@
import sys
def parse(line):
a = line.split(sep=" ", maxsplit=1)
assert len(a) == 2
fromto = a[0].split(sep="-")
assert len(fromto) == 2
(from_, to) = (int(fromto[0]), int(fromto[1]))
charpass = a[1].split(sep=": ")
assert len(charpass) == 2
char = charpass[0]
assert len(char) == 1
pass_ = charpass[1]
assert pass_.endswith("\n")
pass_ = pass_[:-1]
return {
"from": from_,
"to": to,
"char": char,
"pass": pass_
}
def char_in_pass(char, pass_):
return pass_.count(char)
def validate_01(entry):
no = char_in_pass(entry["char"], entry["pass"])
if no < entry["from"]:
return { "too-small": entry }
elif no > entry["to"]:
return { "too-big": entry }
else:
return { "ok": entry }
def char_at_pos(char, pos, pass_):
assert pos <= len(pass_)
return pass_[pos-1] == char
def validate_02(entry):
one = char_at_pos(entry["char"], entry["from"], entry["pass"])
two = char_at_pos(entry["char"], entry["to"], entry["pass"])
if one and two:
return { "both": entry }
elif one:
return { "one": entry }
elif two:
return { "two": entry }
else:
return { "none": entry }
res01 = []
res02 = []
with open("./input", 'r') as f:
for line in f:
p = parse(line)
res01.append(validate_01(p))
res02.append(validate_02(p))
count01=0
for r in res01:
print(r)
if r.get("ok", False):
count01=count01+1
count02=0
for r in res02:
print(r)
if r.get("one", False):
count02=count02+1
elif r.get("two", False):
count02=count02+1
else:
pass
print("count 1: {}".format(count01))
print("count 2: {}".format(count02))

View file

@ -0,0 +1,66 @@
import itertools
import math
def tree_line(init):
return {
"init-len": len(init),
"known": '',
"rest": itertools.repeat(init)
}
def tree_line_at(pos, tree_line):
needed = (pos + 1) - len(tree_line["known"])
# internally advance the tree line to the position requested
if needed > 0:
tree_line["known"] = tree_line["known"] \
+ ''.join(
itertools.islice(
tree_line["rest"],
1+math.floor(needed / tree_line["init-len"])))
# print(tree_line)
return tree_line["known"][pos] == '#'
def tree_at(linepos, pos, trees):
return tree_line_at(pos, trees[linepos])
def slope_positions(trees, right, down):
line = 0
pos = 0
while line < len(trees):
yield (line, pos)
line = line + down
pos = pos + right
trees = []
with open("./input", 'r') as f:
for line in f:
line = line.rstrip()
trees.append(tree_line(line))
# print(list(itertools.islice(trees[0], 5)))
# print(list(map(
# lambda x: tree_at(0, x, trees),
# range(100)
# )))
# print(list(slope_positions(trees, right=3, down=1)))
def count_slope_positions(trees, slope):
count = 0
for (line, pos) in slope:
if tree_at(line, pos, trees):
count = count + 1
return count
print(
count_slope_positions(trees, slope_positions(trees, right=1, down=1))
*
count_slope_positions(trees, slope_positions(trees, right=3, down=1))
*
count_slope_positions(trees, slope_positions(trees, right=5, down=1))
*
count_slope_positions(trees, slope_positions(trees, right=7, down=1))
*
count_slope_positions(trees, slope_positions(trees, right=1, down=2))
)
# I realized I could have just used a modulo instead …