From 9aac127be3a1431d37ca6144729657d1340e4e66 Mon Sep 17 00:00:00 2001 From: Viliam Pucik Date: Thu, 15 Dec 2022 17:11:46 +0100 Subject: [PATCH] Stashing prototypes --- 2015/20.py | 98 +++++++++++++++++++++++++++++++++ 2022/14.py | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/15.py | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 402 insertions(+) create mode 100755 2015/20.py create mode 100755 2022/14.py create mode 100755 2022/15.py diff --git a/2015/20.py b/2015/20.py new file mode 100755 index 0000000..1199fdc --- /dev/null +++ b/2015/20.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +from array import array +from cmath import polar +from collections import Counter, defaultdict, deque +from copy import deepcopy +from functools import cache +from hashlib import md5 +from heapq import heappop, heappush +from itertools import accumulate, chain, cycle, combinations, combinations_with_replacement, count, permutations, product +from math import factorial, gcd, prod, floor, sqrt +from operator import add, mul, gt, lt, eq, iand, ior, lshift, rshift, itemgetter +import networkx as nx +import re +#import regex + +# lights1 = array("B", [0] * 1_000_000) +# lights2 = array("I", [0] * 1_000_000) + +# r, phi = polar((j - i) / 1j) # rotate 90 degrees + +# @cache +# def solve(x): +# return x +# +# solve.cache_clear() + +# h = md5(f"{key}{i}".encode()).hexdigest() + +# heap = [(0, 0, 0)] +# while heap: +# risk, r, c = heappop(heap) +# heappush(heap, (risk * 2, r_, c_)) + +# lcm = lcm * i // gcd(lcm, i) + +# regexp = re.compile(r"([^:]+): (\d+)-(\d+) or (\d+)-(\d+)") +# (field, lo1, hi1, lo2, hi2) = regexp.fullmatch(line).groups() + +# r = regex.compile("(?P 42 (?&R)? 31 )") # recursive pattern +# r.fullmatch(m) + +p = int(input()) + +# a = b = None + +# for i in (2, 3, 5, 7, 11, 13, 17, 19, 23): +# number = i +# presents = 10 + i * 10 + +# while presents < p: +# number *= i +# presents += number * 10 + +# if b is None or b > number: +# b = number +# if a is None or a < number // i: +# a = number // i + +# print(i, number // i, number, a, b, presents) + +# print(a, b) + + +for i in count(1): + presents = 0 + for j in range(1, i // 2 + 1): + if i % j == 0: + presents += j * 10 + if presents >= p: + print(i) + break + if i % 1000 == 0: + print(i, presents) + +# 34_000_000 +# 13_540_800 +# 472_440 + +# 17600 + +# 1 1 +# 2 1 2 +# 3 1 3 +# 4 1 2 4 +# 5 1 5 +# 6 1 2 3 6 +# 7 1 7 +# 8 1 2 4 8 +# 9 1 3 9 +# 10 1 2 5 10 +# 15 1 3 5 15 +# 16 1 2 4 8 16 + +# 2_097_152 20_971_510 41_943_030 + +# 2_476_099.0 2_097_152 + +1_236_880 diff --git a/2022/14.py b/2022/14.py new file mode 100755 index 0000000..38b1de8 --- /dev/null +++ b/2022/14.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python +from itertools import count + +step = lambda a: (a >= 0) - (a < 0) +m, bottom = set(), 0 + +for line in open(0).read().splitlines(): + path = [ + (int(x), int(y)) + for xy in line.split(" -> ") + for x, y in [xy.split(",")] + ] + for a, b in zip(path, path[1:]): + bottom = max(bottom, a[1], b[1]) + for i in range(a[0], b[0] + (xstep := step(b[0] - a[0])), xstep): + for j in range(a[1], b[1] + (ystep := step(b[1] - a[1])), ystep): + m.add((i, j)) + +# print(bottom, m) +# for y in range(11): +# for x in range(493, 505): +# print("#" if (x, y) in m else ".", end="") +# print() + +for u in count(1): + s = (500, 0) + + while s[1] < bottom: + while s + (0, 1) not in m and s[1] + 1 < bottom: + s += (0, 1) + + if s + (-1, 1) not in m and s[1] + 1 < bottom: + s += (-1, 1) + elif s + (1, 1) not in m and s[1] + 1 < bottom: + s += (1, 1) + else: + m.add(s) + break + + + if void or int(s.imag) >= bottom: + break + +print(u-1) + +exit() + + + +sign = lambda a: 1 if a == 0 else (a > 0) - (a < 0) +lines = open(0).read().splitlines() +for line in lines: + path = [] + for xy in line.split(" -> "): + x, y = xy.split(",") + path.append(complex(int(x), int(y))) + # print(path) + # print(line) + + for a, b in zip(path, path[1:]): + m.add(a) + m.add(b) + for x in range(int(a.real), int(b.real) + sign(b.real - a.real), sign(b.real - a.real)): + for y in range(int(a.imag), int(b.imag) + sign(b.imag - a.imag), sign(b.imag - a.imag)): + m.add(complex(x,y)) + +bottom = int(max(i.imag for i in m)) + 2 +# print(bottom) +for u in count(1): + void = False + s = complex(500, 0) + if s in m: + break + + while True: + # print(u, s) + while s + 1j not in m and int(s.imag + 1) < bottom: + s += 1j + + # if int(s.imag) >= bottom: + # void = True + # break + + if s + -1+1j not in m and int(s.imag + 1) < bottom: + s += -1+1j + elif s + 1+1j not in m and int(s.imag + 1) < bottom: + s += 1+1j + else: + m.add(s) + break + + + # if void or int(s.imag) >= bottom: + # break + +print(u-1) +# print(m) +# for y in range(11): +# for x in range(493, 505): +# print("#" if complex(x, y) in m else ".", end="") +# print() + +exit() +sign = lambda a: 1 if a == 0 else (a > 0) - (a < 0) +m = set() + +lines = open(0).read().splitlines() +for line in lines: + path = [] + for xy in line.split(" -> "): + x, y = xy.split(",") + path.append(complex(int(x), int(y))) + # print(path) + # print(line) + + for a, b in zip(path, path[1:]): + m.add(a) + m.add(b) + for x in range(int(a.real), int(b.real) + sign(b.real - a.real), sign(b.real - a.real)): + for y in range(int(a.imag), int(b.imag) + sign(b.imag - a.imag), sign(b.imag - a.imag)): + m.add(complex(x,y)) + +bottom = int(max(i.imag for i in m)) +# print(bottom) +for u in count(1): + void = False + s = complex(500, 0) + if s in m: + break + + while int(s.imag) < bottom: + # print(u, s) + while s + 1j not in m: + s += 1j + + if int(s.imag) >= bottom: + void = True + break + + if s + -1+1j not in m: + s += -1+1j + elif s + 1+1j not in m: + s += 1+1j + else: + m.add(s) + break + + + if void or int(s.imag) >= bottom: + break + +print(u-1) +# print(m) +# for y in range(11): +# for x in range(493, 505): +# print("#" if complex(x, y) in m else ".", end="") +# print() diff --git a/2022/15.py b/2022/15.py new file mode 100755 index 0000000..04d83dc --- /dev/null +++ b/2022/15.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python +from array import array +from cmath import polar +from collections import Counter, defaultdict, deque +from copy import deepcopy +from functools import cache +from hashlib import md5 +from heapq import heappop, heappush +from itertools import accumulate, chain, cycle, combinations, combinations_with_replacement, count, permutations, product +from math import factorial, gcd, prod +from operator import add, mul, gt, lt, eq, iand, ior, lshift, rshift, itemgetter +import networkx as nx +import re +#import regex + +# lights1 = array("B", [0] * 1_000_000) +# lights2 = array("I", [0] * 1_000_000) + +# r, phi = polar((j - i) / 1j) # rotate 90 degrees + +# @cache +# def solve(x): +# return x +# +# solve.cache_clear() + +# h = md5(f"{key}{i}".encode()).hexdigest() + +# heap = [(0, 0, 0)] +# while heap: +# risk, r, c = heappop(heap) +# heappush(heap, (risk * 2, r_, c_)) + +# lcm = lcm * i // gcd(lcm, i) + +# regexp = re.compile(r"([^:]+): (\d+)-(\d+) or (\d+)-(\d+)") +# (field, lo1, hi1, lo2, hi2) = regexp.fullmatch(line).groups() + +# r = regex.compile("(?P 42 (?&R)? 31 )") # recursive pattern +# r.fullmatch(m) + + +# row = 2000000 +# row_scanned = set() +# row_elements = set() +# lines = open(0).read().splitlines() + +# for line in lines: +# sx, sy, bx, by = map(int, re.findall(r"(-?\d+)", line)) +# d = abs(sx - bx) + abs(sy - by) +# if sy - d <= row <= sy + d: +# w = d - abs(sy - row) + +# for x in range(sx - w, sx + w + 1): +# row_scanned.add(x) + +# # print(sx, sy, bx, by, d, w, row_scanned, line) + +# if sy == row: +# row_elements.add(sx) + +# if by == row: +# row_elements.add(bx) + +# print(len(row_scanned - row_elements)) +import z3 + +s = z3.Solver() +x, y = z3.Int("x"), z3.Int("y") + +s.add(0 <= x); s.add(x <= 4000000) +s.add(0 <= y); s.add(y <= 4000000) + +def z3_abs(x): + return z3.If(x >= 0, x, -x) + +lines = open(0).read().splitlines() +for line in lines: + sx, sy, bx, by = map(int, re.findall(r"(-?\d+)", line)) + d = abs(sx - bx) + abs(sy - by) + s.add(z3_abs(sx - x) + z3_abs(sy - y) > d) + +s.check() +model = s.model() + +print("Part 2:", model[x].as_long() * 4000000 + model[y].as_long()) + + +# # m = set() +# lines = open(0).read().splitlines() +# r = defaultdict(list) + +# for line in lines: +# sx, sy, bx, by = map(int, re.findall(r"(-?\d+)", line)) +# d = abs(sx - bx) + abs(sy - by) + +# for y in range(sy - d, sy + d + 1): +# w = d - abs(sy - y) +# # for x in range(sx - w, sx + w + 1): +# # m.add((x, y)) +# r[y].append((sx - w, sx + w)) +# # print(sx, sy, bx, by, d, w, row_scanned, line) + +# # m.add((sx, sy)) +# # m.add((bx, by)) + +# print("Searching...") +# space = 20 +# space = 4_000_001 +# for y in range(0, space): +# if y % 1000 == 0: +# print("Searching", y) + +# for x in range(0, space): +# for a, b in r[y]: +# if a <= x <= b: +# break +# else: +# print(x * 4000000 + y) +# exit() + +# # # row = list() +# # # for a, b in r[y]: +# # # if a <= (sx - w) <= b \ +# # # or a <= (sx + w) <= b \ +# # # or (sx - w) <= a <= (sx + w) \ +# # # or (sx - w) <= b <= (sx + w): +# # # row.append((min(sx - w, a), max(sx + w, b))) +# # # else: +# # # row.append((a, b)) +# # # r[y] = row + +# # row = array("B", [0] * space) + +# # # print(r[y]) +# # for a, b in r[y]: +# # # print(a, b, end=", ") +# # for i in range( max(0, a), min(space, b+1) ): +# # row[i] = 1 +# # # print(row) +# # for x in range(0, space): +# # if row[x] == 0: +# # # # if (x, y) not in m: +# # print(x * 4000000 + y) +# # exit() + +# # print(len(m))