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
from itertools import islice, cycle, tee | |
def a(n, dict_class=dict): | |
a, b = tee(cycle(dict_class() for __ in range(n))) | |
for d in islice(a, n): | |
d.update(dict(enumerate(islice(b, n)))) | |
ret = next(b) | |
return ret | |
class IdDict(dict): |
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
# rings.py | |
# module with ring arithmetic and multiplication and powers | |
# Did this a while ago on another machine? And made a github gist | |
from itertools import islice, cycle, tee | |
import random | |
from typing import Any | |
# from https://gist.github.com/mfm24/eac71574014a30ff0d3b662f947dd77e | |
# wrapped in class with `val` property | |
# This class is unnecessary for functionality, but helpful for debugging |
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
from itertools import product | |
cmd_blocks = [] # list of (set, cube) | |
for li in open('day22.txt'): | |
# on x=-11..33,y=-6..40,z=-16..37 | |
cmd, rblocks = li.split() | |
xs, ys, zs = rblocks.split(',') | |
# print(xs, ys, zs) | |
xs = [int(x) for x in xs.split('x=')[1].split('..')] | |
ys = [int(x) for x in ys.split('y=')[1].split('..')] |
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 heapq | |
import numpy as np | |
x = np.array([[int(c) for c in l.strip()] for l in open('day15.txt')]) | |
def score_map(mp): | |
h, w = mp.shape | |
def neighbours(y, x): | |
for (dy, dx) in [(1, 0), (-1, 0), (0, 1), (0, -1)]: | |
if 0 <= y + dy < h and 0 <= x + dx < w: |
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
# See eg https://codewithoutrules.com/2018/09/04/python-multiprocessing/ | |
# this deadlocks (Process p never terminates) | |
import logging | |
import multiprocessing | |
import threading | |
import time | |
class SlowHandler(logging.Handler): | |
""" Waits 1 second, then prints """ | |
def emit(self, record): |
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
# inspired by https://gist.github.com/bhawkins/3535131, but simpler | |
import numpy as np | |
def running_filter(data, k=7, op=np.median): | |
""" | |
Turns a 1-d source [a,b,c,d,e] | |
into a 2d [[d,e,... .] | |
[c,d,e,....] | |
[b,c,d,e...] | |
[a,b,c,d,e.]] |
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
# copied from a ipython notebook | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import time | |
import warnings | |
%matplotlib inline | |
def julia(c, extent, pixels): | |
t, l, b, r = extent | |
ny, nx = pixels |
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
# config_sectioner | |
# | |
# Treats a text file as a python file object, only | |
# inserting, adding or removing text in a marked off section. | |
# If the section doesn't exist it is appended to the file. | |
# Include information about the last date the file was written | |
# and by which programs. All blocks have a unique key, so that | |
# a file can contain multiple ConfigSections with different keys. | |
from __future__ import division, print_function | |
from cStringIO import StringIO |
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
# -*- coding: utf-8 -*- | |
# MFM 2015-06-04 | |
# Trying to make this send data over wsgi with a | |
# generator to give a time-dependant graph | |
# goal is to acceot a generator like: | |
# def source(): | |
# while True: | |
# time.sleep(1) | |
# yield random() | |
# and get updates live in a webpage. |
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat Jan 10 18:34:00 2015 | |
@author: matt | |
""" | |
from __future__ import print_function | |
from functools import wraps | |
from collections import defaultdict | |
path = "../localanagrampage/wordswithfriends.txt" |
NewerOlder