浏览代码

🧦

master
Roderic Day 1年前
父节点
当前提交
83ae49c23a
共有 3 个文件被更改,包括 25 次插入27 次删除
  1. +1
    -1
      VERSION
  2. +1
    -2
      makefile
  3. +23
    -24
      y2023/p25.py

+ 1
- 1
VERSION 查看文件

@@ -1 +1 @@
🐰
🧦

+ 1
- 2
makefile 查看文件

@@ -28,11 +28,10 @@ save:
test `git log -1 --format=%s` == `cat VERSION` \
&& git commit --amend --reuse-message=head \
|| git commit -m `cat VERSION`
git push -f

${DATA} ${TEST}:
# avoid spam in the lead up to the event
test ${DAY} -le `date +%d` || test ${YEAR} -lt `date +%Y`
TZ=America/New_York test ${DAY} -le `date +%d` || test ${YEAR} -lt `date +%Y`
# only poll if data isn't yet stored locally
test -f ${DATA} || curl -s -b "session=${SESSION}" ${URL}/input > ${DATA}
# try to get test data

+ 23
- 24
y2023/p25.py 查看文件

@@ -1,6 +1,4 @@
import random
import itertools
import copy
import collections


@@ -13,6 +11,27 @@ def make_graph(text):
return graph


def rank_wires(graph, n_cycles=100, n_top=20):
tally = collections.Counter()
for aa, bb in sorted(itertools.combinations(graph, 2))[:n_cycles]:
path = shortest_path(graph, aa, bb)
tally.update(frozenset(pair) for pair in zip(path, path[1:]))
return [k for k, _ in tally.most_common(n_top)]


def shortest_path(graph, aa, bb):
state = {aa: None}
seen = {}
while bb not in state:
state = {new: old for old in state for new in graph[old] if new not in seen}
seen |= state

path = [bb]
while path[-1] != aa:
path.append(seen[path[-1]])
return path


def cut(graph, wires):
graph = graph.copy()
for aa, bb in wires:
@@ -35,30 +54,10 @@ def subgraphs(graph):
return conns


def shortest_path(graph, aa, bb):
state = {aa: None}
seen = {}
while bb not in state:
state = {new: old for old in state for new in graph[old] if new not in seen}
seen |= state

path = [bb]
while path[-1] != aa:
path.append(seen[path[-1]])
return path


def rank_wires(graph, n_cycles=100, n_top=20):
tally = collections.Counter()
for aa, bb in sorted(itertools.combinations(graph, 2))[:n_cycles]:
path = shortest_path(graph, aa, bb)
tally.update(frozenset(pair) for pair in zip(path, path[1:]))
return [k for k, _ in tally.most_common(n_top)]


text = open(0).read()
graph = make_graph(text)
for triplet in itertools.combinations(rank_wires(graph), 3):
wires = rank_wires(graph)
for triplet in itertools.combinations(wires, 3):
mod_graph = cut(graph, triplet)
subs = subgraphs(mod_graph)
if len(subs) == 2:

正在加载...
取消
保存