Arithmatic operations broadcast unlike tuples
>>> complex(1,1) + complex(2,-2)
(3-1j)
Use index/find on list or string
' BXCYAZAXBYCZCXAYBZ'.index(x[0]+x[2])
[1j,1,-1j,-1].index(dir)
'=-012'.find(s[-1])
https://www.reddit.com/r/adventofcode/comments/zac2v2/comment/iylda9n/ https://www.reddit.com/r/adventofcode/comments/zsct8w/comment/j17k7nn/ https://www.reddit.com/r/adventofcode/comments/zur1an/comment/j1l08w6/
>>> concat = lambda xs: ''.join(xs) # :: [Char] -> String
>>> xs = zip(*('aA0', 'bB1', 'cC2', 'dD3', 'eE4', 'fF5', 'gG6', 'hH7', 'iI8', 'jJ9'))
>>> *map(concat, xs),
('abcdefghij', 'ABCDEFGHIJ', '0123456789')
>>> xs = zip(*('abcdefghij', 'ABCDEFGHIJ', '0123456789'))
>>> *map(concat, xs),
('aA0', 'bB1', 'cC2', 'dD3', 'eE4', 'fF5', 'gG6', 'hH7', 'iI8', 'jJ9')
Copy an iterator [iter(x)] * n
chunk = lambda x, n: zip(*[iter(x)] * n)
https://www.reddit.com/r/adventofcode/comments/zb865p/comment/izixghj/
lambda x, *y: print(type(y) is tuple)
fn = lambda a, b, c, d: ...
fn(*iterable)
map(int, re.findall(r'\d+', line))
https://www.reddit.com/r/adventofcode/comments/zc0zta/comment/iyug6hl/
>>> [*open('in.txt')]
['line 1\n', 'line 2\n', 'line 3\n']
flat_map = lambda f, xs: (y for ys in xs for y in f(ys))
rotate = lambda xs, i: xs[i%len(xs):]+xs[i%len(xs):]
Take first element satisfying constraint
next(i+n for i in range(len(x)) if len(set(x[i:i+n]))==n)
https://www.reddit.com/r/adventofcode/comments/zdw0u6/comment/iz3q1hl/
# point is an (x, y) tuple
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
from collections import defaultdict
>>> {}.get("a", 1)
1
>>> "abc"[::-1]
'cba'
>>> [*map(int, "123")]
[1, 2, 3]
rot90 = lambda A: [*map(list, zip(*A[::-1]))]
https://www.reddit.com/r/adventofcode/comments/zfpnka/comment/izj36kj/
fp helpers for if all true (if any true) conditionals.
>>> x in (1,2,3)
True
starship = lambda x: x > 0 - x < 0
https://www.reddit.com/r/adventofcode/comments/zgnice/comment/izhzxb6/
Note the element type of xs
(i.e. complex
) does not need to be the same as start (i.e. int
)
accumulate([start]+[*xs], lambda acc, el: abs(el)+acc)
reduce(lambda acc, el: abs(el)+acc, [start]+[*xs])
from math import prod
enumerate(xs, 1)
divisor, remainder = divmod(x)
https://www.reddit.com/r/adventofcode/comments/zjnruc/comment/izw63we/
https://www.reddit.com/r/adventofcode/comments/zjnruc/comment/izw63we/
match l, r:
case int(), int():
case int(), list():
case list(), int():
case list(), list():
turns cmp function into key for use with sorted()
, min()
, max()
, heapq.nlargest()
, heapq.nsmallest()
, itertools.groupby()
https://www.reddit.com/r/adventofcode/comments/zkmyh4/comment/j00qay8/
>>> for i in range(4):
... print(i)
... else: print(-1)
...
0
1
2
3
-1
>>> x = ["apple", "banana", "cherry"]
>>> del x[0]
>>> x
['banana', 'cherry']
f :: (int -> bool) -> int
print(f(lambda pos: pos.imag > floor),
f(lambda pos: pos == 500) + 1)
https://www.reddit.com/r/adventofcode/comments/zli1rd/comment/j061f6z/
def f(check, path=[500], rock=len(blocked)):
>>> *((a,b) for a in range(3) for b in range(3)),
((0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2))
Big loss for python3
dist = lambda x,y,p,q: abs(x-p) + abs(y-q)
dist(*(1,2), *(3,4))
def search(t, u='AA', vs=frozenset(F), e=False):
return max([F[v] * (t-D[u,v]-1) + search(t-D[u,v]-1, v, vs-{v}, e)
for v in vs if D[u,v]<t] + [search(26, vs=vs) if e else 0])
https://www.reddit.com/r/adventofcode/comments/zn6k1l/comment/j0fti6c/
lambda x, y: (complex(x-y, y-x),
complex(x*y, x/y))
Have to wait for equilibrium before starting caching keys https://www.reddit.com/r/adventofcode/comments/znykq2/comment/j0kdnnj/
https://www.reddit.com/r/adventofcode/comments/zoqhvy/comment/j0oul0u/
- don't produce (i.e. make robots) more than we can possible spend in one turn
- cull early branches that cant beat best branch even if a geode bot is purchased ever remaining turn (Gauss). https://www.reddit.com/r/adventofcode/comments/zpihwi/comment/j0tls7a/
>>> [1,2] * 5
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
>>> "ab" * 5
'ababababab'
e+d+d*x in elves for x in (0,1j,-1j)
https://www.reddit.com/r/adventofcode/comments/zt6xz5/comment/j1d7xpk/
>>> import collections
>>> collections.Counter(['a','b','c','a'])
Counter({'a': 2, 'b': 1, 'c': 1})
aka Counter.update
>>> hist = Counter('aab')
>>> hist += {'a':2}
>>> hist
Counter({'a': 4, 'b': 1})
a, *_ ,b = sorted(e.real for e in elves)
*a, b = s
a, *b = s
eval(open('in.txt').read().replace('\n\n', ',').replace('\n', '+'))``
https://www.reddit.com/r/adventofcode/comments/z9ezjb/2022_day_1_solutions/iygjmei/
>>> print(b := True)
True
>>> type(b)
<class 'bool'>
>>> b = True
>>> 'a'[(not b):]
'a'
>>> b = False
>>> 'a'[(not b):]
''
>>> x=1
>>> 'ab'[x%2==0]
'a'
>>> x=2
>>> 'ab'[x%2==0]
'b'