Skip to content

Instantly share code, notes, and snippets.

View haakonvt's full-sized avatar
💢

Håkon V. Treider haakonvt

💢
View GitHub Profile
@haakonvt
haakonvt / aoc2023_day15.py
Created December 15, 2023 11:30
AOC 2023, day 15
import math
from functools import reduce
from itertools import count, repeat
def reindeer(s):
return reduce(lambda i,c: (17*(i+ord(c)))%256, s, 0)
# Solution, part 1:
sum(map(reindeer, inp.split(",")))
@haakonvt
haakonvt / aoc_2023_day14.py
Created December 14, 2023 23:08
AOC 2023, day 14
# -------- #
# Part One #
# -------- #
def find_slices(col):
slices = []
fixed = np.argwhere(col == "#").ravel()
for start, end in zip(fixed+1, fixed[1:]):
if end > start:
slices.append(slice(start, end))
@haakonvt
haakonvt / aoc_2023_day12_p2.py
Created December 13, 2023 12:44
AOC 2023, day 13, p2
import numpy as np
grids = [np.array(list(map(list, grid.splitlines()))) for grid in inp.split("\n\n")]
def is_reflection(arr, col):
a1, a2 = arr[:, :col], np.fliplr(arr[:, col:])
l1, l2 = a1.shape[1], a2.shape[1]
if l1 > l2:
return np.all(a2 == a1[:,-l2:])
else:
@haakonvt
haakonvt / aoc2023_day10.py
Last active December 11, 2023 19:50
AOC 2023, day 11
import numpy as np
from scipy.spatial.distance import cdist
def dist_calc(expansion):
grid = np.array(list(map(list, inp.splitlines())), dtype="U1")
cols = np.nonzero(np.all(grid == ".", axis=0))[0]
rows = np.nonzero(np.all(grid == ".", axis=1))[0]
locs = np.argwhere(grid == "#")
locs[:, 0] += expansion * np.sum([locs[:, 0] > r for r in rows], axis=0)
locs[:, 1] += expansion * np.sum([locs[:, 1] > c for c in cols], axis=0)
@haakonvt
haakonvt / aoc2023_day9.py
Created December 9, 2023 11:12
AOC 2023, day 9
def diff_inplace(arr):
for idx in range(arr.shape[1], 2, -1):
arr[:,:idx-1] = np.diff(arr[:,:idx])
return arr.sum()
p1 = np.array([l.split() for l in inp.splitlines()], dtype=np.int32)
p2 = np.fliplr(p1.copy())
print(diff_inplace(p1), diff_inplace(p2))
@haakonvt
haakonvt / aoc2023_day7.py
Last active December 7, 2023 21:21
AOC 2023, day 7
# Part 1:
from collections import Counter
ranks = []
vals = dict(zip("AKQJT98765432", reversed(range(13))))
for hand, bid in ((h, int(b)) for h, b in (l.split() for l in inp.splitlines())):
match Counter(hand).most_common(2):
case [(c, 5), *_]:
ranks.append((7, *map(vals.get, hand), bid))
@haakonvt
haakonvt / aoc2023_day6.py
Last active December 6, 2023 17:08
AOC 2023, day 6
import re
from math import floor, ceil, prod
from more_itertools import chunked
from sympy import Symbol, S, solve_univariate_inequality
t = Symbol("t", domain=S.Naturals)
def to_range(conditions):
ineq1, ineq2 = conditions.args
if ineq1.lhs == t: # rule: lhs < rhs
@haakonvt
haakonvt / nightmare.py
Last active December 5, 2023 15:53
AOC 2023, day 5, part 2
class Interval:
__slots__ = ("start", "end")
def __init__(self, start, end):
self.start = start
self.end = end
def __add__(self, n):
return Interval(self.start + n, self.end + n)
def __repr__(self):
@haakonvt
haakonvt / cdf_dps_dataset.py
Last active April 4, 2023 15:06
A first draft at creating a `kedro` `DataSet` for the CDF resource type `Datapoints`
from typing import Any, Dict, List, NoReturn, TYPE_CHECKING, Union
from kedro.io.core import AbstractDataSet, DataSetError
from cognite.client import CogniteClient
from cognite.client.config import ClientConfig
from cognite.client.data_classes import ClientCredentials
from cognite.client.credentials import OAuthClientCredentials
from cognite.client.exceptions import CogniteNotFoundError
if TYPE_CHECKING:
from timeit import default_timer as timer
import cognite.client.proto.data_point_list_response_pb2 as ProtobufDataPointList
from cognite.client.data_classes import Datapoints
payload = dict(
start=32760000000-1,
end=1638000000000,
ignoreUnknownIds=False,