-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle2.py
30 lines (25 loc) · 1.22 KB
/
puzzle2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from itertools import permutations
log = None
def solve_part_1(puzzle_input):
checksum_total = 0
puzzle_input = puzzle_input.replace(' ', '\t') # Helpful for the examples
for row in puzzle_input.split('\n'):
entries = tuple(map(int, row.split('\t'))) # Entries as ints
checksum_row = (max(entries) - min(entries)) # + checksum for cur. row
log('Row-Checksum: %d' % checksum_row)
checksum_total += checksum_row
return checksum_total # The solution is the total of all checksums
def solve_part_2(puzzle_input):
checksum_total = 0
puzzle_input = puzzle_input.replace(' ', '\t') # Helpful for the examples
for row in puzzle_input.split('\n'):
entries = tuple(map(int, row.split('\t'))) # Entries as ints
for entry1, entry2 in permutations(entries, 2):
if entry1 % entry2 is 0:
checksum_row = entry1 // entry2 # + checksum for cur. row
break
else: # If no checksum found:
raise Exception('Could not find any checksum for row: "%s"' % row)
log('Row-Checksum: %d' % checksum_row)
checksum_total += checksum_row
return checksum_total # The solution is the total of all checksums