diff options
author | b-idea <test@test.com> | 2023-10-05 13:37:03 +0200 |
---|---|---|
committer | b-idea <test@test.com> | 2023-10-05 13:37:03 +0200 |
commit | f16943836d8862d12d3eead9e20c6775b06f1588 (patch) | |
tree | 3c84dd93c410461c4c1db2f02a8b15b1241e801f | |
parent | c1fdc53e46d456d6bfa4f5a416a1e58e8a6468ef (diff) |
added day 19 solution
-rw-r--r-- | 2018/aoc2018-d19.py | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/2018/aoc2018-d19.py b/2018/aoc2018-d19.py new file mode 100644 index 0000000..51ad39e --- /dev/null +++ b/2018/aoc2018-d19.py @@ -0,0 +1,180 @@ +#advent of code 2018 +#day 19 +#part 1 and part 2 + +#part 1 was very easy since it reused day 16 instructions +#for part 2 I was at first tricked by the maximum value stored in the register +#then I was tricked again by the value that was increasing by constant value +#which is really stupid on my part because I forgot that the answer to puzzle +#was value stored at register [0] - so none of the two above +#the value at reg[0] stores a sum of all divisors of +#the max value mentioned before +#it took me a while to understand it - that was a tricky part 2 (as usual) + +###opcode functions +#bit modified version of day 16 - removed 'after' list +def addr(before, op): + bef = before.copy(); + C = bef[op[1]] + bef[op[2]]; + bef[op[3]] = C; + return bef; +def addi(before, op): + bef = before.copy(); + C = bef[op[1]] + op[2]; + bef[op[3]] = C; + return bef; + +def mulr(before, op): + bef = before.copy(); + C = bef[op[1]] * bef[op[2]]; + bef[op[3]] = C; + return bef; +def muli(before, op): + bef = before.copy(); + C = bef[op[1]] * op[2]; + bef[op[3]] = C; + return bef; + +def banr(before, op): + bef = before.copy(); + C = int(bef[op[1]] & bef[op[2]]); + bef[op[3]] = C; + return bef; +def bani(before, op): + bef = before.copy(); + C = int(bef[op[1]] & op[2]); + bef[op[3]] = C; + return bef; + +def borr(before, op): + bef = before.copy(); + C = int(bef[op[1]] | bef[op[2]]); + bef[op[3]] = C; + return bef; +def bori(before, op): + bef = before.copy(); + C = int(bef[op[1]] | op[2]); + bef[op[3]] = C; + return bef; + +def setr(before, op): + bef = before.copy(); + C = bef[op[1]]; + bef[op[3]] = C; + return bef; +def seti(before, op): + bef = before.copy(); + C = op[1]; + bef[op[3]] = C; + return bef; + +def gtir(before, op): + bef = before.copy(); + C = 0 + int( op[1] > bef[op[2]] ); + bef[op[3]] = C; + return bef; +def gtri(before, op): + bef = before.copy(); + C = 0 + int( op[2] < bef[op[1]] ); + bef[op[3]] = C; + return bef; +def gtrr(before, op): + bef = before.copy(); + C = 0 + int( bef[op[1]] > bef[op[2]] ); + bef[op[3]] = C; + return bef; + +def eqir(before, op): + bef = before.copy(); + C = 0 + int( op[1] == bef[op[2]] ); + bef[op[3]] = C; + return bef; +def eqri(before, op): + bef = before.copy(); + C = 0 + int( op[2] == bef[op[1]] ); + bef[op[3]] = C; + return bef; +def eqrr(before, op): + bef = before.copy(); + C = 0 + int( bef[op[1]] == bef[op[2]] ); + bef[op[3]] = C; + return bef; + +MatchFunctions = {}; +MatchFunctions.update({"addr":addr}); +MatchFunctions.update({"addi":addi}); +MatchFunctions.update({"mulr":mulr}); +MatchFunctions.update({"muli":muli}); +MatchFunctions.update({"banr":banr}); +MatchFunctions.update({"bani":bani}); +MatchFunctions.update({"borr":borr}); +MatchFunctions.update({"bori":bori}); +MatchFunctions.update({"setr":setr}); +MatchFunctions.update({"seti":seti}); +MatchFunctions.update({"gtir":gtir}); +MatchFunctions.update({"gtri":gtri}); +MatchFunctions.update({"gtrr":gtrr}); +MatchFunctions.update({"eqir":eqir}); +MatchFunctions.update({"eqri":eqri}); +MatchFunctions.update({"eqrr":eqrr}); + +#end of day16 copy-paste +############################################### + +f = open("input.txt",'r'); +Testing = False; +#Testing = True; +if Testing: + f.close(); + f = open("testinput.txt",'r'); + +ip = int(f.readline()[4:]); +instructions = []; + +for l in f: + l = l.split(); + i = []; + i.append(l[0]); + l_i = [int(x) for x in l[1:]]; + i += l_i; + instructions.append(i); + +instr_lim = len(instructions); +reg = [0,0,0,0,0,0]; +reg[ip] = 0; + +while True: + if( reg[ip] >= instr_lim): break; + instr = instructions[reg[ip]]; + fun = MatchFunctions[instr[0]]; + reg = fun(reg,instr); + + reg[ip] += 1; + +part1 = reg[0]; +print("part 1 = ", part1); + +reg = [1,0,0,0,0,0]; +reg[ip] = 0; +counter = 1; + +while True: + if( reg[ip] >= instr_lim): break; + instr = instructions[reg[ip]]; + fun = MatchFunctions[instr[0]]; + reg = fun(reg,instr); + + if (counter%1000 == 0): + print(reg); + y1 = reg[1]; + break; + + reg[ip] += 1; + counter += 1; + +maxval = max(reg); +part2 = 0; +for x in range(1,maxval+1): + if maxval%x == 0: part2 += x; + +print("part 2 = ", part2); |