from Helper import read_input input = read_input('InputDay17.txt') class Space: def __init__(self): self.store = set() def load_input(self, lines): z = 0 for y in range(len(lines)): for x in range(len(lines[y])): if lines[y][x] == '#': self.store.add((x, y, z)) def mark_each_neighbour(self, coord, counters): cx, cy, cz = coord for x in [cx - 1, cx, cx + 1]: for y in [cy - 1, cy, cy + 1]: for z in [cz - 1, cz, cz + 1]: neighbour = (x, y, z) if neighbour != coord: if neighbour not in counters: counters[neighbour] = 1 else: counters[neighbour] += 1 def count_active(self): return len(self.store)
from Helper import read_input input = read_input('InputDay9.txt', 'int') def check_slice(slice, find): for i in slice: for j in slice: if i + j == find: return i, j return None def find_inconsistent_number(input, preamble, consider): index = preamble end = len(input) while index < end: slice = input[index - preamble:index] find = input[index] if check_slice(slice, find) is None: return find index += 1 return None print(find_inconsistent_number(input, 25, 25))
from Helper import read_input import parse input = read_input('InputDay2.txt') format = '{}-{} {}: {}' valid = 0 for line in input: pos1, pos2, letter, input = parse.parse(format, line) # Convert to index pos1 = int(pos1) - 1 pos2 = int(pos2) - 1 if input[pos1] == letter or input[pos2] == letter: if input[pos1] == input[pos2]: continue valid += 1 print(valid)
from Helper import read_input program = [] for line in read_input('InputDay8.txt'): instr, arg = line.split(' ') program.append((instr, int(arg))) acc = 0 iptr = 0 passed = set() while iptr < len(program): if iptr in passed: break passed.add(iptr) instr, arg = program[iptr] if 'jmp' == instr: iptr += arg continue elif 'acc' == instr: acc += arg iptr += 1 print(acc)
from Helper import read_input map = read_input('InputDay3.txt') map_width = len(map[0]) def check_position(x, y): real_x = x % map_width if map[y][real_x] == '#': return 1 return 0 def check_slope(right, down): x, y = 0, 0 trees = 0 while True: x += right y += down if y >= len(map): break trees += check_position(x, y) return trees s1 = check_slope(1, 1)
from Helper import read_input input = sorted(read_input('InputDay10.txt', 'int')) input.insert(0, 0) input.append(input[-1] + 3) diffs = {} for i in range(len(input) - 1): diff = input[i + 1] - input[i] if diff not in diffs: diffs[diff] = 1 else: diffs[diff] += 1 print(diffs[1] * diffs[3])
from Helper import read_input input = read_input('InputDay1.txt', cast='int') answer = 0 for outer in input: for inner in input: for moreInner in input: if 2020 == outer + inner + moreInner: answer = outer * inner * moreInner break print(answer)
from Helper import read_input input = read_input('InputDay5.txt') def approach(start, end, lower, upper, line): range = (start, end) for char in line: diff = range[1] - range[0] if char == lower: range = (range[0], range[0] + diff // 2) elif char == upper: range = (range[0] + diff // 2 + 1, range[1]) else: print("Unknown char {} in line".format(char)) return range[0] highest_seat_id = 0 for line in input: row = approach(0, 127, 'F', 'B', line[:7]) column = approach(0, 7, 'L', 'R', line[7:]) seat_id = row * 8 + column if seat_id > highest_seat_id: highest_seat_id = seat_id print(highest_seat_id)