Skip to content

Commit 061573a

Browse files
committed
day 8 and 10
1 parent f126925 commit 061573a

File tree

5 files changed

+1223
-27
lines changed

5 files changed

+1223
-27
lines changed

2018/08/08.ipynb

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import sys\n",
10+
"sys.path.append(\"..\")\n",
11+
"\n",
12+
"from utils import *\n",
13+
"data = dl_input(8, year=2018, dest_dir='.', sc='../session_cookie.txt')\n",
14+
"\n",
15+
"with open('./input.txt', 'r') as f:\n",
16+
" data = f.read()"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 3,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"# data = \"2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2\""
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 9,
31+
"metadata": {
32+
"scrolled": false
33+
},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"Part 1: 36566\n",
40+
"Part 2: 30548\n"
41+
]
42+
}
43+
],
44+
"source": [
45+
"# Easy-peasy: with recursion\n",
46+
"\n",
47+
"def recur(N, part): \n",
48+
" \n",
49+
" n_children, n_meta, *N = N\n",
50+
" \n",
51+
" children = []\n",
52+
" for _ in range(n_children):\n",
53+
" N, child = recur(N, part)\n",
54+
" children += [child]\n",
55+
"\n",
56+
" if part == 1 or n_children == 0:\n",
57+
" return N[n_meta:], sum(N[:n_meta]) + sum(children)\n",
58+
" \n",
59+
" return N[n_meta:], sum(children[i-1] for i in N[:n_meta] if 0 < i <= len(children))\n",
60+
"\n",
61+
"\n",
62+
"for p in [1, 2]:\n",
63+
" print(f\"Part {p}:\", recur(map(int, data.split()), part=p)[1])"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 10,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"name": "stdout",
73+
"output_type": "stream",
74+
"text": [
75+
"Part 1: 36566\n",
76+
"Part 2: 30548\n"
77+
]
78+
}
79+
],
80+
"source": [
81+
"# Approach 2\n",
82+
"# \n",
83+
"# Recursively build tree first\n",
84+
"\n",
85+
"from dataclasses import dataclass\n",
86+
"from typing import List, Union\n",
87+
"\n",
88+
"@dataclass\n",
89+
"class node:\n",
90+
" meta: int = 0\n",
91+
" children: List['node'] = ()\n",
92+
" \n",
93+
" def part_1(self) -> int:\n",
94+
" return sum(self.meta) + sum(n.part_1() for n in self.children)\n",
95+
"\n",
96+
" def part_2(self) -> int:\n",
97+
" if self.children:\n",
98+
" return sum(self.children[m-1].part_2() \n",
99+
" for m in self.meta if 0 < m <= len(self.children))\n",
100+
" else:\n",
101+
" return sum(self.meta)\n",
102+
"\n",
103+
" \n",
104+
"def recur_tree(N): \n",
105+
" n_children, n_meta, *N = N\n",
106+
" \n",
107+
" children = []\n",
108+
" for _ in range(n_children):\n",
109+
" N, child = recur_tree(N)\n",
110+
" children += [child]\n",
111+
"\n",
112+
" return N[n_meta:], node(meta=N[:n_meta], children=children)\n",
113+
" \n",
114+
"tree = recur_tree(map(int, data.split()))[1]\n",
115+
"\n",
116+
"print(f\"Part 1:\", tree.part_1())\n",
117+
"print(f\"Part 2:\", tree.part_2())"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 7,
123+
"metadata": {},
124+
"outputs": [
125+
{
126+
"name": "stdout",
127+
"output_type": "stream",
128+
"text": [
129+
"Part 1: 36566\n",
130+
"Part 2: 30548\n"
131+
]
132+
}
133+
],
134+
"source": [
135+
"# Iterative version\n",
136+
"\n",
137+
"def iter_process(N, part):\n",
138+
" num_nodes, num_meta, *N = N\n",
139+
" stack = [[num_nodes, num_meta, []]]\n",
140+
"\n",
141+
" while True:\n",
142+
" if stack[-1][0] == 0:\n",
143+
" _, num_meta, meta = stack.pop()\n",
144+
" \n",
145+
" if part == 1:\n",
146+
" tot = sum(meta) + sum(N[:num_meta])\n",
147+
" elif part == 2:\n",
148+
" tot = sum([meta[i-1] for i in N[:num_meta] if 0 < i <= len(meta)])\n",
149+
" \n",
150+
" if not len(stack):\n",
151+
" return tot\n",
152+
" \n",
153+
" stack[-1][2].append(tot)\n",
154+
" N = N[num_meta:]\n",
155+
" else:\n",
156+
" stack[-1][0] -= 1\n",
157+
" num_nodes, num_meta, *N = N\n",
158+
"\n",
159+
" if num_nodes == 0:\n",
160+
" stack[-1][2].append(sum(N[:num_meta]))\n",
161+
" N = N[num_meta:]\n",
162+
" else:\n",
163+
" stack.append([num_nodes, num_meta, []])\n",
164+
" \n",
165+
" \n",
166+
"print(\"Part 1:\", iter_process(map(int, data.split()), part=1))\n",
167+
"print(\"Part 2:\", iter_process(map(int, data.split()), part=2))"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 106,
173+
"metadata": {},
174+
"outputs": [
175+
{
176+
"name": "stdout",
177+
"output_type": "stream",
178+
"text": [
179+
"Part 1: 36566\n",
180+
"Part 2: 30548\n"
181+
]
182+
}
183+
],
184+
"source": [
185+
"# Iterative version (factored-to-death)\n",
186+
"\n",
187+
"def iter_process(N, part):\n",
188+
" stack = [[1, 0, []]]\n",
189+
" \n",
190+
" while True:\n",
191+
" if stack[-1][0]:\n",
192+
" stack[-1][0] -= 1\n",
193+
" num_nodes, num_meta, *N = N\n",
194+
" if num_nodes == 0:\n",
195+
" tot = sum(N[:num_meta])\n",
196+
" else:\n",
197+
" num_nodes, num_meta, meta = stack.pop()\n",
198+
" if not len(stack):\n",
199+
" return meta[0] \n",
200+
" if part == 1:\n",
201+
" tot = sum(meta) + sum(N[:num_meta])\n",
202+
" elif part == 2:\n",
203+
" tot = sum(meta[i-1] for i in N[:num_meta] if 0 < i <= len(meta))\n",
204+
"\n",
205+
" if num_nodes == 0:\n",
206+
" stack[-1][2].append(tot)\n",
207+
" N = N[num_meta:]\n",
208+
" else:\n",
209+
" stack.append([num_nodes, num_meta, []])\n",
210+
" \n",
211+
" \n",
212+
"print(\"Part 1:\", iter_process(map(int, data.split()), part=1))\n",
213+
"print(\"Part 2:\", iter_process(map(int, data.split()), part=2))"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {},
220+
"outputs": [],
221+
"source": []
222+
}
223+
],
224+
"metadata": {
225+
"kernelspec": {
226+
"display_name": "Python 3 (ipykernel)",
227+
"language": "python",
228+
"name": "python3"
229+
},
230+
"language_info": {
231+
"codemirror_mode": {
232+
"name": "ipython",
233+
"version": 3
234+
},
235+
"file_extension": ".py",
236+
"mimetype": "text/x-python",
237+
"name": "python",
238+
"nbconvert_exporter": "python",
239+
"pygments_lexer": "ipython3",
240+
"version": "3.10.8"
241+
}
242+
},
243+
"nbformat": 4,
244+
"nbformat_minor": 4
245+
}

0 commit comments

Comments
 (0)