Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pprint
- signalPatterns = []
- outputValues = []
- digits = {
- 0 : None,
- 1 : None,
- 2 : None,
- 3 : None,
- 4 : None,
- 5 : None,
- 6 : None,
- 7 : None,
- 8 : None,
- 9 : None
- }
- def initPuzzle():
- with open('AdventOfCode2021Day08.txt') as INFILE:
- for LINE in INFILE:
- if LINE.startswith('#'):
- continue
- print(f'{LINE=}')
- LINE = LINE.rstrip().split('|')
- signalPatterns.extend([set(x) for x in LINE[0].split()])
- outputValues.extend([set(x) for x in LINE[1].split()])
- def getDigit1():
- '''
- Digit 1 is the only one with only two segments
- '''
- signalPattern = [x for x in signalPatterns if len(x) == 2]
- return signalPattern[0]
- def getDigit4():
- '''
- Digit 4 is the only one with only four segments
- '''
- signalPattern = [x for x in signalPatterns if len(x) == 4]
- return signalPattern[0]
- def getDigit7():
- '''
- Digit 7 is the only one with only three segments
- '''
- signalPattern = [x for x in signalPatterns if len(x) == 3]
- return signalPattern[0]
- def getDigit8():
- '''
- Digit 8 is the only one with all seven segments
- '''
- signalPattern = [x for x in signalPatterns if len(x) == 7]
- return signalPattern[0]
- def getDigit3():
- '''
- Digit 3 is the only one with five segments, and including all segments from digit 1
- '''
- for signalPattern in signalPatterns:
- if len(signalPattern) == 5:
- if digits[1] - signalPattern == set():
- return signalPattern
- return None
- #
- # Main program
- #
- initPuzzle()
- pprint.pprint(f'{signalPatterns}')
- pprint.pprint(f'{outputValues}')
- # Get digits
- digits[1] = getDigit1()
- digits[4] = getDigit4()
- digits[7] = getDigit7()
- digits[8] = getDigit8()
- digits[3] = getDigit3()
- pprint.pprint(digits)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement