def solve_day3(file_input_name): wire1, wire2 = file_input(file_input_name) wire1 = wire1.split(',') wire2 = wire2.split(',') edges1 = find_edges(wire1) edges2 = find_edges(wire2) prev1 = [0, 0, 0] prev2 = [0, 0, 0] intersection = [] for point1 in edges1: for point2 in edges2: line1 = (prev1[:2], point1[:2]) line2 = (prev2[:2], point2[:2]) meetup = intersect(line1, line2) if meetup[0]: if meetup[1] != (0, 0): intersection.append( (abs(meetup[1][0]) + abs(meetup[1][1]), meetup[1], prev2[2] + prev1[2] + abs(line1[0][0] - line2[0][0]) + abs(line1[0][1] - line2[0][1]))) prev2 = point2 prev1 = point1 return [ min(intersection, key=lambda x: x[0])[0], min(intersection, key=lambda x: x[2])[2] ]
def solve_day6(input_file_name): graph = nx.Graph() data = file_input(input_file_name) for line in data: temp = line.split(')') graph.add_edge(temp[0], temp[1]) paths = nx.single_source_shortest_path_length(graph, 'COM') sum = 0 # part 1 for key in paths: sum += paths[key] to_santa = nx.shortest_path_length(graph, 'YOU', 'SAN') - 2 # Part 2 return sum, to_santa
def solve_day1(input_file_name): data = file_input(input_file_name, int) part1 = 0 part2 = 0 for mass in data: fuel = int(mass / 3) - 2 part1 += fuel part2 += fuel while fuel > 0: fuel = int(fuel / 3) - 2 if fuel < 0: fuel = 0 part2 += fuel return part1, part2
def solve_day10(input_file_name): data = file_input(input_file_name) asteroids = [] for i in range(len(data)): for j in range(len(data[0])): if data[i][j] == '#': asteroids.append((i, j)) h = [] for asteroid in asteroids: h.append(asteroids_in_range(asteroids, asteroid, data)) part1, station, asteroids = max(h, key=lambda x: x[0]) clockwise = [] for asteroid in asteroids: clockwise.append((list(reversed(asteroid)), to_clockwise(get_angle(station, asteroid)))) clockwise.sort(key=lambda x: x[1]) # return part1, list(list(clockwise[10 -1][0]).__reversed__()), station return part1
from aocutil import file_input l = file_input('day5.txt') highest = 0 ids = [] for line in l: row_max = 127 row_min = 0 col_max = 7 col_min = 0 for char in line: if char == 'F': row_max = row_max - 1 - (row_max - row_min) // 2 if char == 'B': row_min = (row_min + row_max + 1) // 2 if char == 'L': col_max = col_max - 1 - (col_max - col_min) // 2 if char == 'R': col_min = (col_min + col_max + 1) // 2 seat_id = row_max * 8 + col_max ids.append(seat_id) if seat_id > highest: highest = seat_id ids.sort() prev = None
from aocutil import file_input temp = file_input('day6.txt') temp.append('') group = [] groups = [] for line in temp: if line != '': person = set() for char in line: person.add(char) group.append(person) else: groups.append(group.copy()) group.clear() part1 = 0 part2 = 0 letters = 'qwertyuiopasdfghjklzxcvbnm' for group in groups: anyone = set() everyone = {x for x in letters} for person in group: anyone = anyone | person everyone = everyone & person
from aocutil import file_input temp = file_input('day7.txt') rules = [] for i in range(len(temp)): rule = [] temp[i] = temp[i].split(' bags contain ') for j in range(len(temp[i])): temp[i][j] = temp[i][j].split('bag') for k in temp[i][j]: rule.append(k) for k in range(len(rule)): rule[k] = rule[k].replace('bag', '') rule[k] = rule[k].replace(',', '') rule[k] = rule[k].replace(', ', '') rule[k] = rule[k].replace('s.', '') rule[k] = rule[k].replace('s ', '') rule[k] = rule[k].replace('.', '') rule[k] = rule[k].strip() if rule[k] == 'no other': rule[k] = None try: rule.remove('') except ValueError: pass for j in range(1, len(rule)): if rule[1] is not None: rule[j] = (int(rule[j][0]), rule[j][2:])
from aocutil import file_input def checkhex(string): n = '0x' + string try: n = float.fromhex(n) return True except ValueError: return False temp = file_input('day4.txt') temp.append('') string = '' passports = [] for line in temp: if line != '': string += line + ' ' else: data = string.split() entry = dict() for field in data: key, value = field.split(sep=':') entry[key] = value passports.append(entry) string = '' fields = ('byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid') n = [0, 0]
from aocutil import file_input entry = file_input('day1.txt', int) for i in range(len(entry)): for j in range(i + 1): sums = entry[i] + entry[j] if sums == 2020: print('1/2:\n{0} + {1} = 2020\n{0} + {1} = {2}'.format( entry[i], entry[j], entry[i] * entry[j])) print() for i in range(len(entry)): for j in range(i + 1): for k in range(j + 1): sums = (entry[i] + entry[j] + entry[k]) if sums == 2020: print('2/2\n{0} + {1} + {2} = 2020\n{0} * {1} * {2} = {3}'. format(entry[i], entry[j], entry[k], entry[i] * entry[j] * entry[k]))
from aocutil import file_input grid = file_input('day3.txt', matrix=True) slopes = [0, 0, 0, 0, 0] for i in range(len(grid)): mode1 = i % len(grid[0]) mode2 = (i * 3) % len(grid[0]) mode3 = (i * 5) % len(grid[0]) mode4 = (i * 7) % len(grid[0]) mode5 = i % len(grid[0]) if grid[i][mode1] == '#': slopes[0] += 1 if grid[i][mode2] == '#': slopes[1] += 1 if grid[i][mode3] == '#': slopes[2] += 1 if grid[i][mode4] == '#': slopes[3] += 1 if i * 2 < len(grid) and grid[i * 2][mode5] == '#': slopes[4] += 1 print('Part 1 = {}\nPart 2 = {}'.format( slopes[1], slopes[0] * slopes[1] * slopes[2] * slopes[3] * slopes[4]))
try: moons[i][1][1] -= gy // abs(gy) except ZeroDivisionError: moons[i][1][1] += 0 try: moons[i][1][2] -= gz // abs(gz) except ZeroDivisionError: moons[i][1][2] += 0 for i in range(4): # update position moons[i][0][0] += moons[i][1][0] moons[i][0][1] += moons[i][1][1] moons[i][0][2] += moons[i][1][2] if step == 1000 - 1: total_energy = 0 for moon in moons: pot = abs(moon[0][0]) + abs(moon[0][1]) + abs(moon[0][2]) kin = abs(moon[1][0]) + abs(moon[1][1]) + abs(moon[1][2]) total = pot * kin total_energy += total energy_at_1000 = total_energy print(energy_at_1000) return energy_at_1000 step += 1 data = file_input('day12.txt', as_list=False) print(solve_day12(data))
from aocutil import file_input temp = file_input('day2.txt') passwords = [] for line in temp: split = line.split() entry = [None, None, None, None] num = split[0].split(sep='-') entry[1] = int(num[0]) entry[2] = int(num[1]) entry[0] = split[1][0] entry[3] = split[2] passwords.append(entry) n = 0 m = 0 for entry in passwords: char = entry[0] i = entry[1] j = entry[2] password = entry[3] part2 = False if i <= password.count(char) <= j: n += 1 if password[i - 1] == char: part2 = not part2 if password[j - 1] == char: part2 = not part2 if part2: m += 1