From 6e9b3b0288dea09ba2a6d49243f0f63457f76156 Mon Sep 17 00:00:00 2001 From: Ben Charlton Date: Mon, 2 Dec 2019 21:36:51 +0000 Subject: [PATCH] day 1 and 2 --- 1/1.py | 23 +++++++++++++++++++++++ 2/2.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 1/1.py create mode 100755 2/2.py diff --git a/1/1.py b/1/1.py new file mode 100755 index 0000000..d803e3c --- /dev/null +++ b/1/1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import sys + +def calcmass (mass): + mass = int(mass / 3) - 2 + return mass + +# run with '2' argument for part2 result. +part2 = (len(sys.argv) == 2 and sys.argv[1] == '2') + +sum = 0 +for line in sys.stdin: + line = int(line.rstrip()) + cm = calcmass(line) + sum += cm + if part2: + while cm > 0: + cm = calcmass(cm) + if cm > 0: + sum += cm + +print(sum) diff --git a/2/2.py b/2/2.py new file mode 100755 index 0000000..20ac47f --- /dev/null +++ b/2/2.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import sys + +def r(inst, pos): + return inst[inst[pos]] + +def w(inst, pos, v): + inst[inst[pos]] = v + +def perform(inst): + pos = 0 + while 1: + if inst[pos] == 1: + w(inst, pos+3, (r(inst,pos+1) + r(inst,pos+2))) + elif inst[pos] == 2: + w(inst, pos+3, (r(inst,pos+1) * r(inst,pos+2))) + elif inst[pos] == 99: + break + pos += 4 + return inst[0] + +# run with '2' argument for part2 result. +part2 = (len(sys.argv) == 2 and sys.argv[1] == '2') + +for line in sys.stdin: + instructions = line.rstrip().split(',') + + sx = {12} + sy = {2} + + if part2: + sx = range(0,99) + sy = range(0,99) + + for x in sx: + for y in sy: + # reset the list + ints = [] + for i in instructions: + ints.append(int(i)) + + # override values + ints[1] = x + ints[2] = y + res = perform(ints) + if part2: + if(res == 19690720): + print((100*x)+y) + else: + print(res) \ No newline at end of file