-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.py
More file actions
67 lines (56 loc) · 2.09 KB
/
day5.py
File metadata and controls
67 lines (56 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from boilerplates import read_number_from_file
from intcode import get_output
def get_output2(init_inst, system_id):
inst_pnt = 0
cnt = 0
while init_inst[inst_pnt] != 99:
cnt += 1
instruction = init_inst[inst_pnt]
positionals = str(instruction)[:-2][::-1] + "00"
instruction = instruction % 100
pos_2 = ""
if positionals[0] == "1":
pos_1 = init_inst[inst_pnt + 1]
else:
pos_1 = init_inst[init_inst[inst_pnt + 1]]
if instruction not in [3, 4]:
if positionals[1] == "1" :
pos_2 = init_inst[inst_pnt + 2]
else:
pos_2 = init_inst[init_inst[inst_pnt + 2]]
#print(positionals, instruction, pos_1, pos_2)
if instruction == 1:
init_inst[init_inst[inst_pnt + 3]] = pos_1 + pos_2
inst_pnt += 4
elif instruction == 2:
init_inst[init_inst[inst_pnt + 3]] = pos_1 * pos_2
inst_pnt += 4
if instruction == 3:
init_inst[init_inst[inst_pnt + 1]] = system_id
inst_pnt += 2
elif instruction == 4:
if pos_1 != 0:
return pos_1
#print("Output:", pos_1)
inst_pnt += 2
elif instruction == 5:
inst_pnt = pos_2 if pos_1 != 0 else inst_pnt + 3
elif instruction == 6:
inst_pnt = pos_2 if pos_1 == 0 else inst_pnt + 3
elif instruction == 7:
init_inst[init_inst[inst_pnt + 3]] = 1 if pos_1 < pos_2 else 0
inst_pnt += 4
elif instruction == 8:
init_inst[init_inst[inst_pnt + 3]] = 1 if pos_1 == pos_2 else 0
inst_pnt += 4
return init_inst[0]
program = read_number_from_file("day5", split=",")
print("Part 1:", get_output2(program[:], 1))
program = read_number_from_file("day5", split=",")
print(get_output(program[:], 1)[1:])
#print("Part 2:", get_output2(program[:], 5))
# def solution():
# return get_output(program[:], 1) + get_output(program[:], 5)
#
# #from timeit import timeit
# #print(timeit(solution, number=1000)/1000)