Minor cleaning 2023.

This commit is contained in:
Mikaël Capelle 2023-12-30 19:35:06 +01:00
parent d002e419c3
commit 3a9c7e728b
4 changed files with 34 additions and 21 deletions

19
poetry.lock generated
View File

@ -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)"] extra = ["lxml (>=4.6)", "pydot (>=1.4.2)", "pygraphviz (>=1.11)", "sympy (>=1.10)"]
test = ["pytest (>=7.2)", "pytest-cov (>=4.0)"] 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]] [[package]]
name = "nodeenv" name = "nodeenv"
version = "1.8.0" version = "1.8.0"
@ -1375,4 +1392,4 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "b05e052aed9ec92c0edf386f9143271d69f3fc6e4c56a638721a16d7fcb2065b" content-hash = "0b582360ca444fe39469eaa4a242c74c516ef5e1cf8d4d69be7623dbbd3d6e6b"

View File

@ -45,6 +45,7 @@ isort = "^5.13.2"
ruff = "^0.1.8" ruff = "^0.1.8"
poethepoet = "^0.24.4" poethepoet = "^0.24.4"
ipykernel = "^6.27.1" ipykernel = "^6.27.1"
networkx-stubs = "^0.0.1"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]

View File

@ -1,11 +1,8 @@
import sys import sys
from collections import defaultdict
from dataclasses import dataclass
import numpy as np import numpy as np
from sympy import solve, symbols from sympy import solve, symbols
lines = sys.stdin.read().splitlines() lines = sys.stdin.read().splitlines()
positions = [ positions = [
@ -19,7 +16,7 @@ velocities = [
low, high = [7, 27] if len(positions) <= 10 else [200000000000000, 400000000000000] low, high = [7, 27] if len(positions) <= 10 else [200000000000000, 400000000000000]
count = 0 count = 0
for i, (p1, v1) in enumerate(zip(positions, velocities)): 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] p, r = p1[:2], v1[:2]
q, s = p2[:2], v2[:2] q, s = p2[:2], v2[:2]
@ -31,7 +28,7 @@ for i, (p1, v1) in enumerate(zip(positions, velocities)):
continue continue
else: else:
assert rs != 0 assert rs != 0
t = np.cross((q - p), s) / rs t = np.cross((q - p), s) / rs
u = np.cross((q - p), r) / rs u = np.cross((q - p), r) / rs
if t >= 0 and u >= 0: if t >= 0 and u >= 0:
@ -50,14 +47,16 @@ print(f"answer 1 is {answer_1}")
# p3 + t3 * v3 == p0 + t3 * v0 # p3 + t3 * v3 == p0 + t3 * v0
# ... # ...
# pn + tn * vn == p0 + tn * 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 # equations (x / y / z), so 3 lines give 9 equations and 9
# variables: position (3), velocities (3) and times (3). # variables: position (3), velocities (3) and times (3).
n = 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 = [] equations = []
for i, ti in zip(range(n), ts): 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]): for p, d, pi, di in zip((x, y, z), (vx, vy, vz), positions[i], velocities[i]):

View File

@ -1,24 +1,20 @@
import sys import sys
from collections import defaultdict
from dataclasses import dataclass
import networkx as nx import networkx as nx
components = { components = {
(p := line.split(": "))[0]: p[1].split() for line in sys.stdin.read().splitlines() (p := line.split(": "))[0]: p[1].split() for line in sys.stdin.read().splitlines()
} }
targets = { targets = {t for c in components for t in components[c] if t not in components}
t for c in components for t in components[c] if t not in components
}
G = nx.Graph() graph = nx.Graph()
G.add_nodes_from(list(components) + list(targets)) graph.add_edges_from((u, v) for u, vs in components.items() for v in vs)
G.add_edges_from((u, v) for u, vs in components.items() for v in vs)
cut = nx.minimum_edge_cut(G) cut = nx.minimum_edge_cut(graph)
G.remove_edges_from(cut) graph.remove_edges_from(cut)
c1, c2 = nx.connected_components(G) c1, c2 = nx.connected_components(graph)
# part 1 # part 1
answer_1 = len(c1) * len(c2) answer_1 = len(c1) * len(c2)