import numpy as np import advent_io as io encode_seat = lambda s: 0 if s == 'L' else np.nan encode_line = lambda l: [encode_seat(s) for s in l] room = np.array([encode_line(l) for l in io.read_input('day11.input')]) adjacent_count = 4 def adjacent_seats(row, col, room): max_row = len(room) - 1 max_col = len(room[row]) - 1 return [ room[row - 1, col - 1] if row > 0 and col > 0 else np.nan, room[row - 1, col] if row > 0 else np.nan, room[row - 1, col + 1] if row > 0 and col < max_col else np.nan, room[row, col - 1] if col > 0 else np.nan, room[row, col + 1] if col < max_col else np.nan, room[row + 1, col - 1] if row < max_row and col > 0 else np.nan, room[row + 1, col] if row < max_row else np.nan, room[row + 1, col + 1] if row < max_row and col < max_col else np.nan, ] def process_seat(row_ix, col_ix, room): row = room[row_ix] seat = row[col_ix] #if not seat do nothing # if seat filled and prev 4 or next 4 are filled, unfill # if first seat and next is not filled then fill
l1 = line.replace('.', '').split("bags contain ") outer = l1[0].strip() # print(f"{outer}") inner_parse = lambda match: { "color": match.group("col").strip(), "qty": int(match.group("qty")) } inner_bags = [ inner_parse(match) for match in re.finditer(inner_bag_re, l1[1]) ] return (outer, inner_bags) for line in io.read_input(input_file): outer, inner = parse_input_line(line) G.add_node(outer) for bag in inner: color = bag["color"] qty = bag["qty"] G.add_node(color) G.add_edge(color, outer, weight=qty) # print(f"outer: {outer} inner: {inner}") print(f"nodes {G.number_of_nodes()} edges {G.number_of_edges()}") def x(node, bags=set()):
def process_op(op, cur_op_index, accumulator): op_name = op.get("operation") op_value = op.get("value") print(f"processing {op_name} {op_value}") if op_name == "nop": return (cur_op_index + 1, accumulator) elif op_name == "acc": return (cur_op_index + 1, accumulator + op_value) elif op_name == "jmp": return (cur_op_index + op_value, accumulator) else: raise Exception(f"Invalid Op! {op_name}") ops = [parse_input_line(line) for line in io.read_input(input_file)] cur_op_index = 0 accumulator = 0 ix = 0 while ix < len(ops): op = ops[cur_op_index] prev_accumulator = accumulator cur_op_index, accumulator = process_op(op, cur_op_index, accumulator) if cur_op_index in executed: print( f"loop detected in {ix} iterations at {cur_op_index}. accumulator={prev_accumulator}" ) break
'N': [1, 0], 'E': [0, 1], 'S': [-1, 0], 'W': [0, -1] } degree_shift = { 'N': [E, S, W ], 'E': [ S, W, N ], 'S': [ W, N, E ], 'W': [ N, E, S ] , } # N/S, E/W direction = E position = [0,0] for line in io.read_input('day12.input'): op = line[0] val = int(line[1:]) if op == 'F': position = np.add(position, np.multiply(v[direction], val)) elif op == 'R': x = int(val/90) - 1 direction = degree_shift[direction][int(val/90) - 1] elif op == 'L': direction = list(reversed(degree_shift[direction]))[(int(val/90) - 1)] elif op in v: position = np.add(position, np.multiply(v[op], val)) else: raise Exception(f"Op not supported {op}")
input_file = 'day5.input' total_rows = 128 total_cols = 7 def fetch_row(start, end, code, low_code): half = int((end - start) / 2) next_start, next_end = (start, start + half) if code[0:1] == low_code else (end - half, end) if len(code) > 1: return fetch_row(next_start, next_end, code[1:], low_code) else: return next_start def process_seat_id(line): x1 = fetch_row(0, total_rows, line[:7], "F") y1 = fetch_row(0, total_cols, line[7:], "L") print(f"row {x1} seat {y1} = {x1*8 + y1}") return (x1 * 8) + y1 answer = max([process_seat_id(line) for line in io.read_input(input_file)]) print(answer)