From 89a2e56002afc99d6179b260505d0c047b426d3f Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 18 Dec 2022 23:55:45 -0500 Subject: [PATCH] try 2015 again --- 2015/day07.py | 12 ++- 2015/{ => python}/day01.py | 0 2015/{ => python}/day02.py | 0 2015/{ => python}/day03.py | 0 2015/{ => python}/day04.py | 0 2015/{ => python}/day05.py | 0 2015/{ => python}/day06.py | 0 2015/python/day07.py | 56 +++++++++++++ 2015/{ => python}/matrix.py | 0 2015/{ => python}/sample.py | 0 2015/python/shared.py | 161 ++++++++++++++++++++++++++++++++++++ 2015/shared.py | 31 ------- 2022/full/day19.txt | 0 2022/python/day19.py | 27 ++++++ 2022/samples/day19.txt | 0 15 files changed, 252 insertions(+), 35 deletions(-) rename 2015/{ => python}/day01.py (100%) rename 2015/{ => python}/day02.py (100%) rename 2015/{ => python}/day03.py (100%) rename 2015/{ => python}/day04.py (100%) rename 2015/{ => python}/day05.py (100%) rename 2015/{ => python}/day06.py (100%) create mode 100644 2015/python/day07.py rename 2015/{ => python}/matrix.py (100%) rename 2015/{ => python}/sample.py (100%) create mode 100644 2015/python/shared.py delete mode 100644 2015/shared.py create mode 100644 2022/full/day19.txt create mode 100644 2022/python/day19.py create mode 100644 2022/samples/day19.txt diff --git a/2015/day07.py b/2015/day07.py index 1017dda..c0e5e73 100644 --- a/2015/day07.py +++ b/2015/day07.py @@ -1,15 +1,19 @@ import shared import matrix -import numpy as np -def pt1(inp): - print(inp) +def part1(inp): + print(x) + for i in inp: + instruction, destination = i.split(" -> ") + print(instruction, destination) + + def main(): with open(shared.get_fname(7), "r") as f: inp = [x.rstrip() for x in f.readlines()] - pt1(inp) + part1(inp) #pt2(inp) if __name__ == "__main__": diff --git a/2015/day01.py b/2015/python/day01.py similarity index 100% rename from 2015/day01.py rename to 2015/python/day01.py diff --git a/2015/day02.py b/2015/python/day02.py similarity index 100% rename from 2015/day02.py rename to 2015/python/day02.py diff --git a/2015/day03.py b/2015/python/day03.py similarity index 100% rename from 2015/day03.py rename to 2015/python/day03.py diff --git a/2015/day04.py b/2015/python/day04.py similarity index 100% rename from 2015/day04.py rename to 2015/python/day04.py diff --git a/2015/day05.py b/2015/python/day05.py similarity index 100% rename from 2015/day05.py rename to 2015/python/day05.py diff --git a/2015/day06.py b/2015/python/day06.py similarity index 100% rename from 2015/day06.py rename to 2015/python/day06.py diff --git a/2015/python/day07.py b/2015/python/day07.py new file mode 100644 index 0000000..a881b2b --- /dev/null +++ b/2015/python/day07.py @@ -0,0 +1,56 @@ +import shared +import matrix +from pprint import pprint +import numpy as np +import operator + +OPS = { + 'AND': operator.and_, + 'OR': operator.or_, + 'LSHIFT': operator.lshift, + 'RSHIFT': operator.rshift, +} + + +def NOT(x): + return int(x) ^ 65535 + + +def pt1(inp): + print(inp) + registers = {} + for i in inp: + inst, dest = i.split(" -> ") + if " " not in inst: + registers[dest] = inst + continue + inst = inst.split() + if inst[0] == "NOT": + registers[dest] = NOT(registers[inst[1]]) + continue + a,op,b = inst + try: + a = int(a) + except ValueError: + a = int(registers[a]) + try: + b = int(b) + except ValueError: + b = int(registers[b]) + print(a,b) + registers[dest] = OPS[op](a,b) + + #x,op,y = inst.split(" ") + + + pprint(registers) + + +def main(): + with open(shared.get_fname(7), "r") as f: + inp = [x.rstrip() for x in f.readlines()] + pt1(inp) + #pt2(inp) + +if __name__ == "__main__": + main() diff --git a/2015/matrix.py b/2015/python/matrix.py similarity index 100% rename from 2015/matrix.py rename to 2015/python/matrix.py diff --git a/2015/sample.py b/2015/python/sample.py similarity index 100% rename from 2015/sample.py rename to 2015/python/sample.py diff --git a/2015/python/shared.py b/2015/python/shared.py new file mode 100644 index 0000000..3ad982a --- /dev/null +++ b/2015/python/shared.py @@ -0,0 +1,161 @@ +from contextlib import contextmanager +from timeit import default_timer +from pathlib import Path +import cProfile +import functools +import pstats + + +def profile(func): + @functools.wraps(func) + def inner(*args, **kwargs): + profiler = cProfile.Profile() + profiler.enable() + try: + retval = func(*args, **kwargs) + finally: + profiler.disable() + with open("profile.out", "w") as profile_file: + stats = pstats.Stats(profiler, stream=profile_file) + stats.print_stats() + return retval + + return inner + + +spl = lambda y: [int(w) for w in y] + + +def minmax(l): + return min(l), max(l) + + +def load_rows(day): + return [row for row in load(day)] + + +def load(day): + path = Path(get_fname(day)) + return path.read_text().rstrip().split("\n") + + +def get_fname(day: int) -> str: + import sys + + if sys.argv[-1] == "--sample": + return f"../samples/day{day:02}.txt" + else: + return f"../full/day{day:02}.txt" + + +############# +def load_char_matrix(f): + my_file = [] + for line in f: + my_file.append(line.rstrip()) + return [list(x) for x in my_file] + + +def load_file_char_matrix(name): + with open(name, "r") as f: + return load_char_matrix(f) + + +def load_int_matrix(f): + my_file = [] + for line in f: + my_file.append(line.rstrip()) + return [list(map(int, x)) for x in my_file] + + +def load_file_int_matrix(name): + with open(name, "r") as f: + return load_int_matrix(f) + + +def load_word_matrix(f): + my_file = [] + for line in f: + my_file.append(line.rstrip()) + return [x.split(" ") for x in my_file] + + +def load_file_word_matrix(name): + with open(name, "r") as f: + return load_word_matrix(f) + + +############# + + +def rotate(WHAT, times=1): + what = WHAT + for x in range(times): + what = list(zip(*what[::-1])) + return what + + +@contextmanager +def elapsed_timer(): + start = default_timer() + elapser = lambda: default_timer() - start + yield lambda: elapser() + end = default_timer() + elapser = lambda: end - start + + + + +def render_cubes(maxX,maxY,maxZ, my_cubes): + from mpl_toolkits.mplot3d import Axes3D + import numpy as np + import matplotlib.pyplot as plt + from mpl_toolkits.mplot3d.art3d import Poly3DCollection + + def cuboid_data(o, size=(1,1,1)): + X = [[[0, 1, 0], [0, 0, 0], [1, 0, 0], [1, 1, 0]], + [[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0]], + [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]], + [[0, 0, 1], [0, 0, 0], [0, 1, 0], [0, 1, 1]], + [[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]], + [[0, 1, 1], [0, 0, 1], [1, 0, 1], [1, 1, 1]]] + X = np.array(X).astype(float) + for i in range(3): + X[:,:,i] *= size[i] + X += np.array(o) + return X + + def plotCubeAt(positions,sizes=None,colors=None, **kwargs): + if not isinstance(colors,(list,np.ndarray)): colors=["C0"]*len(positions) + if not isinstance(sizes,(list,np.ndarray)): sizes=[(1,1,1)]*len(positions) + g = [] + for p,s,c in zip(positions,sizes,colors): + g.append( cuboid_data(p, size=s) ) + return Poly3DCollection(np.concatenate(g), + facecolors=np.repeat(colors,6, axis=0), **kwargs) + + N1 = maxX + N2 = maxY + N3 = maxZ + ma = np.random.choice([0,1], size=(N1,N2,N3), p=[0.99, 0.01]) + x,y,z = np.indices((N1,N2,N3))-.5 + #positions = np.c_[x[ma==1],y[ma==1],z[ma==1]] + positions = np.c_[my_cubes] + colors= np.random.rand(len(positions),3) + + fig = plt.figure() + ax = fig.add_subplot(projection='3d') + ax.set_aspect('equal') + + pc = plotCubeAt(positions, colors=colors,edgecolor="k") + ax.add_collection3d(pc) + + ax.set_xlim([0,maxX]) + ax.set_ylim([0,maxY]) + ax.set_zlim([0,maxZ]) + #plotMatrix(ax, ma) + #ax.voxels(ma, edgecolor="k") + + plt.show() + + diff --git a/2015/shared.py b/2015/shared.py deleted file mode 100644 index 66f2c10..0000000 --- a/2015/shared.py +++ /dev/null @@ -1,31 +0,0 @@ -def get_fname(day: int) -> str: - import sys - - if sys.argv[-1] == "--sample": - return f"samples/day{day:02}.txt" - else: - return f"full/day{day:02}.txt" - - -def load_file_char_matrix(name): - with open(name, "r") as f: - my_file = [] - for line in f: - my_file.append(line.rstrip()) - return [list(x) for x in my_file] - - -def load_file_int_matrix(name): - with open(name, "r") as f: - my_file = [] - for line in f: - my_file.append(line.rstrip()) - return [list(map(int, x)) for x in my_file] - - -def load_file_word_matrix(name): - with open(name, "r") as f: - my_file = [] - for line in f: - my_file.append(line.rstrip()) - return [x.split(" ") for x in my_file] diff --git a/2022/full/day19.txt b/2022/full/day19.txt new file mode 100644 index 0000000..e69de29 diff --git a/2022/python/day19.py b/2022/python/day19.py new file mode 100644 index 0000000..c385afd --- /dev/null +++ b/2022/python/day19.py @@ -0,0 +1,27 @@ +import matrix +import shared +import itertools +import functools + +# @shared.profile +def part1(rows): + pass + + +# @shared.profile +def part2(rows): + pass + + +def main(): + rows = [row for row in shared.load_rows(19)] + with shared.elapsed_timer() as elapsed: + part1(rows) + print("🕒", elapsed()) + with shared.elapsed_timer() as elapsed: + part2(rows) + print("🕒", elapsed()) + + +if __name__ == "__main__": + main() diff --git a/2022/samples/day19.txt b/2022/samples/day19.txt new file mode 100644 index 0000000..e69de29