Advertisement
bob_f

AOC2021Day8.py

Sep 25th, 2023
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import pprint
  2.  
  3. signalPatterns = []
  4. outputValues = []
  5. digits = {
  6.     0 : None,
  7.     1 : None,
  8.     2 : None,
  9.     3 : None,
  10.     4 : None,
  11.     5 : None,
  12.     6 : None,
  13.     7 : None,
  14.     8 : None,
  15.     9 : None
  16. }
  17.  
  18. def initPuzzle():
  19.     with open('AdventOfCode2021Day08.txt') as INFILE:
  20.         for LINE in INFILE:
  21.  
  22.             if LINE.startswith('#'):
  23.                 continue
  24.  
  25.             print(f'{LINE=}')            
  26.             LINE = LINE.rstrip().split('|')
  27.             signalPatterns.extend([set(x) for x in LINE[0].split()])
  28.             outputValues.extend([set(x) for x in LINE[1].split()])
  29.  
  30. def getDigit1():
  31.     '''
  32.    Digit 1 is the only one with only two segments
  33.    '''
  34.     signalPattern = [x for x in signalPatterns if len(x) == 2]
  35.     return signalPattern[0]
  36.  
  37. def getDigit4():
  38.     '''
  39.    Digit 4 is the only one with only four segments
  40.    '''
  41.     signalPattern = [x for x in signalPatterns if len(x) == 4]
  42.     return signalPattern[0]
  43.  
  44. def getDigit7():
  45.     '''
  46.    Digit 7 is the only one with only three segments
  47.    '''
  48.     signalPattern = [x for x in signalPatterns if len(x) == 3]
  49.     return signalPattern[0]
  50.  
  51. def getDigit8():
  52.     '''
  53.    Digit 8 is the only one with all seven segments
  54.    '''
  55.     signalPattern = [x for x in signalPatterns if len(x) == 7]
  56.     return signalPattern[0]
  57.  
  58. def getDigit3():
  59.     '''
  60.    Digit 3 is the only one with five segments, and including all segments from digit 1
  61.    '''
  62.     for signalPattern in signalPatterns:
  63.         if len(signalPattern) == 5:
  64.             if digits[1] - signalPattern == set():
  65.                 return signalPattern
  66.  
  67.     return None
  68.  
  69. #
  70. # Main program
  71. #
  72.  
  73. initPuzzle()
  74. pprint.pprint(f'{signalPatterns}')
  75. pprint.pprint(f'{outputValues}')
  76.  
  77. # Get digits
  78.  
  79. digits[1] = getDigit1()
  80. digits[4] = getDigit4()
  81. digits[7] = getDigit7()
  82. digits[8] = getDigit8()
  83. digits[3] = getDigit3()
  84. pprint.pprint(digits)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement