def test_part_two_spiral(self): """Test building the part 2 spiral""" # 1. Create default Spiral object myspiral = spiral.Spiral(part2=True) # 2. Make sure it has the default values self.assertEqual(myspiral.part2, True) # 3. Build the spiral result = myspiral.build_until_gt(806, verbose=False) # 4. Validate the spiral self.assertEqual(result, 880) self.assertEqual(myspiral.spiral[(0, 0)], 1) self.assertEqual(myspiral.spiral[(1, 0)], 1) self.assertEqual(myspiral.spiral[(1, 1)], 2) self.assertEqual(myspiral.spiral[(0, 1)], 4) self.assertEqual(myspiral.spiral[(-1, 1)], 5) self.assertEqual(myspiral.spiral[(-1, 0)], 10) self.assertEqual(myspiral.spiral[(-1, -1)], 11) self.assertEqual(myspiral.spiral[(-1, -2)], 747) self.assertEqual(myspiral.spiral[(0, -2)], 806) self.assertEqual(myspiral.spiral[(1, -2)], 880) self.assertTrue((2, -2) not in myspiral.spiral)
def test_ring_offsets(self): """Test ring and side offsets""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) self.assertEqual(myspiral.spiral, {}) # 3. Check ring_offset() self.assertEqual(myspiral.ring_offset(0), None) self.assertEqual(myspiral.ring_offset(1), 0) self.assertEqual(myspiral.ring_offset(5), 4) self.assertEqual(myspiral.ring_offset(10), 1) self.assertEqual(myspiral.ring_offset(15), 6) self.assertEqual(myspiral.ring_offset(20), 11) self.assertEqual(myspiral.ring_offset(25), 0) self.assertEqual(myspiral.ring_offset(30), 5) # 3. Check side_offset() self.assertEqual(myspiral.side_offset(0), None) self.assertEqual(myspiral.side_offset(1), 0) self.assertEqual(myspiral.side_offset(5), 0) self.assertEqual(myspiral.side_offset(10), 1) self.assertEqual(myspiral.side_offset(15), 2) self.assertEqual(myspiral.side_offset(20), 3) self.assertEqual(myspiral.side_offset(25), 0) self.assertEqual(myspiral.side_offset(30), 5)
def test_part_one_spiral(self): """Test building the part 1 spiral""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) # 3. Build the spiral result = myspiral.build_until_gt(23, verbose=False) # 4. Validate the spiral self.assertEqual(result, 24) self.assertEqual(myspiral.spiral[(0, 0)], 1) self.assertEqual(myspiral.spiral[(1, 0)], 2) self.assertEqual(myspiral.spiral[(1, 1)], 3) self.assertEqual(myspiral.spiral[(0, 1)], 4) self.assertEqual(myspiral.spiral[(-1, 1)], 5) self.assertEqual(myspiral.spiral[(-1, 0)], 6) self.assertEqual(myspiral.spiral[(-1, -1)], 7) self.assertEqual(myspiral.spiral[(-1, -2)], 22) self.assertEqual(myspiral.spiral[(0, -2)], 23) self.assertEqual(myspiral.spiral[(1, -2)], 24) self.assertTrue((2, -2) not in myspiral.spiral)
def test_ring_max_min(self): """Test highest and lowest number on a ring""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) self.assertEqual(myspiral.spiral, {}) # 3. Check ring_max() self.assertEqual(myspiral.ring_max(0), 1) self.assertEqual(myspiral.ring_max(1), 9) self.assertEqual(myspiral.ring_max(2), 25) self.assertEqual(myspiral.ring_max(3), 49) self.assertEqual(myspiral.ring_max(4), 81) self.assertEqual(myspiral.ring_max(5), 121) # 3. Check ring_min() self.assertEqual(myspiral.ring_min(0), 1) self.assertEqual(myspiral.ring_min(1), 2) self.assertEqual(myspiral.ring_min(2), 10) self.assertEqual(myspiral.ring_min(3), 26) self.assertEqual(myspiral.ring_min(4), 50) self.assertEqual(myspiral.ring_min(5), 82)
def run_spiral(self, timeout, last_step=0, maxsteps=500): """Runs spiral pointing to find the star.""" s = spiral.Spiral(1, 1) x = 0 y = 0 step_alt = 0.08 step_az = 0.08 alt = self.j.getValue(self.telescope, "TEL_", refresh_not_found=True)['alt'] cosa = math.cos(math.radians(alt)) step_az /= cosa print _('Scaling azimuth by factor {0:.10f} to {1:.2f}').format( cosa, step_az) for i in range(maxsteps): a, e = s.get_next_step() x += a y += e if i < last_step: continue print _('step {0} next {1} {2} altaz {3:.3f} {4:.3f}').format( i, x, y, x * step_alt, y * step_az) self.j.setValue(self.telescope, 'AZALOFFS', '{0} {1}'.format(x * step_alt, y * step_az)) if wait_for_key(timeout): return i print _('spiral ends..') return i
def test_empty_init(self): """Test default Spiral creation""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) self.assertEqual(myspiral.spiral, {}) # 3. Check methods self.assertEqual(myspiral.steps(1), 0)
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the spiral solver = spiral.Spiral(part2=True) # 2. Build the spiral until we reach a larger value solution = solver.build_until_gt(int(input_lines[0]), verbose=args.verbose) if solution is None: print("You could not determine the next larger value") else: print("The next value larger is %d" % (solution)) # 3. Return result return solution is not None
def part_one(args, input_lines): "Process part one of the puzzle" # 1. Create the spiral solver = spiral.Spiral() # 2. Generate the number of steps solution = solver.steps(int(input_lines[0]), verbose=args.verbose) if solution is None: print("You could not determine the number steps") else: print("The number of steps is %d" % (solution)) # 3. Return result return solution is not None
def test_part_one_examples(self): """Test Spiral examples""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) # 3. Loop for all of the part 1 examples for example in P1_EXAMPLES: # 4. Check the number of steps from the square to the access port self.assertEqual(myspiral.steps(example[0], verbose=False), example[1])
def test_side_distance(self): """Test distance to horizontal or vertical axis""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) self.assertEqual(myspiral.spiral, {}) # 3. Check ring_offset() self.assertEqual(myspiral.side_distance(0), None) self.assertEqual(myspiral.side_distance(1), 0) self.assertEqual(myspiral.side_distance(5), 1) self.assertEqual(myspiral.side_distance(10), 1) self.assertEqual(myspiral.side_distance(15), 0) self.assertEqual(myspiral.side_distance(20), 1) self.assertEqual(myspiral.side_distance(25), 2) self.assertEqual(myspiral.side_distance(30), 2)
def test_ring_number(self): """Test ring_number determination""" # 1. Create default Spiral object myspiral = spiral.Spiral() # 2. Make sure it has the default values self.assertEqual(myspiral.part2, False) self.assertEqual(myspiral.spiral, {}) # 3. Check ring_number() self.assertEqual(myspiral.ring_number(1), 0) self.assertEqual(myspiral.ring_number(7), 1) self.assertEqual(myspiral.ring_number(10), 2) self.assertEqual(myspiral.ring_number(15), 2) self.assertEqual(myspiral.ring_number(20), 2) self.assertEqual(myspiral.ring_number(25), 2) self.assertEqual(myspiral.ring_number(30), 3) self.assertEqual(myspiral.ring_number(35), 3) self.assertEqual(myspiral.ring_number(40), 3) self.assertEqual(myspiral.ring_number(45), 3) self.assertEqual(myspiral.ring_number(50), 4)
psList.append(str(v[0])) psList.append(str(v[1])) return ",".join(psList) def mcLaunch(cycles, vertexlist): for i in range(cycles): psList= vertexToOneStrList(vertexlist) print(invokeLambda(psList)) def launch(cycles, vertexlist): num_cores = min(1, cpu_count() -1) pool = Pool(num_cores) for p in pool.imap_unordered(invokeLambda, vertexToList(cycles, vertexlist)): print(p,end='',flush=True) if __name__=="__main__": cycles = 1000 steps = 13 g = spiral.Spiral(xzero=1,yzero=1) vertexlist = list(set(g.generate(steps=steps))) #vertexlist = [(0,0),(10,10),(10,0),(0,10),(5,6)] print("https://afexwi4dg8.execute-api.eu-west-3.amazonaws.com/api/polygon/%s"%(vertexToOneStrList(vertexlist))) launch(cycles, vertexlist) print("\n\n------------>\n\n",uniquepolygons,len(uniquepolygons)) #mcLaunch(cycles, vertexlist)
import spiral sp = spiral.Spiral() sp.initialize() print(sp.get_area())