How to use get_param_value method in Slash

Best Python code snippet using slash

11-main.py

Source:11-main.py Github

copy

Full Screen

...18 # the third parameter's mode is in the ten-thousands digit,19 mode3 = (op // 10000) % 1020 # and so on. 21 return [mode1, mode2, mode3]22def get_param_value(d, val, mode, rbase):23 if mode == immediate_mode:24 return val25 if mode == position_mode:26 return d[val]27 if mode == relative_mode:28 return d[rbase + val]29 raise 'Bad parameter mode'30def get_write_address(val, mode, rbase):31 if mode == relative_mode:32 return rbase + val33 return val34def rotate_robot(d, how):35 x,y = d36 37 if x == 1:38 if how == 0: # left39 return [0, -1]40 else:41 return [0, 1]42 if x == -1:43 if how == 0: # left44 return [0, 1]45 else:46 return [0, -1]47 if y == 1:48 if how == 0: # left49 return [1, 0]50 else:51 return [-1, 0]52 if y == -1:53 if how == 0: # left54 return [-1, 0]55 else:56 return [1, 0]57def follow_instructions(instructions, pos, direction, points):58 color,turn = instructions59 d = 'left' if turn == 0 else 'right'60 print(f'paint: { color} on {pos}, {len(points)}, turn {d}')61 points[id(pos)] = color62 dx,dy = rotate_robot(direction, turn)63 print(direction, (dx,dy))64 x,y = pos65 66 return [(dx+x, dy+y), (dx,dy)]67 68def id(pos):69 return f'{pos[0]},{pos[1]}'70def run(d, inputs, points):71 72 pos = [0,0]73 direction = [0,-1]74 d += [0]*100000075 rbase = 076 pc = 077 output = []78 79 while pc < len(d):80 opsize = 481 op = d[pc]82 # print(op)83 [mode1, mode2, mode3] = get_parameter_modes(op)84 op = op % 10085 86 if op == 99: # halt87 opsize = 1 # unnecessary but complete88 print(f'total points painted: {len(points)}')89 return [False, pc, output]90 elif op == 1: # add91 [addr1,addr2,outAddr] = d[pc+1:pc+4]92 # size 493 v1 = get_param_value(d, addr1, mode1, rbase)94 v2 = get_param_value(d, addr2, mode2, rbase)95 # print(d[pc:pc+4])96 # print(f'add: {v1} + {v2} = {v1+v2} -> [{outAddr}]')97 outAddr = get_write_address(outAddr, mode3, rbase)98 d[outAddr] = v1 + v299 elif op == 2: # mul100 [addr1,addr2,outAddr] = d[pc+1:pc+4]101 # size 4102 v1 = get_param_value(d, addr1, mode1, rbase)103 v2 = get_param_value(d, addr2, mode2, rbase)104 105 outAddr = get_write_address(outAddr, mode3, rbase)106 d[outAddr] = v1 * v2107 elif op == 3: # input108 addr1 = d[pc+1]109 opsize = 2 110 111 # #hand off when unable to read anymore112 # if len(inputs) == 0:113 # raise 'got an input!'114 addr1 = get_write_address(addr1, mode1, rbase)115 val = points.get(id(pos), 0)116 d[addr1] = val117 118 elif op == 4: # output119 addr1 = d[pc+1]120 v1 = get_param_value(d, addr1, mode1, rbase)121 opsize = 2 122 val = v1123 124 # output val somewhere125 output.append( val)126 if len(output) == 2:127 [p,di] = follow_instructions(output, pos, direction, points)128 pos = p129 direction = di130 output = []131 132 133 elif op == 5: # jump-if-true134 [addr1,addr2] = d[pc+1:pc+3]135 v1 = get_param_value(d, addr1, mode1, rbase)136 v2 = get_param_value(d, addr2, mode2, rbase)137 opsize = 3138 #if the first parameter is non-zero139 if v1 != 0:140 #set the instruction pointer to the value from the second parameter141 pc = v2142 continue143 elif op == 6: # jump-if-false144 [addr1,addr2] = d[pc+1:pc+3]145 v1 = get_param_value(d, addr1, mode1, rbase)146 v2 = get_param_value(d, addr2, mode2, rbase)147 opsize = 3148 #if the first parameter is zero149 if v1 == 0:150 #set the instruction pointer to the value from the second parameter151 pc = v2152 continue153 elif op == 7: # less than154 [addr1,addr2,outAddr] = d[pc+1:pc+4]155 v1 = get_param_value(d, addr1, mode1, rbase)156 v2 = get_param_value(d, addr2, mode2, rbase)157 opsize = 4158 159 outAddr = get_write_address(outAddr, mode3, rbase)160 d[outAddr] = 1 if v1 < v2 else 0161 elif op == 8: # equals162 [addr1,addr2,outAddr] = d[pc+1:pc+4]163 v1 = get_param_value(d, addr1, mode1, rbase)164 v2 = get_param_value(d, addr2, mode2, rbase)165 opsize = 4166 outAddr = get_write_address(outAddr, mode3, rbase)167 d[outAddr] = 1 if v1 == v2 else 0168 elif op == 9: # change relative base169 addr1 = d[pc+1]170 v1 = get_param_value(d, addr1, mode1, rbase)171 opsize = 2172 173 rbase += v1174 else:175 print(f'unknown op: {op} at pc: {pc}')176 return [False,-1,output]177 178 pc += opsize179 180 return [False,-1,output]181 #part2182def part1():183 run(data, [], {})184def part2():...

Full Screen

Full Screen

17-main.py

Source:17-main.py Github

copy

Full Screen

...18 # the third parameter's mode is in the ten-thousands digit,19 mode3 = (op // 10000) % 1020 # and so on. 21 return [mode1, mode2, mode3]22def get_param_value(d, val, mode, rbase):23 if mode == immediate_mode:24 return val25 if mode == position_mode:26 return d[val]27 if mode == relative_mode:28 return d[rbase + val]29 raise 'Bad parameter mode'30def get_write_address(val, mode, rbase):31 if mode == relative_mode:32 return rbase + val33 return val34def run(d, inputs, part1):35 d += [0]*1000036 rbase = 037 pc = 038 output = ''39 if not part1:40 d[0] = 241 while pc < len(d):42 43 opsize = 444 op = d[pc]45 [mode1, mode2, mode3] = get_parameter_modes(op)46 op = op % 10047 if op == 99: # halt48 opsize = 1 # unnecessary but complete49 50 return output51 elif op == 1: # add52 [addr1,addr2,outAddr] = d[pc+1:pc+4]53 # size 454 v1 = get_param_value(d, addr1, mode1, rbase)55 v2 = get_param_value(d, addr2, mode2, rbase)56 outAddr = get_write_address(outAddr, mode3, rbase)57 d[outAddr] = v1 + v258 elif op == 2: # mul59 [addr1,addr2,outAddr] = d[pc+1:pc+4]60 # size 461 v1 = get_param_value(d, addr1, mode1, rbase)62 v2 = get_param_value(d, addr2, mode2, rbase)63 64 65 outAddr = get_write_address(outAddr, mode3, rbase)66 d[outAddr] = v1 * v267 elif op == 3: # input68 addr1 = d[pc+1]69 opsize = 2 70 v = 071 addr1 = get_write_address(addr1, mode1, rbase)72 if not part1:73 if len(inputs[0]) == 0:74 inputs.pop(0)75 v = inputs[0].pop(0)76 d[addr1] = v77 elif op == 4: # output78 addr1 = d[pc+1]79 v1 = get_param_value(d, addr1, mode1, rbase)80 opsize = 2 81 val = v182 83 # output val somewhere84 if part1:85 output += chr(val)86 else:87 if val < 255:88 output += chr(val)89 else:90 output = val91 92 elif op == 5: # jump-if-true93 [addr1,addr2] = d[pc+1:pc+3]94 v1 = get_param_value(d, addr1, mode1, rbase)95 v2 = get_param_value(d, addr2, mode2, rbase)96 opsize = 397 #if the first parameter is non-zero98 if v1 != 0:99 #set the instruction pointer to the value from the second parameter100 pc = v2101 continue102 elif op == 6: # jump-if-false103 [addr1,addr2] = d[pc+1:pc+3]104 v1 = get_param_value(d, addr1, mode1, rbase)105 v2 = get_param_value(d, addr2, mode2, rbase)106 opsize = 3107 #if the first parameter is zero108 if v1 == 0:109 #set the instruction pointer to the value from the second parameter110 pc = v2111 continue112 elif op == 7: # less than113 [addr1,addr2,outAddr] = d[pc+1:pc+4]114 v1 = get_param_value(d, addr1, mode1, rbase)115 v2 = get_param_value(d, addr2, mode2, rbase)116 opsize = 4117 118 outAddr = get_write_address(outAddr, mode3, rbase)119 d[outAddr] = 1 if v1 < v2 else 0120 elif op == 8: # equals121 [addr1,addr2,outAddr] = d[pc+1:pc+4]122 v1 = get_param_value(d, addr1, mode1, rbase)123 v2 = get_param_value(d, addr2, mode2, rbase)124 opsize = 4125 outAddr = get_write_address(outAddr, mode3, rbase)126 d[outAddr] = 1 if v1 == v2 else 0127 elif op == 9: # change relative base128 addr1 = d[pc+1]129 v1 = get_param_value(d, addr1, mode1, rbase)130 opsize = 2131 132 rbase += v1133 else:134 print(f'unknown op: {op} at pc: {pc}')135 return 'unknown op'136 137 pc += opsize138 139 return 'abnormal termination'140def part1(d):141 cv = [list(x) for x in run(d, [], part1=True).strip().splitlines()]142 h = len(cv)143 w = len(cv[0])...

Full Screen

Full Screen

09-main.py

Source:09-main.py Github

copy

Full Screen

...18 # the third parameter's mode is in the ten-thousands digit,19 mode3 = (op // 10000) % 1020 # and so on. 21 return [mode1, mode2, mode3]22def get_param_value(d, val, mode, rbase):23 if mode == immediate_mode:24 return val25 if mode == position_mode:26 return d[val]27 if mode == relative_mode:28 return d[rbase + val]29 raise 'Bad parameter mode'30def get_write_address(val, mode, rbase):31 if mode == relative_mode:32 return rbase + val33 return val34def run(d, inputs):35 d += [0]*100000036 rbase = 037 pc = 038 output = []39 40 while pc < len(d):41 opsize = 442 op = d[pc]43 [mode1, mode2, mode3] = get_parameter_modes(op)44 op = op % 10045 # print(f'---\nop: {op}')46 if op == 99: # halt47 opsize = 1 # unnecessary but complete48 # print(output)49 return [False, pc, output]50 elif op == 1: # add51 [addr1,addr2,outAddr] = d[pc+1:pc+4]52 # size 453 v1 = get_param_value(d, addr1, mode1, rbase)54 v2 = get_param_value(d, addr2, mode2, rbase)55 # print(d[pc:pc+4])56 # print(f'add: {v1} + {v2} = {v1+v2} -> [{outAddr}]')57 outAddr = get_write_address(outAddr, mode3, rbase)58 d[outAddr] = v1 + v259 elif op == 2: # mul60 [addr1,addr2,outAddr] = d[pc+1:pc+4]61 # size 462 v1 = get_param_value(d, addr1, mode1, rbase)63 v2 = get_param_value(d, addr2, mode2, rbase)64 # print(d[pc:pc+4])65 # print(f'mul: {v1} * {v2} = {v1*v2} -> [{outAddr}]')66 outAddr = get_write_address(outAddr, mode3, rbase)67 d[outAddr] = v1 * v268 elif op == 3: # input69 addr1 = d[pc+1]70 opsize = 2 71 72 # print(f'store: 1 -> [{addr1}]')73 #hand off when unable to read anymore74 if len(inputs) == 0:75 raise 'got an input!'76 # return [True, pc, output]77 78 addr1 = get_write_address(addr1, mode1, rbase)79 v = inputs.pop(0)80 d[addr1] = v81 82 elif op == 4: # output83 addr1 = d[pc+1]84 v1 = get_param_value(d, addr1, mode1, rbase)85 opsize = 2 86 val = v187 if val != 0:88 print(pc)89 # print(d[pc:pc+2])90 # print(f'get: [{addr1}] -> val = {val}')91 # output val somewhere92 output.append( val)93 # print(val)94 # print('='*10)95 96 ## after output hand off to next amp97 98 elif op == 5: # jump-if-true99 [addr1,addr2] = d[pc+1:pc+3]100 v1 = get_param_value(d, addr1, mode1, rbase)101 v2 = get_param_value(d, addr2, mode2, rbase)102 opsize = 3103 #if the first parameter is non-zero104 if v1 != 0:105 #set the instruction pointer to the value from the second parameter106 pc = v2107 continue108 elif op == 6: # jump-if-false109 [addr1,addr2] = d[pc+1:pc+3]110 v1 = get_param_value(d, addr1, mode1, rbase)111 v2 = get_param_value(d, addr2, mode2, rbase)112 opsize = 3113 #if the first parameter is zero114 if v1 == 0:115 #set the instruction pointer to the value from the second parameter116 pc = v2117 continue118 elif op == 7: # less than119 [addr1,addr2,outAddr] = d[pc+1:pc+4]120 v1 = get_param_value(d, addr1, mode1, rbase)121 v2 = get_param_value(d, addr2, mode2, rbase)122 opsize = 4123 124 outAddr = get_write_address(outAddr, mode3, rbase)125 d[outAddr] = 1 if v1 < v2 else 0126 elif op == 8: # equals127 [addr1,addr2,outAddr] = d[pc+1:pc+4]128 v1 = get_param_value(d, addr1, mode1, rbase)129 v2 = get_param_value(d, addr2, mode2, rbase)130 opsize = 4131 outAddr = get_write_address(outAddr, mode3, rbase)132 d[outAddr] = 1 if v1 == v2 else 0133 elif op == 9: # change relative base134 addr1 = d[pc+1]135 v1 = get_param_value(d, addr1, mode1, rbase)136 opsize = 2137 138 rbase += v1139 else:140 print(f'unknown op: {op} at pc: {pc}')141 return [False,-1,output]142 143 pc += opsize144 145 return [False,-1,output]146# data = [ 109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 ] ## makes a copy of itself147# data = [1102,34915192,34915192,7,4,7,99,0] # should output a 16 digit number 148# data = [104,1125899906842624,99] # should output the large number 149# [done,pc,output] = run(data, [1]) # part1...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful