Skip to content

Instantly share code, notes, and snippets.

@r3domfox
r3domfox / day9_tests.py
Created December 9, 2021 15:08
AOC2021 Day 9 (Python)
def read_file(file):
return {(x, y): int(c) for y, line in enumerate(file) for x, c in enumerate(line.strip())}
def adjacent_coords(map, coord):
x, y = coord
return (coord for coord in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)) if coord in map)
def get_adjacent(map, coord):
@r3domfox
r3domfox / day8_tests.py
Created December 9, 2021 13:15
AOC2021 Day 8 (Python)
def parse_line(line):
input_values, output_values = [l.strip() for l in line.split("|")]
input_signals = input_values.split(" ")
output_signals = output_values.split(" ")
return input_signals, output_signals
unique_lengths = {2, 3, 4, 7}
@r3domfox
r3domfox / day7_test.py
Created December 7, 2021 09:46
AOC2021 Day 7 (Python)
def simple_cost_function(l, r):
return abs(l - r)
def increasing_cost_function(l, r):
distance = abs(l - r)
return int(distance * (distance + 1) / 2)
def nearest(positions, cost_function):
@r3domfox
r3domfox / day6_tests.py
Created December 7, 2021 00:05
AOC2021 Day 6 (Python)
def population_after_generations(initial_ages, generations):
populations = [0, 0, 0, 0, 0, 0, 0, 0, 0]
for age in initial_ages:
populations[age] += 1
for _ in range(generations):
zeroes, *populations = populations
populations[6] += zeroes
populations.append(zeroes)
@r3domfox
r3domfox / day5_tests.py
Last active December 5, 2021 10:38
AOC2021 Day 5
import re
def read_line(line):
start_x_str, start_y_str, end_x_str, end_y_str = re.search(r'(\d+),(\d+) -> (\d+),(\d+)', line).groups()
return int(start_x_str), int(start_y_str), int(end_x_str), int(end_y_str)
def test_read_line():
assert(read_line("911,808 -> 324,221")) == (911, 808, 324, 221)
@r3domfox
r3domfox / day4_tests.py
Last active December 4, 2021 23:31
AOC2021 Day 4 (Python)
import re
class BingoBoard(object):
def __init__(self, rows):
self.rows = [set(row) for row in rows]
self.cols = [set(row[i] for row in rows) for i in range(len(rows[0]))]
self.unmarked = set(n for row in rows for n in row)
self.is_bingo = False
fun hexagons() {
val pathStrings = "hexagons.txt".asResource { it.split("\n") }
// Convert the path strings into lists of pairs of (dx, dy)
fun parsePathSegment(input: String, pos: Int): Pair<Int, Pair<Int, Int>> = when(input[pos]) {
'e' -> (pos + 1) to (2 to 0)
'w' -> (pos + 1) to (-2 to 0)
'n' -> when(input[pos + 1]) {
'e' -> (pos + 2) to (1 to 1)
'w' -> (pos + 2) to (-1 to 1)
// Part one - play rounds with mutable Deques.
fun decks() {
val decks = "decks.txt".asResource {
it.split("\n\n").map { deck ->
deck.split("\n").drop(1).map { line -> line.toInt() }
}
}
val playerA = ArrayDeque<Int>().apply { addAll(decks[0]) }
val playerB = ArrayDeque<Int>().apply { addAll(decks[1]) }
@r3domfox
r3domfox / allergens.kt
Created December 21, 2020 09:15
Kotlin solution to AdventOfCode day 21
fun allergens(fileContents: List<String>) {
// Parse out the list, turn it into a list of pairs of sets of ingredients and allergens
val ingredientsToAllergens = fileContents.map { line ->
val (ingredients, allergies) = line.dropLast(1).split("(contains", limit = 2)
val ingredientList = ingredients.split(" ").map(String::trim).filterNot(String::isBlank).toSet()
val allergyList = allergies.split(",").map(String::trim).filterNot(String::isBlank).toSet()
ingredientList to allergyList
}
// Extract the unique allergens and ingredients