Solve 2022 day 3 pt 1-2
This commit is contained in:
parent
8e49f8b3d5
commit
62d19f09f6
1 changed files with 28 additions and 0 deletions
28
2022-python/output/day_03.py
Normal file
28
2022-python/output/day_03.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def solve(data):
|
||||
rs = data.split()
|
||||
p1 = sum(map(_p1, rs))
|
||||
p2 = sum(map(_p2, zip(rs[::3], rs[1::3], rs[2::3])))
|
||||
return p1, p2
|
||||
|
||||
|
||||
def _p1(rs):
|
||||
c = (set(rs[: len(rs) // 2]) & set(rs[len(rs) // 2 :])).pop()
|
||||
return ord(c) - 96 if c.islower() else ord(c) - 38
|
||||
|
||||
|
||||
def _p2(rsg):
|
||||
c = (set(rsg[0]) & set(rsg[1]) & set(rsg[2])).pop()
|
||||
return ord(c) - 96 if c.islower() else ord(c) - 38
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/03.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 7817
|
||||
assert p2 == 2444
|
||||
Loading…
Add table
Reference in a new issue