#!/usr/bin/python3 from aoctools import InputReader debugging_output = False # get the input, parse it and (if needed) print the initial state lanternfishes = list(map(int, InputReader.strings("./input/6.txt")[0].split(","))) if debugging_output: print("Initial state: " + str(lanternfishes)) # simulate the 80 days for day in range(0, 80): for i in range(0, len(lanternfishes)): lanternfishes[i] -= 1 if lanternfishes[i] < 0: lanternfishes[i] = 6 lanternfishes.append(8) # if needed: output the current state if debugging_output: print("After " + str(day+1) + " days: " + str(lanternfishes)) # print the result of the simulation print("Total number of lanternfishes: " + str(len(lanternfishes)))
version_sum += v_sum return version_sum, packet_binary[ length_of_subpackets:], calc_value_of_operator( packet_type_id, results) # ELSE: If the length type ID is 1, then the next 11 bits are a number that represents the number # of sub-packets immediately contained by this packet. number_of_subpackets = int(packet_binary[0:11], 2) packet_binary = packet_binary[11:] for _ in range(0, number_of_subpackets): v_sum, packet_binary, res = parse_packet(packet_binary) results.append(res) version_sum += v_sum return version_sum, packet_binary, calc_value_of_operator( packet_type_id, results) # get the input packet_hex = InputReader.strings('./input/16.txt')[0] # convert the input to a binary string packet_bin = '' for p in packet_hex: packet_bin += bin(int(p, 16))[2:].zfill(4) # calculate and print the results print('Part 1: ' + str(parse_packet(packet_bin)[0])) # 957 print('Part 2: ' + str(parse_packet(packet_bin)[2])) # 744953223228
#!/usr/bin/python3 from aoctools import InputReader lines = InputReader.strings("./input/3.txt") gamma = "" epsilon = "" for i in range(0, len(lines[0])): ones = 0 zeros = 0 for line in lines: if (int(line[i])): ones += 1 else: zeros += 1 result_gamma = "1" if ones > zeros else "0" result_epsilon = "0" if ones > zeros else "1" gamma = gamma + result_gamma epsilon = epsilon + result_epsilon print(int(gamma, 2) * int(epsilon, 2))
result = [] for x in list_with_duplicates: if x not in result: result.append(x) return result def print_coordinates(map: list): x_arr: list = ['.'] * (max([x[0] for x in map]) + 1) y_arr = [x_arr.copy() for i in range(0, max([y[1] for y in map]) + 1)] for coordinate in map: y_arr[coordinate[1]][coordinate[0]] = '#' for line in y_arr: print(''.join(l for l in line)) def fold_paper(instructs: list, coords: list, part: Part): for instruction in instructs: middle = int(re.findall('\d+', instruction)[0]) t = 1 if 'y' in instruction else 0 for i in range(len(coords)): if coords[i][t] > middle: coords[i][t] -= (coords[i][t] - middle) * 2 coords = remove_duplicates(coords) # for part 1 only execute the first folding instruction if(part == Part.ONE): break return coords # get the input and parse it input = InputReader.strings('./input/13.txt') coordinates = [list(map(int, i.split(','))) for i in input if re.match('\d+,\d+', i)] fold_intructions = [i for i in input if re.match('fold along (x|y)=\d+', i)] # fold the paper and print the results print("Part 1: " + str(len(fold_paper(fold_intructions, coordinates, Part.ONE)))) # 755 print_coordinates(fold_paper(fold_intructions, coordinates, Part.TWO)) # BLKJRBAG
name = '' def __init__(self, starting_space, name): self.space = starting_space self.name = name def increment_space(self, incr: int): for _ in range(0, incr): self.space += 1 if self.space > 10: self.space = self.space % 10 # get the input player1 = Player( int(InputReader.strings('./input/21.txt')[0].split(' ').pop()), 'player1') player2 = Player( int(InputReader.strings('./input/21.txt')[1].split(' ').pop()), 'player2') die, die_roll_counter = 1, 0 while True: for player in [player1, player2]: sum = 0 # roll the die three times for i in range(0, 3): die_roll_counter += 1 sum += die die += 1 if die > 100: die = 1
#!/usr/bin/python3 from aoctools import InputReader class Digit: ONE = 2 FOUR = 4 SEVEN = 3 EIGHT = 7 # get the input and parse it output_values = [] for line in [ line.split('|')[1] for line in InputReader.strings('./input/8.txt') ]: output_values += [l for l in line.split(' ') if l] # calculate and print the result output = len([ v for v in output_values if len(v) in [Digit.ONE, Digit.FOUR, Digit.SEVEN, Digit.EIGHT] ]) print('The digits 1, 4, 7, or 8 appear ' + str(output) + ' times in the output values.')
#!/usr/bin/python3 from aoctools import InputReader # FYI: This is just my simple, own implementation of the Dijkstra's algorithm for practice. I didn't know the algorithm before. # All steps are explained here: https://en.wikipedia.org/wiki/Dijkstra's_algorithm#Algorithm # get the input and parse it input = [[int(n) for n in i] for i in InputReader.strings('./input/15.txt')] class Node: def __init__(self, cost, y, x): self.x = x self.y = y self.cost = cost self.cost_sum = float('inf') # 1. Mark all nodes unvisited. Create a set of all the unvisited nodes called the unvisited set. nodes = [] for y in range(0, len(input)): for x in range(0, len(input[y])): nodes.append(Node(input[y][x], y, x)) unvisited_set = nodes # 2. Set the initial node as current. Assign to every node a tentative distance value: # set it to zero for our initial node and to infinity for all other nodes. # [-> infinity is set as default in Node class constructor] current_node = nodes[0] current_node.cost_sum, current_node.cost = 0, 0
#!/usr/bin/python3 from aoctools import InputReader import re # get the input input = InputReader.strings('./input/17.txt')[0] # parse the input values = re.findall('target area: x=(-?\d+..-?\d+), y=(-?\d+..-?\d+)', input)[0] x_values = list(map(int, re.findall('(-?\d+)..(-?\d+)', values[0])[0])) x_start, x_end = min(x_values), max(x_values) y_values = list(map(int, re.findall('(-?\d+)..(-?\d+)', values[1])[0])) y_start, y_end = min(y_values), max(y_values) x_target_area_values = [x for x in range(x_start, x_end + 1, 1 if x_start < x_end else -1)] y_target_area_values = [y for y in range(y_start, y_end + 1, 1 if y_start < y_end else -1)] count_part_2 = 0 overall_probe_y_history = [] for count_y in range(y_start, 250): for count_x in range(0, 250): y_velocity = count_y x_velocity = count_x probe_y, probe_x = 0, 0 probe_y_history = [] for step in range(0, 1000): # The probe's x position increases by its x velocity. probe_x += x_velocity
ONE = 2 FOUR = 4 SEVEN = 3 EIGHT = 7 digits = [Digit.ONE, Digit.FOUR, Digit.SEVEN, Digit.EIGHT] switch_digits = {Digit.ONE: 1, Digit.FOUR: 4, Digit.SEVEN: 7, Digit.EIGHT: 8} def count(arr, c): return len([a for a in arr if c in a]) sum = 0 for line in InputReader.strings('./input/8.txt'): output_values = [ ''.join(sorted(v)) for v in line.split('|')[1].split(' ') if len(v) ] input_signal_patterns = [ ''.join(sorted(v)) for v in line.split('|')[0].split(' ') if len(v) ] signal_patterns = ['', '', '', '', '', '', '', '', 'abcdefg', ''] # A # ------- # B | | C # | D | # ------- # E | | F # | |
# removes stuff like (), {}, [], <> def remove_empty_chunks(lines: list) -> list: for i in range(0, len(lines)): for pair in ["()", "{}", "[]", "<>"]: lines[i] = lines[i].replace(pair, "") if len([ l for l in lines if len([x for x in ["()", "{}", "[]", "<>"] if x in l]) > 0 ]) > 0: lines = remove_empty_chunks(lines) return lines # get the input and parse it input = remove_empty_chunks(InputReader.strings('./input/10.txt')) # calculate the score for part 1 score_p1 = 0 scores_p1 = [3, 57, 1197, 25137] corrupt_line_idxs = [] for line in input: for i in range(0, len(line)): if line[i] in close_characters: score_p1 += scores_p1[close_characters.index(line[i])] corrupt_line_idxs.append(input.index(line)) break # filter out the corrupt lines -> the remaining ones are the incomplete lines input = [l for l in input if input.index(l) not in corrupt_line_idxs]