def process_data(subdir): linelist = load_input(subdir, DAY) linelist.sort() dic = {} key = "" for line in linelist: a = line.split() year, month, day = a[0][1:].split("-") hour, minute = list(map(int, a[1][:-1].split(":"))) if hour == 23: day = str(int(day) + 1).zfill(2) minute = minute - 60 if a[2:][0] == "Guard": key = f"{month}-{day}#{a[2:][1][1:]}" # hour = 0 # minute = 0 lst = [] lst.append(minute) dic[key] = lst return dic
def main(): numbers = list(map(int, load_input("day09_input.txt"))) print() result_1 = part_1(numbers, 25) print(f"\nPart 1: {result_1}\n") result_2 = part_2(numbers, result_1) print(f"\nPart 2: {result_2}")
def main(): depthlist = aoc_helper.load_input("day01_input.txt") depthlist = [int(x) for x in depthlist] print("Part 1: {}".format(count_increased(depthlist))) new_depthlist = combine_data(depthlist, 3) print("Part 2: {}".format(count_increased(new_depthlist)))
def main(): linelist = aoc_helper.load_input("day04_input.txt", separator="\n\n") numbers = get_numbers(linelist) boards = get_boards(linelist) result_1 = win_bingo(numbers, boards) # result_2 = lose_bingo(numbers, boards) print(f"Part 1: {result_1}")
def main(): data = aoc_helper.load_input("day05_input.txt") data = process_data(data) grid1 = draw_grid(data) grid1 = draw_lines(grid1, data) overlaps1 = count_overlap(grid1) print(f"Part 1: {overlaps1}") grid2 = draw_grid(data) grid2 = draw_lines(grid2, data, 1) overlaps2 = count_overlap(grid2) print(f"Part 2: {overlaps2}")
north += north_waypoint * value east += east_waypoint * value elif action == "R" or action == "L": rot = int(value / 90) if action == "L": rot = -rot rot = rot % 4 if rot == 1: north_waypoint, east_waypoint = -east_waypoint, north_waypoint elif rot == 2: north_waypoint, east_waypoint = -north_waypoint, -east_waypoint elif rot == 3: north_waypoint, east_waypoint = east_waypoint, -north_waypoint if print_log: logging.info(f"Action: {action}, Value: {value}\n") logging.info(f"Current Position: [ {north} | {east} ]") logging.info(f"Current Waypoint: [ {north_waypoint} | " f"{east_waypoint} ]\n\n") return abs(east) + abs(north) subdir = "input" day = 12 linelist = load_input(f"{subdir}/day_{day}.txt") linelist = [[x[0], int(x[1:])] for x in linelist] print(f"Part 1: {part_1(linelist)}") print(f"Part 2: {part_2(linelist)}")
def gen_heightmap(filename): heightmap = aoc_helper.load_input(filename) return [list(map(int, x)) for x in heightmap]
# --- Day 8: Handheld Halting --- from aoc_helper import load_input linelist = load_input("day08_example.txt") def process_data(lst): oplist = [] for i, line in enumerate(lst): op, arg = line.split() arg = int(arg) oplist.append([i, op, arg]) return oplist def run_program(lst): value = 0 i = 0 id_list = [] while True: try: id_, op, arg = lst[i] if id_ not in id_list: if op == "acc": value += arg i += 1 elif op == "jmp": i += arg elif op == "nop": i += 1
# --- Day 10: Adapter Array --- from aoc_helper import load_input # from tqdm import tqdm numbers = list(map(int, load_input("examples/day_10.txt"))) highest = max(numbers) # bar = tqdm(desc="2020 Day 10", total=highest) j = 0 ojd = 0 # One Jolt Difference tjd = 0 # Three Jolt Difference while j < highest: match = sorted([x for x in numbers if x >= j + 1 and x <= j + 3])[0] diff = match - j if diff == 1: ojd += 1 if diff == 3: tjd += 1 # bar.update(diff) j += diff tjd += 1 print(f"Part 1: {ojd * tjd} = {ojd} * {tjd}")
most = False else: raise ValueError(f"Unsupported type {typ}.") tmp_lst = [] for i in range(len(lst[0])): if i > 0: lst = tmp_lst[:] tmp_lst = [] gamma = calc_gamma(lst, reverse, most) for x in lst: if x[i] == gamma[i]: tmp_lst.append(x) return tmp_lst[0] linelist = aoc_helper.load_input("day03_input.txt") gamma = calc_gamma(linelist, True, True) epsilon = bitwise_complement(gamma) print(f"Part 1: {mult_binary(gamma, epsilon)}") oxygen = calc_rating(linelist, "oxy") cotwo = calc_rating(linelist, "co2") print(f"Part 2: {mult_binary(oxygen, cotwo)}")
[day_list.append(1) for i in range(rest)] return day_list DAYS = 80 FILENAME = "example" EXT = ".txt" day_list = splitup_days(DAYS) # day_list = [40] read_file = "day06_" + FILENAME + EXT for i, day in enumerate(day_list): ages = aoc_helper.load_input(read_file, separator=",") ages = [int(x) for x in ages] text = ",".join([str(x) for x in lanternfish(ages, day)]) write_file = "_".join([FILENAME, str(DAYS) + "days", "batch" + str(i)]) write_file += EXT print(write_file) with open(write_file, "w") as f: f.write(text) read_file = write_file print(len(ages))
# --- Day 2: Corruption Checksum --- from aoc_helper import load_input line_list = load_input("input", 2) checksum_part_one = 0 checksum_part_two = 0 for line in line_list: vals = [int(val) for val in line.split()] vals.sort() checksum_part_one += vals[-1] - vals[0] for i in vals: for j in vals: if i != j and i % j == 0: checksum_part_two += int(i / j) break print(checksum_part_one) print(checksum_part_two)
def convert_to_string(lst): s = "" for row in lst: s += "".join(row) return s def stablilize_chaos(lst): i = 0 while True: if i % 2 == 0: new_lst = occupy_seats(lst) else: new_lst = empty_seats(lst) string = convert_to_string(lst) new_string = convert_to_string(new_lst) if string == new_string: return string.count("#") else: lst = copy.deepcopy(new_lst) i += 1 dir_ = "input" layout = load_input(f"{dir_}/day_11.txt") layout = [list(x) for x in layout] print(stablilize_chaos(layout))
# --- Day 1: Report Repair --- from aoc_helper import load_input def find_two(lst): for a in lst: for b in lst: if a != b and a + b == 2020: return a * b def find_three(lst): for a in lst: for b in lst: for c in lst: if a != b and b != c and c != a and a + b + c == 2020: return a * b * c linelist = load_input("day01_input.txt") linelist = list(map(int, linelist)) print(find_two(linelist)) print(find_three(linelist))
# --- Day 7: Recursive Circus --- from aoc_helper import load_input linelist = load_input("example", 7) print(linelist)
# --- Day 4: High-Entropy Passphrases --- from aoc_helper import load_input passphrase_list = load_input("input", 4) passphrase_list = [x.split() for x in passphrase_list] def count_valid(lst, include_anagrams=0): valid = 0 for x in lst: if include_anagrams: x = ["".join(sorted(y)) for y in x] if len(x) == len(set(x)): valid += 1 return valid result1 = count_valid(passphrase_list) result2 = count_valid(passphrase_list, include_anagrams=1) print(f"Part 1: {result1}") print(f"Part 2: {result2}")
# --- Day 5: A Maze of Twisty Trampolines, All Alike --- from aoc_helper import load_input linelist = load_input("input", 5) linelist = list(map(int, linelist)) def find_exit(list_, strange_offset=False): lst = list_.copy() idx = 0 i = 0 while True: try: new_idx = idx + lst[idx] except: break if strange_offset: if lst[idx] >= 3: lst[idx] -= 1 else: lst[idx] += 1 else: lst[idx] += 1 idx = new_idx i += 1 return i result1 = find_exit(linelist) result2 = find_exit(linelist, strange_offset=True) print(f"Part 1: {result1}") print(f"Part 2: {result2}")
x, y = pos xmax = len(lst[0]) ymax = len(lst) neighbours = [[x, y + 1], [x + 1, y + 1], [x - 1, y], [x + 1, y], [x - 1, y - 1], [x, y - 1], [x + 1, y - 1], [x - 1, y + 1]] for n in list(neighbours): if n[0] == -1 or n[0] == xmax: neighbours.remove(n) elif n[1] == -1 or n[1] == ymax: neighbours.remove(n) return neighbours linelist = load_input("day11_example2.txt") linelist = [list(map(int, list(x))) for x in linelist] for i in range(1): for y, row in enumerate(linelist): for x, val in enumerate(row): val += 1 while any(z > 9 in z for z in linelist): print("nine") # if val > 9: val = 0 print(val) linelist[y][x] = val # print()
# --- Day 12: Passage Pathing --- from aoc_helper import load_input import re def find_next(path, lst): paths = [] last = path[-1] firsts = [x[0] for x in lst] for i in range(firsts.count(last)): for x in lst: if x[0] == path[-1]: path.append(x[1]) paths.append(path) return paths linelist = load_input("day12_example1.txt") linelist = [x.split("-") for x in linelist] a = [x[0] for x in linelist] b = [x[1] for x in linelist] path = ["start", "A"] path = find_next(path, linelist) path = find_next(path, linelist)
minpos = min(data) maxpos = max(data) minfuel = float("inf") for i in range(minpos, maxpos + 1): fuel = 0 for pos in data: dist = abs(pos - i) if constant_fuel: fuel += dist else: for j in range(1, dist + 1): fuel += j if fuel < minfuel: minfuel = fuel return minfuel data = aoc_helper.load_input("day07_input.txt", separator=",") data = [int(x) for x in data] result1 = align_submarines(data) result2 = align_submarines(data, constant_fuel=False) print(f"Part 1: {result1}") print(f"Part 2: {result2}")
if not use_aim: if d == "up": depth -= x elif d == "down": depth += x else: if d == "forward": depth += aim * x elif d == "up": aim -= x elif d == "down": aim += x return horizontal * depth raw_cmd_list = aoc_helper.load_input("day02_input.txt") cmd_list = [] for L in raw_cmd_list: direction, amount = L.split(" ") amount = int(amount) cmd_list.append([direction, amount]) part1 = drive(cmd_list) part2 = drive(cmd_list, 1) print(f"Part 1: {part1}") print(f"Part 2: {part2}")