day 1 and 2
This commit is contained in:
commit
6e9b3b0288
2 changed files with 74 additions and 0 deletions
23
1/1.py
Executable file
23
1/1.py
Executable file
|
@ -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)
|
51
2/2.py
Executable file
51
2/2.py
Executable file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue