diff --git a/day_7/input.txt b/day_7/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..74d32f019010bb764ba027ec0395593b7a4de181 --- /dev/null +++ b/day_7/input.txt @@ -0,0 +1,4 @@ +3,8,1001,8,10,8,105,1,0,0,21,34,51,76,101,114,195,276,357,438,99999,3,9,1001,9,3,9,1002,9,3,9,4,9,99,3,9,101,4,9,9,102,4,9,9,1001,9,5,9,4,9,99,3,9,1002,9,4,9,101,3,9,9,102,5,9,9,1001,9,2,9,1002,9,2,9,4,9,99,3,9,1001,9,3,9,102,2,9,9,101,4,9,9,102,3,9,9,101,2,9,9,4,9,99,3,9,102,2,9,9,101,4,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99 +3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33, 1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0 +3,23,3,24,1002,24,10,24,1002,23,-1,23, 101,5,23,23,1,24,23,23,4,23,99,0,0 +3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 diff --git a/day_7/main.py b/day_7/main.py new file mode 100644 index 0000000000000000000000000000000000000000..f551d04559e1101b1ecf44a06c49cc78acbbf568 --- /dev/null +++ b/day_7/main.py @@ -0,0 +1,112 @@ +"""Day 7: Amplification Circuit.""" + +from itertools import permutations + +def get_codes(): + with open("input.txt") as f: + return [int(num) for num in f.readline().strip().split(",")] + + +def get_opcode(opcode): + tmp = int(opcode) + op = tmp % 100 + tmp //= 100 + h = tmp % 10 + tmp //= 10 + k = tmp % 10 + tmp //= 10 + t = tmp % 10 + + if op in (1, 2, 7, 8): + t = 1 + if op in (3,): + h = 1 + + return (h, k, t), op + + +def get_next_command(ip, codes): + parm_counts = {1: 3, 2: 3, 3: 1, 4: 1, 5: 2, 6: 2, 7: 3, 8: 3, 99: 0} + modes, op = get_opcode(codes[ip]) + + params = [0, 0, 0] + ip += 1 + for x in range(parm_counts[op]): + params[x] = codes[ip] + if modes[x] == 0: + params[x] = codes[params[x]] + ip += 1 + + return ip, op, params + + +def exec_command(ip, op, params, codes, inputs, output): + if op == 1: # add + codes[params[2]] = params[0] + params[1] + elif op == 2: # multiply + codes[params[2]] = params[0] * params[1] + elif op == 3: # inout + # val = int(input("Enter input val? ")) + val = inputs.pop(0) + codes[params[0]] = val + elif op == 4: # output + # print(f"Output value = ({params[0]})") + output = params[0] + elif op == 5: # branch true + if params[0] != 0: + ip = params[1] + elif op == 6: # branch false + if params[0] == 0: + ip = params[1] + elif op == 7: # less than + if params[0] < params[1]: + codes[params[2]] = 1 + else: + codes[params[2]] = 0 + elif op == 8: # equals + if params[0] == params[1]: + codes[params[2]] = 1 + else: + codes[params[2]] = 0 + elif op == 99: # halt + ip = -1 + else: + print(f"Bad Op Code {op}") + ip = -1 + + return ip, output + + +def run_program(codes, inputs, output): + ip = 0 + while ip != -1: + ip, op, params = get_next_command(ip, codes) + ip, output = exec_command(ip, op, params, codes, inputs, output) + return output + +def main(): + """Drive the program.""" + inputs = list(permutations(range(5))) + + # inputs = [(2,0,3, 0, 1,0,0,0,4,0)] + # print (inputs[:10]) + # return + max = -1 + sig = [] + seq = [] + for series in inputs: + output = 0 + l = iter(series) + out = [] + for _ in range(5): + codes = get_codes() + output = run_program(codes, [next(l), output], output) + + if output > max: + max = output + sig = series + seq = output + print(f'Max({max}), Signal({sig}) Out({seq})') + +if __name__ == "__main__": + main()