def parse_map(input_path): grid = [] lines = read_raw_entries(input_path, strip=False) for line in lines: grid.append(list(line)) return grid
def test_find_most_sleepy_guard(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) shifts = process_shifts(entries) guard, mins_asleep = find_most_sleepy_guard(shifts) self.assertEqual(10, guard) self.assertEqual(50, mins_asleep)
def test_find_most_sleepy_minute(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) shifts = process_shifts(entries) guard, mins_asleep = find_most_sleepy_guard(shifts) min = find_sleepiest_minute(guard, shifts) self.assertEqual(24, min)
def parse_input(input_path): global nanobots entries = read_raw_entries(input_path) p = re.compile(r'pos=<(-?\d+),(-?\d+),(-?\d+)>, r=(\d+)') nanobots = [] for entry in entries: m = p.match(entry) nanobots.append(Nanobot(Point3d(m.group(1), m.group(2), m.group(3)), m.group(4))) return nanobots
def test_build_network_map(self): entries = read_raw_entries('test_input_d19.txt', strip=False) network_map = build_network_map(entries) self.assertEqual(network_map[(0, 0)], ' ') self.assertEqual(network_map[(5, 0)], '|') self.assertEqual(network_map[(5, 5)], '+') self.assertEqual(network_map[(6, 5)], 'B') self.assertEqual(network_map[(14, 5)], '+')
def test_parse_particles(self): entries = read_raw_entries('test_d20.txt') particles = parse_particles(entries) self.assertEqual( particles[0], Particle(Point3d(3, 0, 0), Point3d(2, 0, 0), Point3d(-1, 0, 0))) self.assertEqual( particles[1], Particle(Point3d(4, 0, 0), Point3d(0, 0, 0), Point3d(-2, 0, 0)))
def test_find_aa(self): # Should be 88 locations entries = read_raw_entries('tests/find-accessible-test.txt') grid_map, elves, goblins = parse_map(entries) filled_map = fill_temp_map(elves + goblins, grid_map) aa = find_accessible_areas_from_point(goblins[0].loc + Point(1, 0), filled_map, []) print(len(set(aa))) self.assertEqual(329, len(set(aa)))
def test_target_selection(self): entries = read_raw_entries( path(__file__, 'tests/target_selection/start_map.txt')) grid_map, elves, goblins = parse_map(entries) print_state('Initial', grid_map, elves, goblins) i = 0 for f in sort_fighters(elves, goblins): i += 1 f.move(grid_map, elves + goblins) print_state('0.{}'.format(i), grid_map, elves, goblins)
def test_movement(self): entries = read_raw_entries(path(__file__, 'tests/movement.txt')) grid_map, elves, goblins = parse_map(entries) print_state('Initial', grid_map, elves, goblins) for i in range(5): j = 0 for f in sort_fighters(elves, goblins): j += 1 f.move(grid_map, elves + goblins) print_state('{}.{}'.format(i, j), grid_map, elves, goblins)
def test_sample_positions(self): entries = read_raw_entries('test_d20.txt') particles = parse_particles(entries) for _ in range(3): for p in particles: p.step() self.assertEqual( particles[0], Particle(Point3d(3, 0, 0), Point3d(-1, 0, 0), Point3d(-1, 0, 0))) self.assertEqual( particles[1], Particle(Point3d(-8, 0, 0), Point3d(-6, 0, 0), Point3d(-2, 0, 0)))
def parse_ascii_input(self, input_path): entries = read_raw_entries(input_path) points = [] grid = [] for entry in entries: grid.append(list(entry)) width = len(grid[0]) left = 500 - (width // 2) for y in range(len(grid)): for x in range(len(grid[0])): if grid[y][x] == '#': points.append(Point(left + x, y + 1)) return points
def read_stars(input: str): entries = read_raw_entries(input) stars = [] for entry in entries: # position = < -3, 11 > velocity = < 1, -2 > p = re.compile(r'.*<(.*)>.*<(.*)>.*') matcher = p.match(entry) x, y = matcher.group(1).split(',') x_vel, y_vel = matcher.group(2).split(',') stars.append( Star(x=int(x), y=int(y), x_vel=int(x_vel), y_vel=int(y_vel))) return stars
def solve_15(input_path, elf_strength=3, allow_elves_to_die=True): entries = read_raw_entries(input_path) grid_map, elves, goblins = parse_map(entries) total_elves = len(elves) for elf in elves: elf.attack_strength = elf_strength round = 0 combat = True while combat: print_state(round, grid_map, elves, goblins) for fighter in sort_fighters(elves, goblins): elves = list(filter(lambda e: e.hit_points > 0, elves)) goblins = list(filter(lambda g: g.hit_points > 0, goblins)) if allow_elves_to_die is False and len(elves) != total_elves: combat = False break elif len(elves) == 0 or len(goblins) == 0: combat = False break fighter.move(grid_map, elves + goblins) fighter.attack(elves + goblins) if combat: round += 1 hit_points = 0 elf_count = 0 for f in elves + goblins: hit_points += f.hit_points if f.type == 'E': elf_count += 1 print_state(round, grid_map, elves, goblins) print('Elves: {}/{}. Rounds: {}, HP: {}, Total: {}'.format( elf_count, total_elves, round, hit_points, round * hit_points)) return round * hit_points, len(elves) == total_elves
def test_process_shifts(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) shifts = process_shifts(entries) self.assertEqual(5, len(shifts)) self.assertEqual(10, shifts[0].guard_id) self.assertEqual(datetime.date(1518, 11, 1), shifts[0].date) self.assertEqual(5, shifts[0].sleep_times[0].sleep_start) self.assertEqual(25, shifts[0].sleep_times[0].sleep_until) self.assertEqual(20, shifts[0].sleep_times[0].mins_asleep()) self.assertEqual(30, shifts[0].sleep_times[1].sleep_start) self.assertEqual(55, shifts[0].sleep_times[1].sleep_until) self.assertEqual(25, shifts[0].sleep_times[1].mins_asleep()) self.assertEqual(45, shifts[0].mins_asleep()) self.assertEqual( list(range(5, 25)) + list(range(30, 55)), shifts[0].list_of_mins())
def parse_real_input(input_path): entries = read_raw_entries(input_path) points = [] for entry in entries: parts = entry.split() if 'x' in parts[0]: x = parts[0].split('=')[1].replace(',', '') s, e = parts[1].split('=')[1].split('..') for y in range(int(s), int(e) + 1): p = Point(x, y) points.append(p) else: y = parts[0].split('=')[1].replace(',', '') s, e = parts[1].split('=')[1].split('..') for x in range(int(s), int(e) + 1): p = Point(x, y) points.append(p) return points
def test_sample_part_2(self): entries = read_raw_entries('test_input_d19.txt', strip=False) r = solve_19b(entries) self.assertEqual(r, 38)
def test_sample(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) r = solve_18(entries) self.assertEqual(1147, r)
for direction in [Point(-1, 0), Point(0, -1)]: if (region.loc.x == 0 and direction.x == -1) or (region.loc.y == 0 and direction.y == -1): continue neighbor = cave[region.loc + direction] for tool in allowed_tools: if tool in neighbor.allowed_tools(): g.add_edge(region.nodes[tool], neighbor.nodes[tool], weight=1) path_length = nx.dijkstra_path_length(g, source=cave[Point(0, 0)].nodes['T'], target=cave[target].nodes['T']) return path_length if __name__ == '__main__': entries = read_raw_entries('input.txt') depth = int(entries[0].split()[1]) x, y = entries[1].split()[1].split(',') target = Point(int(x), int(y)) risk = solve_22_part_1(depth, target) print('Risk: {}'.format(risk)) time = solve_22_part_2(depth, target) print('Total distance in minutes: {}'.format(time))
_x = self.read_value(x) self.last_played = _x self.snd_q.append(_x) self.snd_count += 1 def rcv(self, x): self.rcv_wait = True if len(self.rcv_q) > 0: self.registers[x] = self.rcv_q.popleft() self.rcv_wait = False def solve_18(entries): soundcard = Soundcard(entries) return soundcard.process_instructions() def solve_18b(entries): soundcard_pair = SoundcardPair(entries) soundcard_pair.run() return soundcard_pair.card_1.snd_count if __name__ == '__main__': entries = read_raw_entries('input_d18.txt') r = solve_18(entries) print('part 1, last freq: {}'.format(r)) r = solve_18b(entries) print('part 2, card 1 send count: {}'.format(r))
def run_python_deque(self): input = read_raw_entries('input.txt')[0].strip() solve_9_with_deque(input, 100)
def solve_22(entries): grid, start = setup_grid_and_start(entries) virus = Virus(start, grid) for _ in range(10_000): virus.burst() return virus.infection_count def solve_22b(entries): grid, start = setup_grid_and_start(entries) virus = Virus2(start, grid) for _ in range(10_000_000): virus.burst() return virus.infection_count if __name__ == '__main__': entries = read_raw_entries('input_d22.txt') r = solve_22(entries) print('part 1, number of infections: {}'.format(r)) r = solve_22b(entries) print('part 2, number of infections: {}'.format(r))
def test_solve_6a(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) points = convert_points(entries) r = solve_6a(points) self.assertEqual(17, r)
def test_example_1(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) r = solve_12_good(entries) self.assertEqual(325, r)
def test_find_start(self): entries = read_raw_entries('test_input_d19.txt', strip=False) network_map = build_network_map(entries) start = find_start_pos(network_map) self.assertEqual(start, Point(5, 0))
def test_sample(self): entries = read_raw_entries('test_input_d19.txt', strip=False) r = solve_19(entries) self.assertEqual(r, 'ABCDEF')
return max(scores) def solve_9_with_deque(input: str, multiplier: int = 1): player_count, last_marble_value = parse_params(input) last_marble_value *= multiplier scores: List[int] = [] for i in range(0, player_count): scores.append(0) circle = DequeCircle() marble_value = 1 while marble_value <= last_marble_value: score, string_rep = circle.add_marble(marble_value) scores[(marble_value - 1) % player_count] += score marble_value += 1 return max(scores) if __name__ == '__main__': input = read_raw_entries(path(__file__, 'input.txt'))[0].strip() r = solve_9(input, 1) print('9a. Winning score: {}'.format(r)) input = read_raw_entries(path(__file__, 'input.txt'))[0].strip() r = solve_9(input, 100) print('9b. Winning score: {}'.format(r))
key=lambda kv: kv[1]) return sorted_influence_list[len(sorted_influence_list) - 1][1] def solve_6b(points, threshold): grid, max_x, max_y, min_x, min_y = setup_grid(points) total_size = 0 for i in range(min_x, max_x + 1): for j in range(min_y, max_y + 1): if grid[i][j].is_within_threshold(threshold): total_size += 1 return total_size if __name__ == '__main__': entries = read_raw_entries(path(__file__, 'input.txt')) points = convert_points(entries) r = solve_6a(points) print('Largest size: {}'.format(r)) entries = read_raw_entries(path(__file__, 'input.txt')) points = convert_points(entries) r = solve_6b(points, 10000) print('Largest size: {}'.format(r))
def test_solve_7a(self): entries = read_raw_entries(path(__file__, 'test-input.txt')) steps = process_into_steps(entries, 0) r = solve_7a(steps) self.assertEqual('CABDFE', r)
return list( itertools.islice(seen_order, len(seen_order) - repeat_period, len(seen_order)))[offset_in_period] # No repeat yet, append the new data to our tracking lists seen_order.append(sequence_str) seen[sequence_str] = i # Never found a repeating pattern, return the computed value # (this will only happen for small values probably) return ''.join(sequence) if __name__ == '__main__': # Set up list letters = [chr(i) for i in range(ord('a'), ord('p') + 1)] sequence = deque(letters) instructions = read_raw_entries('input_d16.txt')[0].split(',') r = solve_16(sequence, instructions) print('part 1, order: {}'.format(r)) # Reset for part 2 letters = [chr(i) for i in range(ord('a'), ord('p') + 1)] sequence = deque(letters) instructions = read_raw_entries('input_d16.txt')[0].split(',') r = solve_16b(sequence, instructions, 1_000_000_000) print('part 2, order: {}'.format(r))
def run_python_dll(self): input = read_raw_entries('input.txt')[0].strip() solve_9(input, 100)