My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2021/day08.py

+66 -1
+65
2021/day08.py
··· 1 + import fileinput 2 + from string import maketrans 3 + from itertools import permutations 4 + 5 + 6 + UNIQUES = { 7 + 1: 2, 8 + 4: 4, 9 + 7: 3, 10 + 8: 7, 11 + } 12 + 13 + DIGIT_MAP = { 14 + 0: 'abcefg', 15 + 1: 'cf', 16 + 2: 'acdeg', 17 + 3: 'acdfg', 18 + 4: 'bcdf', 19 + 5: 'abdfg', 20 + 6: 'abdefg', 21 + 7: 'acf', 22 + 8: 'abcdefg', 23 + 9: 'abcdfg', 24 + } 25 + 26 + SEGMENTS_MAP = {v: str(k) for k, v in DIGIT_MAP.items()} 27 + 28 + VALID_SEGMENTS = set(v for v in DIGIT_MAP.values()) 29 + 30 + ALL_SEGMENTS = 'abcdefg' 31 + 32 + part_1 = 0 33 + part_2 = 0 34 + 35 + for y, line in enumerate(fileinput.input()): 36 + input, output = line.strip().split(' | ') 37 + 38 + # Solve part 1 39 + for word in output.split(): 40 + if len(word) in UNIQUES.values(): 41 + part_1 += 1 42 + 43 + # Solve part 2 44 + for perm in permutations('abcdefg', 7): 45 + table = maketrans(ALL_SEGMENTS, perm) 46 + 47 + for word in input.split(): 48 + new_word = word.translate(table) 49 + new_word = ''.join(sorted(new_word)) 50 + 51 + if new_word not in VALID_SEGMENTS: 52 + break 53 + else: 54 + ans = '' 55 + for word in output.split(): 56 + new_word = word.translate(table) 57 + new_word = ''.join(sorted(new_word)) 58 + ans += SEGMENTS_MAP[new_word] 59 + 60 + part_2 += int(ans) 61 + 62 + 63 + print "Part 1:", part_1 64 + print "Part 2:", part_2 65 +
+1 -1
2021/starter.py
··· 26 26 board = {} 27 27 table = new_table(None, width=2, height=4) 28 28 29 - # Uncomment for multi-group style inputs. :c 29 + # # Uncomment for multi-group style inputs. :c 30 30 # data = ''.join([line for line in fileinput.input()]) 31 31 # groups = [g.split('\n') for g in data.split('\n\n')] 32 32