def part1(input_file, output_file=None): instructions = util.read_input(input_file) data = range(0, 256) cur_pos = 0 skip_size = 0 data, cur_pos, skip_size = apply_transform(instructions, data, cur_pos, skip_size) print "Product of the first two elements is {prd}".format(prd=(data[0] * data[1]))
def part1(input_file, output_file=None): steps = util.read_input(input_file) pos = [0, 0] for step in steps: pos = take_step(pos, step) print "{steps} steps are needed".format(steps=abs(pos[0]) + abs(pos[1]))
def part1(input_file, output=None): lines = util.read_input(input_file, util.line_tok) valid_phrases = [] for line in lines: if not has_dupe(line): valid_phrases.append(line) print "There are {n} valid phrases".format(n=len(valid_phrases))
def part1(input_file, output_file=None): registers = {} commands = util.read_input(input_file, util.line_tok) for cmd in commands: execute_command(cmd, registers) max_entry = max(registers.iteritems(), key=operator.itemgetter(1)) print "Max value is {val} in {reg}".format(val=max_entry[1], reg=max_entry[0])
def build_firewall(input_file): data = util.read_input(input_file, util.line_tok) fw = {} for line in data: parts = line.split(":") idx = int(parts[0].strip()) depth = int(parts[1].strip()) fw[idx] = depth return fw
def part1(input_file, output_file=None): instructions = to_ints(util.read_input(input_file, util.line_tok)) cur_pos = 0 jump_count = 0 while 0 <= cur_pos < len(instructions): jump = instructions[cur_pos] instructions[cur_pos] += 1 cur_pos += jump jump_count += 1 print "Took {cnt} jumps to exit list".format(cnt=jump_count)
def part1(input_file, output_file=None): pipe_map = util.read_input(input_file, tokenizer) letters = [] pos = (0, pipe_map[0].index('|')) direction = (1, 0) while True: pos, direction, took_step = take_step(pos, direction, pipe_map, letters) if not took_step: break print("Letters encountered: {letters}".format(letters=''.join(letters)))
def part1(input_file, output=None): digits = util.read_input(input_file, util.line_tok)[0] sum = 0 for i in range(0, len(digits)): if i < len(digits) - 1: if digits[i] == digits[i + 1]: sum += int(digits[i]) else: if digits[i] == digits[0]: sum += int(digits[i]) print "Sum: {sum}".format(sum=sum)
def part2(input_file, output_file): translated_program = ["#include <stdio.h>", "int main(int argc, char** argv)", "{", "long a = 1, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;"] lines = util.read_input(input_file, util.line_tok) for num, line in enumerate(lines): translated_program.append(translate_line(num, line, len(lines))) translated_program += ["eop:", 'printf("%ld\\n", h);', "}"] util.write_file(output_file, translated_program) print("Manually optimize, then compile and execute {file} for solution".format(file=output_file))
def find_first_dupe(input_file): next_alloc = [ int(i) for i in util.read_input(input_file, util.rowwise_space_tok)[0] ] seen_states = [] count = 0 while True: next_alloc = get_next_alloc(next_alloc) count += 1 if next_alloc in seen_states: return count, next_alloc seen_states.append(next_alloc)
def part2(input_file, output_file=None): steps = util.read_input(input_file) pos = [0, 0] max_steps = 0 for step in steps: pos = take_step(pos, step) cur_steps = abs(pos[0]) + abs(pos[1]) if cur_steps > max_steps: max_steps = cur_steps print "Max steps away was {steps}".format(steps=max_steps)
def part1(input_file, output_file=None): instructions = util.read_input(input_file, util.line_tok) ptr = 0 process = Process(0) mul_count = 0 while ptr >= 0 and ptr < len(instructions): ins = instructions[ptr] ptr = process.handle_instruction(ins, [])[0] parts = ins.split() if parts[0] == 'mul': mul_count += 1 print("Performed {cnt} mul operations".format(cnt=mul_count))
def part2(input_file, output_file=None): pipe_map = util.read_input(input_file, tokenizer) letters = [] pos = (0, pipe_map[0].index('|')) direction = (1, 0) count = 0 while True: pos, direction, took_step = take_step(pos, direction, pipe_map, letters) if not took_step: break count += 1 print("Took {steps} steps".format(steps=count))
def part2(input_file, output_file=None): moves = util.read_input(input_file) positions = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'] seen = [positions[:]] while True: for move in moves: positions = apply_move(positions, move) if positions in seen: positions = seen[1000000000 % len(seen)] break else: seen.append(positions[:]) print ("Final order is {pos}".format(pos=''.join(positions)))
def part2(input_file, output=None): digits = util.read_input(input_file, util.line_tok)[0] sum = 0 step = len(digits) / 2 for i in range(0, len(digits)): if i + step < len(digits): if digits[i] == digits[i + step]: sum += int(digits[i + step]) else: idx = i + step - len(digits) if digits[i] == digits[idx]: sum += int(digits[idx]) print "Sum: {sum}".format(sum=sum)
def read_map(input_file): infected_nodes = {} lines = util.read_input(input_file, util.line_tok) line_num = 0 for line in lines: col = 0 for c in line.strip(): if c == '#': infected_nodes[(col, line_num)] = "I" col += 1 line_num += 1 return infected_nodes, (col / 2, len(lines) / 2)
def process_data(input_file, iterations): rules = util.read_input(input_file, rule_tok) cur_data = [['.', '#', '.'], ['.', '.', '#'], ['#', '#', '#']] for i in range(iterations): print i dim = 3 if len(cur_data) % 2 == 0: dim = 2 squares = [] for sq in get_squares(cur_data, dim): squares.append(translate(sq, rules)) cur_data = merge_squares(squares) return cur_data
def build_graph(input_file): lines = util.read_input(input_file, util.line_tok) nodes = {} for line in lines: parts = line.split("<->") nid = parts[0].strip() node = nodes.get(nid, Node(nid)) nodes[nid] = node if len(parts) > 1 and len(parts[1].strip()) > 0: for p in parts[1].strip().split(","): xid = p.strip() if len(xid) > 0: node.connect(xid, nodes) neighbor_node = nodes.get(xid, Node(xid)) return nodes
def part2(input_file, output_file=None): registers = {} commands = util.read_input(input_file, util.line_tok) max_entry = None for cmd in commands: execute_command(cmd, registers) if len(registers) > 0: cur_max = max(registers.iteritems(), key=operator.itemgetter(1)) if max_entry is None: max_entry = cur_max elif max_entry[1] < cur_max[1]: max_entry = cur_max print "Max value is {val} in {reg}".format(val=max_entry[1], reg=max_entry[0])
def part2(input_file, output_file=None): stream = util.read_input(input_file, util.line_tok)[0] garbage_chars = 0 in_ignore = False in_garbage = False for char in stream: if char == "!" and not in_ignore: in_ignore = True elif char == "<" and not in_ignore and not in_garbage: in_garbage = True elif char == ">" and not in_ignore: in_garbage = False elif in_garbage and not in_ignore: garbage_chars += 1 elif in_ignore: in_ignore = False print "Total garbage chars {ttl}".format(ttl=garbage_chars)
def init(input_file): data = util.read_input(input_file, util.line_tok) node_map = {} for line in data: node = Node(line) node_map[node.name] = node # now build the tree set_levels(node_map) min_node = None for node in node_map.values(): if min_node is None: min_node = node elif min_node.get_level() > node.get_level(): min_node = node return min_node, node_map
def part2(input_file, output_file=None): raw_input = util.read_input(input_file, util.line_tok)[0] input = [] for c in raw_input: input.append(ord(c)) input += [17, 31, 73, 47, 23] data = range(0, 256) cur_pos = 0 skip_size = 0 for i in range(0, 64): data, cur_pos, skip_size = apply_transform(input, data, cur_pos, skip_size) hash_raw = [] cur_hash = 0 for i in range(0, len(data)): cur_hash = cur_hash ^ data[i] if (i + 1) % 16 == 0: hash_raw.append(cur_hash) cur_hash = 0 hex_hash = [hex(i).replace("0x", "").zfill(2) for i in hash_raw] print "Hash is {hash}".format(hash=''.join(hex_hash))
def part1(input_file, output_file=None): stream = util.read_input(input_file, util.line_tok)[0] group_count = 0 group_level = 0 total_score = 0 in_ignore = False in_garbage = False for char in stream: if char == "{" and not in_ignore and not in_garbage: group_level += 1 elif char == "}" and not in_ignore and not in_garbage: total_score += group_level group_level -= 1 group_count += 1 elif char == "!" and not in_ignore: in_ignore = True elif char == "<" and not in_ignore: in_garbage = True elif char == ">" and not in_ignore: in_garbage = False elif in_ignore: in_ignore = False print "Total score of all groups is {score}".format(score=total_score)
def get_components(input_file): lines = util.read_input(input_file, util.line_tok) components = [] for line in lines: components.append(line.split("/")) return components
def part1(input_file, output_file=None): moves = util.read_input(input_file) positions = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'] for move in moves: positions = apply_move(positions, move) print ("Final order is {pos}".format(pos=''.join(positions)))
def load_particles(input_file): input_lines = util.read_input(input_file, util.line_tok) particles = [] for line in input_lines: particles.append(Particle(line)) return particles