Advent of Code: Day 12

For Day 12, the challenge is to count the number of isolated groups within a graph. My Python solution is below: connections = {} with open("input.txt", "r") as o: for line in o.read().split("\n"): p = line.split(" <-> "…

Advent of Code: Day 11

For Day 11, the challenge is to optimize a path through a hex grid. My Python solution is below: with open("input.txt", "r") as o: directions = o.read().split(",") DIR = { "n": (0, 1, -1), "nw": (-1, 1, 0), "…

Advent of Code: Day 10

For Day 10, the challenge is to hash a string using a "knot-based" hash algorithm. My Python solution is below: from functools import reduce class CircularList(list): def __getitem__(self, x): return super(CircularList, self).__getitem__(x % len(self)) def __setitem__(self, x, v): super(CircularList, self).__setitem_…

Advent of Code: Day 9

For Day 9, the challenge is to parse a stream of text and counting occurences of certain patterns. Heres my Python solution: with open("input.txt", "r") as o: data = o.read() index = 0 current_score = 0 current_depth = 0 garbage_count = 0 def read(): global…

Advent of Code: Day 8

For Day 8, the challenge is to execute some simple instructions to manipulate registers, and then print some basic facts about the state of the registers. My Python solution is below: registers = {} highest_value = -99999 def check_condition(condition): register, command, value = condition.split(" ") value = int(value) if…

Advent of Code: Day 7

For Day 7, the challenge is to build a graph of running programs and dependencies and then adjust the weighting. My Python code is below: import Queue from collections import Counter class Program: def __init__(self, line): self.parent = None line = line.split(" -> ") if len(line)…

Advent of Code: Day 6

For Day 6, the challenge is to redistribute values in an array until we find a loop in the process. My Python 2 solution is below: def apply_redistribution(data): data = list(data) max_index = 0 max_value = data[0] for i in range(len(data)): if data[i] >…

Advent of Code: Day 5

For Day 5, the challenge is to calculate the number of steps needed to bounce around an array following one of two rulesets. My Python 2 solution is below: def simple_increment(i): return i + 1 def part_2_increment(i): return i - 1 if i >= 3 else…

Advent of Code: Day 4

For Day 4, the challenge is to calculate the number of valid passphrases given some rules. My Python 2 solution is below: def is_valid(input, modification=None): hsh = set() for word in input.split(" "): if modification: word = modification(word) if word in hsh: return False hsh.add(…

Advent of Code: Day 3

For Day 3, the challenge is to calculate the Manhattan Distance for a given number given a formula for calculating a spiral of numbers. My initial brute force solution, in Javascript, is shown below: const UP = [0, -1]; const DOWN = [0, 1]; const LEFT = [-1, 0]; const RIGHT = [1, 0]…