slow but functional part 2 day 7
This commit is contained in:
		
							parent
							
								
									e0b97af8df
								
							
						
					
					
						commit
						50b13d3b61
					
				
							
								
								
									
										3
									
								
								7/1.py
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								7/1.py
									
									
									
									
									
								
							@ -1,5 +1,4 @@
 | 
				
			|||||||
from itertools import product
 | 
					from itertools import product
 | 
				
			||||||
import random
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test(expression: list[str|int]) -> int:
 | 
					def test(expression: list[str|int]) -> int:
 | 
				
			||||||
    while len(expression) >= 3:
 | 
					    while len(expression) >= 3:
 | 
				
			||||||
@ -22,7 +21,7 @@ def genrate_expression(vals: list[int], ops: list[str]) -> list[str|int]:
 | 
				
			|||||||
    return expr
 | 
					    return expr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
with open('input.data') as file:
 | 
					with open('sample.data') as file:
 | 
				
			||||||
    inputs = []
 | 
					    inputs = []
 | 
				
			||||||
    for line in file:
 | 
					    for line in file:
 | 
				
			||||||
        vals = [int(val) for val in line.replace(':', '').split()]
 | 
					        vals = [int(val) for val in line.replace(':', '').split()]
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										41
									
								
								7/2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								7/2.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					from itertools import product
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def tokenize(vals: list[int], ops: list[str]) -> list[str|int]:
 | 
				
			||||||
 | 
					    tok = []
 | 
				
			||||||
 | 
					    for item in  zip(vals, ops):
 | 
				
			||||||
 | 
					        tok.extend(item)
 | 
				
			||||||
 | 
					    tok.append(vals[-1])
 | 
				
			||||||
 | 
					    return tok
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def compute(tokens: list[str|int]) -> int:
 | 
				
			||||||
 | 
					    # Case where we have 1 || 2 ==> 12
 | 
				
			||||||
 | 
					    while len(tokens) >= 3:
 | 
				
			||||||
 | 
					        # Pop off the first 3 items
 | 
				
			||||||
 | 
					        left, op, right = tokens[:3]
 | 
				
			||||||
 | 
					        tokens = tokens[3:] if len(tokens) > 3 else []
 | 
				
			||||||
 | 
					        # Now we do some computation and prepend the list with that result
 | 
				
			||||||
 | 
					        if op == '*':
 | 
				
			||||||
 | 
					            tokens.insert(0, left * right)
 | 
				
			||||||
 | 
					        elif op == '+':
 | 
				
			||||||
 | 
					            tokens.insert(0, left + right)
 | 
				
			||||||
 | 
					        # the coalesce case
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            tokens.insert(0, int(f'{left}{right}'))
 | 
				
			||||||
 | 
					    return tokens[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with open('input.data') as file:
 | 
				
			||||||
 | 
					    cases = []
 | 
				
			||||||
 | 
					    for line in file:
 | 
				
			||||||
 | 
					        line = line.replace(':', '').split()
 | 
				
			||||||
 | 
					        cases.append([int(val) for val in line])
 | 
				
			||||||
 | 
					    s = 0
 | 
				
			||||||
 | 
					    for case in cases:
 | 
				
			||||||
 | 
					        answer, vals = case[0], case[1:]
 | 
				
			||||||
 | 
					        results = set()
 | 
				
			||||||
 | 
					        for ops in list(product(['*', '+', '||'], repeat=len(vals)-1)):
 | 
				
			||||||
 | 
					            tokens = tokenize(vals, ops)
 | 
				
			||||||
 | 
					            results.add(compute(tokens))
 | 
				
			||||||
 | 
					        if answer in results:
 | 
				
			||||||
 | 
					            s += answer
 | 
				
			||||||
 | 
					    print(s)
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user