From 4a2a63b0b07127b1bb5e214758dcaf9e90d8b9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 30 Dec 2023 19:35:06 +0100 Subject: [PATCH] Minor cleaning 2023. --- poetry.lock | 19 ++++++++++++++++++- pyproject.toml | 1 + src/holt59/aoc/2023/day24.py | 17 ++++++++--------- src/holt59/aoc/2023/day25.py | 18 +++++++----------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/poetry.lock b/poetry.lock index f0e2dd8..a6645da 100644 --- a/poetry.lock +++ b/poetry.lock @@ -566,6 +566,23 @@ doc = ["nb2plots (>=0.7)", "nbconvert (<7.9)", "numpydoc (>=1.6)", "pillow (>=9. extra = ["lxml (>=4.6)", "pydot (>=1.4.2)", "pygraphviz (>=1.11)", "sympy (>=1.10)"] test = ["pytest (>=7.2)", "pytest-cov (>=4.0)"] +[[package]] +name = "networkx-stubs" +version = "0.0.1" +description = "Typing stubs for NetworkX" +optional = false +python-versions = "*" +files = [ + {file = "networkx-stubs-0.0.1.tar.gz", hash = "sha256:1751cbc87898328f89d79476ec6363174c05f9e48592f0953cc1077188717a21"}, + {file = "networkx_stubs-0.0.1-py3-none-any.whl", hash = "sha256:ce58dff9b9dcbfdf895d0fce20fa8a73f2e82e581004596b7552086b5bb91366"}, +] + +[package.dependencies] +networkx = "*" + +[package.extras] +dev = ["black", "flake8", "flake8-black", "flake8-pyi", "mypy", "setuptools", "wheel"] + [[package]] name = "nodeenv" version = "1.8.0" @@ -1375,4 +1392,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "b05e052aed9ec92c0edf386f9143271d69f3fc6e4c56a638721a16d7fcb2065b" +content-hash = "0b582360ca444fe39469eaa4a242c74c516ef5e1cf8d4d69be7623dbbd3d6e6b" diff --git a/pyproject.toml b/pyproject.toml index f57e7d4..6ea0d45 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,6 +45,7 @@ isort = "^5.13.2" ruff = "^0.1.8" poethepoet = "^0.24.4" ipykernel = "^6.27.1" +networkx-stubs = "^0.0.1" [build-system] requires = ["poetry-core"] diff --git a/src/holt59/aoc/2023/day24.py b/src/holt59/aoc/2023/day24.py index e7e481a..645160f 100644 --- a/src/holt59/aoc/2023/day24.py +++ b/src/holt59/aoc/2023/day24.py @@ -1,11 +1,8 @@ import sys -from collections import defaultdict -from dataclasses import dataclass + import numpy as np - from sympy import solve, symbols - lines = sys.stdin.read().splitlines() positions = [ @@ -19,7 +16,7 @@ velocities = [ low, high = [7, 27] if len(positions) <= 10 else [200000000000000, 400000000000000] count = 0 for i, (p1, v1) in enumerate(zip(positions, velocities)): - for p2, v2 in zip(positions[i + 1:], velocities[i + 1:]): + for p2, v2 in zip(positions[i + 1 :], velocities[i + 1 :]): p, r = p1[:2], v1[:2] q, s = p2[:2], v2[:2] @@ -31,7 +28,7 @@ for i, (p1, v1) in enumerate(zip(positions, velocities)): continue else: assert rs != 0 - t = np.cross((q - p), s) / rs + t = np.cross((q - p), s) / rs u = np.cross((q - p), r) / rs if t >= 0 and u >= 0: @@ -50,14 +47,16 @@ print(f"answer 1 is {answer_1}") # p3 + t3 * v3 == p0 + t3 * v0 # ... # pn + tn * vn == p0 + tn * v0 -# +# -# we can solve with only 3 lines since each lines contains 3 +# we can solve with only 3 lines since each lines contains 3 # equations (x / y / z), so 3 lines give 9 equations and 9 # variables: position (3), velocities (3) and times (3). n = 3 -x, y, z, vx, vy, vz, *ts = symbols('x y z vx vy vz ' + ' '.join(f't{i}' for i in range(n + 1))) +x, y, z, vx, vy, vz, *ts = symbols( + "x y z vx vy vz " + " ".join(f"t{i}" for i in range(n + 1)) +) equations = [] for i, ti in zip(range(n), ts): for p, d, pi, di in zip((x, y, z), (vx, vy, vz), positions[i], velocities[i]): diff --git a/src/holt59/aoc/2023/day25.py b/src/holt59/aoc/2023/day25.py index d6afdf1..9addbf8 100644 --- a/src/holt59/aoc/2023/day25.py +++ b/src/holt59/aoc/2023/day25.py @@ -1,24 +1,20 @@ import sys -from collections import defaultdict -from dataclasses import dataclass + import networkx as nx components = { (p := line.split(": "))[0]: p[1].split() for line in sys.stdin.read().splitlines() } -targets = { - t for c in components for t in components[c] if t not in components -} +targets = {t for c in components for t in components[c] if t not in components} -G = nx.Graph() -G.add_nodes_from(list(components) + list(targets)) -G.add_edges_from((u, v) for u, vs in components.items() for v in vs) +graph = nx.Graph() +graph.add_edges_from((u, v) for u, vs in components.items() for v in vs) -cut = nx.minimum_edge_cut(G) -G.remove_edges_from(cut) +cut = nx.minimum_edge_cut(graph) +graph.remove_edges_from(cut) -c1, c2 = nx.connected_components(G) +c1, c2 = nx.connected_components(graph) # part 1 answer_1 = len(c1) * len(c2)