This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder