Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc84a78d

Browse files
committed
day25
1 parentc6600ec commitc84a78d

File tree

3 files changed

+1503
-4
lines changed

3 files changed

+1503
-4
lines changed

‎adventOfCode/2018/AdventOfCode2018.ipynb

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,7 +2735,7 @@
27352735
},
27362736
{
27372737
"cell_type":"code",
2738-
"execution_count":76,
2738+
"execution_count":17,
27392739
"metadata": {},
27402740
"outputs": [],
27412741
"source": [
@@ -2744,13 +2744,58 @@
27442744
},
27452745
{
27462746
"cell_type":"code",
2747-
"execution_count":77,
2747+
"execution_count":19,
27482748
"metadata": {},
2749-
"outputs": [],
2749+
"outputs": [
2750+
{
2751+
"name":"stdout",
2752+
"output_type":"stream",
2753+
"text": [
2754+
"338\n"
2755+
]
2756+
}
2757+
],
27502758
"source": [
27512759
"## solution 1\n",
2752-
""
2760+
"import sys\n",
2761+
"import re\n",
2762+
"from collections import deque\n",
2763+
"\n",
2764+
"P = []\n",
2765+
"for line in input_data.read().strip().split('\\n'):\n",
2766+
" w,x,y,z = map(int, re.findall('-?\\d+', line))\n",
2767+
" P.append((w,x,y,z))\n",
2768+
"E = [set() for _ in range(len(P))]\n",
2769+
"for i,(w1,x1,y1,z1) in enumerate(P):\n",
2770+
" for j,(w2,x2,y2,z2) in enumerate(P):\n",
2771+
" d = abs(w1-w2)+abs(x1-x2)+abs(y1-y2)+abs(z1-z2)\n",
2772+
" if d <= 3:\n",
2773+
" E[i].add(j)\n",
2774+
"\n",
2775+
"S = set()\n",
2776+
"ans = 0\n",
2777+
"for i in range(len(P)):\n",
2778+
" if i in S:\n",
2779+
" continue\n",
2780+
" ans += 1\n",
2781+
" Q = deque()\n",
2782+
" Q.append(i)\n",
2783+
" while Q:\n",
2784+
" x = Q.popleft()\n",
2785+
" if x in S:\n",
2786+
" continue\n",
2787+
" S.add(x)\n",
2788+
" for y in E[x]:\n",
2789+
" Q.append(y)\n",
2790+
"print(ans)"
27532791
]
2792+
},
2793+
{
2794+
"cell_type":"code",
2795+
"execution_count":null,
2796+
"metadata": {},
2797+
"outputs": [],
2798+
"source": []
27542799
}
27552800
],
27562801
"metadata": {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp