def part_1(data): GRID = [["." for i in range(400)] for j in range(400)] lets = [chr(i) for i in range(ord('a'), ord('z') + 1)] + [chr(i) for i in range(ord('A'), ord('Z') + 1)] li = 0 # array of tuples (letter, x-coord, y-coord) locations = [] for coords in data: x, y = map(int, coords.split(', ')) locations.append((lets[li], x, y)) GRID [y][x] = lets[li] li += 1 lets = lets[:li] li = 0 for i in rl(GRID): for j in rl(GRID[i]): mini = 400 ** 2 s_mini = None closest = None for loc in locations: l, x, y = loc if (d:=man_dist((i, j), (y, x))) <= mini: s_mini = mini mini = d closest = l if s_mini != mini: GRID[i][j] = closest
def get_vel(data): for i in rl(data): vel = [0, 0, 0] for j in rl(data): if i == j: continue for axis in rl(data[i]['pos']): if data[j]['pos'][axis] > data[i]['pos'][axis]: vel[axis] += 1 elif data[j]['pos'][axis] < data[i]['pos'][axis]: vel[axis] -= 1 for v in rl(vel): data[i]['vel'][v] += vel[v] return data
def get_max_area(g, border): areas = dict() for i in rl(g): for j in rl(g[i]): l = g[i][j] if l == '.' or l in border: continue try: areas[l] += 1 except: areas[l] = 1 maxi = 0 for k in areas.keys(): if areas[k] > maxi: maxi = areas[k] return maxi
def part_2(data): GRID = [["." for i in range(800)] for j in range(800)] locations = [] for coords in data: x, y = map(lambda x: int(x) + 200, coords.split(', ')) locations.append((x, y)) cnt = 0 n = 10000 for i in rl(GRID): for j in rl(GRID[i]): if is_under_n((i, j), n, locations): cnt += 1 print(cnt) print('END OF PART2') return
def part_1(data, second): data = [list(i) for i in data] keys = [] cnt = 0 for i in rl(data): for j in rl(data[0]): if data[i][j] not in ['#', '.']: num = int(data[i][j]) keys.append((j, i, num)) elif data[i][j] == '.': cnt += 1 keys.sort(key=lambda x: x[2]) dists = dict() maxi = 0 for key in keys: x, y, k = key dists[k] = (x, y) if k > maxi: maxi = k maxi += 1 dists = [[None for j in range(maxi)] for i in range(maxi)] for i in range(maxi): bfs_res = bfs(copy.deepcopy(data), keys[i], maxi) dists[i] = bfs_res[::] min_d = len(data)**2 for i in it.permutations(list(range(1, maxi))): # HERE perm = [0] + list(i) + ([0] if second else []) curr_d = 0 for j in range(len(perm) - 1): curr_d += dists[perm[j]][perm[j + 1]] if curr_d < min_d: min_d = curr_d print(min_d) print('END OF', 'PART1' if not second else 'PART2') return
def get_energy(data): total = 0 for i in rl(data): kin = 0 pot = 0 for p, v in zip(data[i]['pos'], data[i]['vel']): pot += abs(p) kin += abs(v) # print(f"pot: {pot:10}; kin: {kin:10}; total: {kin+pot:10}") total += kin * pot return total
def part_1(data): positions = [] for d in data: x = int(d[10:10 + 6]) y = int(d[18:18 + 6]) dx = int(d[36:36 + 2]) dy = int(d[40:40 + 2]) positions.append(new_pos([x, y], (dx, dy))) seconds = 0 while True: min_y = 100000 min_x = 100000 max_y = 0 max_x = 0 for i in rl(positions): positions[i]['pos'][0] += positions[i]['vel'][0] if positions[i]['pos'][0] < min_x: min_x = positions[i]['pos'][0] if positions[i]['pos'][0] > max_x: max_x = positions[i]['pos'][0] positions[i]['pos'][1] += positions[i]['vel'][1] if positions[i]['pos'][1] < min_y: min_y = positions[i]['pos'][1] if positions[i]['pos'][1] > max_y: max_y = positions[i]['pos'][1] # print(max_x, max_y, min_x, min_y) seconds += 1 if abs(min_x - max_x) < 100 and abs(min_y - max_y) < 100: r = display(positions, min_x, min_y) if r == 0: print(seconds) break print('END OF PART1') return
def get_bordered(g): border = set() l = len(g) - 1 for i in rl(g): border |= {g[0][i], g[l][i], g[i][0], g[i][l]} return list(border)
def get_pos(data): for i in rl(data): for v in rl(data[i]['vel']): data[i]['pos'][v] += data[i]['vel'][v] return data
print(res) print('END OF PART1') return res def part_2(data): print('END OF PART2') return if __name__ == '__main__': with open('09_input') as f: data = f.read() # data = data.split('\n') # data = list(map(int, data.split())) tests = [[10, 1618], [13, 7999], [17, 1104], [21, 6111], [30, 5807]] sols = [8317, 146373, 2764, 54718, 37305] for i in rl(tests): res = part_1(copy.deepcopy(tests[i])) if res != sols[i]: print("########################################################") print("ERROR") print(f"Solution {i} is not correct!") print("########################################################") exit(1) part_1([439, 71307]) part_2(copy.deepcopy(data))