# Daniel Kim # Bergen County Academies # Senior Division file_name = "3sr_sampledata.txt" def eight_adjacents(grid): horiz=["B","D","~B"] for k in range(0,3): works = True for i in range(k,k+2): for j in range(0,4): works = works & grid[i][j] if works: for i in range(k,k+2): for j in range(0,4): grid[i][j] = 0 return horiz[k] vert = ["A","C","~A"] for k in range(0,3): works = True for i in range(k,k+2): for j in range(0,4): works = works & grid[j][i] if works: for i in range(k,k+2): for j in range(0,4): grid[j][i] = 0 return vert[k] works = True for i in range(0,4): works = works & grid[i][0] & grid[i][3] if works: for i in range(0,4): grid[i][0] = 0 grid[i][3] = 0 return "~C" works = True for i in range(0,4): works = works & grid[0][i] & grid[3][i] if works: for i in range(0,4): grid[0][i] = 0 grid[3][i] = 0 return "~D" return None def four_adjacents(grid): horiz=["B~D","BD","~BD","~B~D"] for i in range(0,4): works = True for j in range(0,4): works = works & grid[i][j] if works: for j in range(0,4): grid[i][j] = 0 return horiz[i] vert=["A~C","AC","~AC","~A~C"] for i in range(0,4): works = True for j in range(0,4): works = works & grid[j][i] if works: for j in range(0,4): grid[j][i] = 0 return vert[i] blocks = ["AB","BC","~AB","AD","CD","~AD","A~B","~BC","~A~B"] for i in range(0,3): for j in range(0,3): works = grid[i][j] & grid[i+1][j] & grid[i][j+1] & grid[i+1][j+1] if works: grid[i][j] = 0 grid[i+1][j] = 0 grid[i][j+1] = 0 grid[i+1][j+1] = 0 return blocks[3*i+j] endrows = ["B~C","~CD","~B~C"] for k in range(0,3): works = True for i in range(k,k+2): works = works & grid[i][0] & grid[i][3] if works: for i in range(k,k+2): grid[i][0] = 0 grid[i][3] = 0 return endrows[k] endcols = ["A~D","C~D","~A~D"] for k in range(0,3): works = True for i in range(k,k+2): works = works & grid[0][i] & grid[3][i] if works: for i in range(k,k+2): grid[0][i] = 0 grid[3][i] = 0 return endcols[k] if grid[0][0] & grid[0][3] & grid[3][0] & grid[3][3]: grid[0][0] = 0 grid[0][3] = 0 grid[3][0] = 0 grid[3][3] = 0 return "~C~D" return None def two_adjacents(grid): horiz=["AB~D","BC~D","~AB~D","ABD","BCD","~ABD","A~BD","~BCD","~A~BD","A~B~D","~BC~D","~A~B~D"] for i in range(0,4): for j in range(0,3): if grid[i][j] & grid[i][j+1]: grid[i][j] = 0 grid[i][j+1] = 0 return horiz[3*i+j] vert=["AB~C","A~CD","A~B~C","ABC","ACD","A~BC","~ABC","~ACD","~A~BC","~AB~C","~A~CD","~A~B~C"] for i in range(0,4): for j in range(0,3): if grid[j][i] & grid[j+1][i]: grid[j][i] = 0 grid[j+1][i] = 0 return vert[3*i+j] endrows=["B~C~D","B~CD","~B~CD","~B~C~D"] for i in range(0,4): if grid[i][0] & grid[i][3]: grid[i][0] = 0 grid[i][3] = 0 return endrows[i] endcols=["A~C~D","AC~D","~AC~D","~A~C~D"] for i in range(0,4): if grid[0][i] & grid[3][i]: grid[0][i] = 0 grid[3][i] = 0 return endcols[i] return None def single(grid): for i in range(0,4): for j in range(0,4): if grid[i][j]: a = "" b = "" c = "" d = "" if i == 0: b = "B" d = "~D" elif i == 1: b = "B" d = "D" elif i == 2: b = "~B" d = "D" else: b = "~B" d = "~D" if j == 0: a = "A" c = "~C" elif j == 1: a = "A" c = "C" elif j == 2: a = "~A" c = "C" elif j == 3: a = "~A" c = "~C" grid[i][j] = 0 return a+b+c+d return None def decode(num): arr = [0 for i in range(4)] if 8 <= num: arr[0] = 1 num -= 8 if 4 <= num: arr[1] = 1 num -= 4 if 2 <= num: arr[2] = 1 num -= 2 if 1 <= num: arr[3] = 1 return arr with open(file_name, "r") as test_cases: for i in range(5): line = test_cases.readline().strip() grid = [] for c in line: grid.append(decode(int(c, 16))) expression = "" eights = eight_adjacents(grid) while (eights != None): expression += eights + "+" eights = eight_adjacents(grid) fours = four_adjacents(grid) while (fours != None): expression += fours + "+" fours = four_adjacents(grid) twos = two_adjacents(grid) while (twos != None): expression += twos + "+" twos = two_adjacents(grid) singles = single(grid) while (singles != None): expression += singles + "+" singles = single(grid) print(expression[0:len(expression)-1])