Skip to content

Instantly share code, notes, and snippets.

@qubard
qubard / Aoc day 16 pt2.py
Created December 16, 2024 17:27
Aoc day 16 pt 2.py
# READ FILE
FILENAME = "A"
f = open(FILENAME).readlines()
f = [x.strip() for x in f]
G=defaultdict()
r=0
P = None
end = None
@qubard
qubard / Aoc day 14 pt2.py
Last active December 14, 2024 05:28
Aoc Day 14 Pt 2.py
import re
from copy import deepcopy,copy
from collections import defaultdict, deque
#import networkx as nx
def ints(s):
return [int(match) for match in re.findall(r'-?\d+', s)]
def n3(P):
# includes P in a 3x3
@qubard
qubard / Aoc Day 7 Pt 2.py
Created December 7, 2024 06:13
aoc day 7 pt 2.py
import re
from copy import deepcopy
from collections import defaultdict, deque
# READ FILE
FILENAME = "A"
f = open(FILENAME).readlines()
f = [x.strip() for x in f]
ans = 0
@qubard
qubard / fenwick-range.py
Last active January 29, 2024 03:53
Fenwick Tree W/ Range And Point Updates (Python)
class FenwickTreeRangeUpdatePointQuery:
def __init__(self, values):
if values is None:
raise ValueError("Values list cannot be None!")
self.N = len(values)
values[0] = 0
fenwick_tree = values.copy()
self.A = values
@qubard
qubard / aoc 2023 day 22 pt 2.py
Last active December 22, 2023 08:09
aoc 2023 day 22 pt 2.py
from util import ints
from collections import defaultdict
from functools import cmp_to_key
from copy import deepcopy, copy
prob = "22"
f = open(prob, "r")
lines = []
@qubard
qubard / aoc 2023 day 20 part 2.py
Last active December 22, 2023 08:43
aoc 2023 day 20 part 2.py
from util import ints
from collections import defaultdict
from functools import cmp_to_key
from copy import deepcopy, copy
prob = "20"
f = open(prob, "r")
lines = []
intervals = [(200,300),(300,500),(550,600)]
def overlap(A,B):
"""Does the range (start1, end1) overlap with (start2, end2)?"""
start1,end1 = A
start2,end2 = B
return (
start1 <= start2 <= end1 or
start1 <= end2 <= end1 or
start2 <= start1 <= end2 or
@qubard
qubard / interval primer
Last active December 8, 2023 00:31
intervals
interval problems:
if you can solve these you can use them as abstractions to solve virtually any interval problem
with modifications
1) given a bunch of disjoint* intervals, and an interval you have, return the total # of units for the intersection of your interval
with the other intervals, note that these intervals are inclusive on points (DONE)
https://nedbatchelder.com/blog/201310/range_overlap_in_two_compares.html
the only way this works is if they are disjoint, if they are not disjoint merge them to be disjoint
@qubard
qubard / solve.py
Created January 17, 2023 05:21
NWN Group question
import z3
# num trinkets = t
# num girls = x
"""
https://imgur.com/a/i9x75EX
The number of trinkets (T) divides evenly by the number of girls (G) and the number of families, which is G-2 (because there are two pairs of sisters, essentially eliminating two girls from the count). Since T is a multiple of G, let's say that T = k*G, where k is some counting number. The number of trinkets per family (T/[G-2]) is five more than the number of trinkets per girl (T/G). In other words,
T/G + 5 = T/(G-2)
Substituting in T = kG and solving for G gives
@qubard
qubard / aoc2022-problem17.py
Created December 17, 2022 07:53
aoc2022-problem-17
#from aocd import submit
from util import ints, sign, neighbors
from collections import defaultdict
filename = "17"
# proble input is jet pattern
# rocks spawn 3 units above the last rock or the floor if there isnt a highest rock there