Realized afterwards I got the ints() helper, so original code before cleanup splitted the input and mapped all words with int(). valuable seconds lost there. Also, collections.Counter() was used initially since I was too tired to remember count() method in lists. Line 8 took the longest time to figure out. A typo took 1-3 minutes to find for part 2. Form: Sleep deprived, felt slow.
20 lines
433 B
Python
20 lines
433 B
Python
from output import ints
|
|
|
|
|
|
def solve(puzzle_input):
|
|
left, right = [sorted(col) for col in zip(*map(ints, puzzle_input.splitlines()))]
|
|
|
|
p1 = sum(abs(l - r) for l, r in zip(left, right))
|
|
p2 = sum(k * right.count(k) for k in left)
|
|
|
|
return p1, p2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("./input/01.txt", "r") as f:
|
|
puzzle_input = f.read().strip()
|
|
|
|
p1, p2 = solve(puzzle_input)
|
|
|
|
print(p1)
|
|
print(p2)
|