Browse Source

Day 2

master
Chris Smith 5 years ago
parent
commit
50511c49db
2 changed files with 365 additions and 4 deletions
  1. 115
    4
      2018.ipynb
  2. 250
    0
      data/02.txt

+ 115
- 4
2018.ipynb View File

@@ -23,15 +23,19 @@
23 23
   {
24 24
    "cell_type": "code",
25 25
    "execution_count": 1,
26
-   "metadata": {},
26
+   "metadata": {
27
+    "day": 0
28
+   },
27 29
    "outputs": [],
28 30
    "source": [
29 31
     "import itertools\n",
32
+    "import functools\n",
33
+    "import operator\n",
30 34
     "\n",
31 35
     "\n",
32 36
     "def get_input(day):\n",
33 37
     "    with open(f'data/{day:02}.txt') as file:\n",
34
-    "        return file.readlines()"
38
+    "        return [l.strip() for l in file.readlines()]"
35 39
    ]
36 40
   },
37 41
   {
@@ -71,7 +75,9 @@
71 75
   {
72 76
    "cell_type": "code",
73 77
    "execution_count": 2,
74
-   "metadata": {},
78
+   "metadata": {
79
+    "day": 1
80
+   },
75 81
    "outputs": [
76 82
     {
77 83
      "data": {
@@ -119,7 +125,9 @@
119 125
   {
120 126
    "cell_type": "code",
121 127
    "execution_count": 3,
122
-   "metadata": {},
128
+   "metadata": {
129
+    "day": 1
130
+   },
123 131
    "outputs": [
124 132
     {
125 133
      "data": {
@@ -144,9 +152,112 @@
144 152
     "\n",
145 153
     "first_duplicate(itertools.accumulate(itertools.cycle(map(int, get_input(1)))))"
146 154
    ]
155
+  },
156
+  {
157
+   "cell_type": "markdown",
158
+   "metadata": {},
159
+   "source": [
160
+    "## Day 2: Inventory Management System\n",
161
+    "\n",
162
+    "You stop falling through time, catch your breath, and check the screen on the device. \"Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10.\" You made it! Now, to find those anomalies.\n",
163
+    "\n",
164
+    "Outside the utility closet, you hear footsteps and a voice. \"...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?\" Another voice responds, \"Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!\"\n",
165
+    "\n",
166
+    "\"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs...\" They walk too far away to hear any more.\n",
167
+    "\n",
168
+    "Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).\n",
169
+    "\n",
170
+    "To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.\n",
171
+    "\n",
172
+    "For example, if you see the following box IDs:\n",
173
+    "\n",
174
+    "    abcdef contains no letters that appear exactly two or three times.\n",
175
+    "    bababc contains two a and three b, so it counts for both.\n",
176
+    "    abbcde contains two b, but no letter appears exactly three times.\n",
177
+    "    abcccd contains three c, but no letter appears exactly two times.\n",
178
+    "    aabcdd contains two a and two d, but it only counts once.\n",
179
+    "    abcdee contains two e.\n",
180
+    "    ababab contains three a and three b, but it only counts once.\n",
181
+    "\n",
182
+    "Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.\n",
183
+    "\n",
184
+    "What is the checksum for your list of box IDs?"
185
+   ]
186
+  },
187
+  {
188
+   "cell_type": "code",
189
+   "execution_count": 4,
190
+   "metadata": {
191
+    "day": 2
192
+   },
193
+   "outputs": [
194
+    {
195
+     "data": {
196
+      "text/plain": [
197
+       "4920"
198
+      ]
199
+     },
200
+     "execution_count": 4,
201
+     "metadata": {},
202
+     "output_type": "execute_result"
203
+    }
204
+   ],
205
+   "source": [
206
+    "def has_dupes(str, n):\n",
207
+    "    return any(str.count(x) == n for x in str)\n",
208
+    "\n",
209
+    "functools.reduce(operator.mul, map(sum, zip(*[(has_dupes(i, 2), has_dupes(i, 3)) for i in get_input(2)])))"
210
+   ]
211
+  },
212
+  {
213
+   "cell_type": "markdown",
214
+   "metadata": {},
215
+   "source": [
216
+    "Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.\n",
217
+    "\n",
218
+    "The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:\n",
219
+    "\n",
220
+    "    abcde\n",
221
+    "    fghij\n",
222
+    "    klmno\n",
223
+    "    pqrst\n",
224
+    "    fguij\n",
225
+    "    axcye\n",
226
+    "    wvxyz\n",
227
+    "\n",
228
+    "The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.\n",
229
+    "\n",
230
+    "What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)"
231
+   ]
232
+  },
233
+  {
234
+   "cell_type": "code",
235
+   "execution_count": 5,
236
+   "metadata": {
237
+    "day": 2
238
+   },
239
+   "outputs": [
240
+    {
241
+     "data": {
242
+      "text/plain": [
243
+       "'fonbwmjquwtapeyzikghtvdxl'"
244
+      ]
245
+     },
246
+     "execution_count": 5,
247
+     "metadata": {},
248
+     "output_type": "execute_result"
249
+    }
250
+   ],
251
+   "source": [
252
+    "def common_letters(str1, str2):\n",
253
+    "    return ''.join([i for i, j in zip(str1, str2) if i == j])\n",
254
+    "\n",
255
+    "max(itertools.starmap(common_letters, itertools.combinations(get_input(2), 2)), key=len)"
256
+   ]
147 257
   }
148 258
  ],
149 259
  "metadata": {
260
+  "celltoolbar": "Edit Metadata",
150 261
   "kernelspec": {
151 262
    "display_name": "Python 3",
152 263
    "language": "python",

+ 250
- 0
data/02.txt View File

@@ -0,0 +1,250 @@
1
+fonbsmjyqugrapsczckghtvdxl
2
+fonpsmjyquwrnpeczikghtvdxw
3
+fonbsmdymuwrapexzikghtvdxl
4
+fonwsmjyquwrapeczikghttdpl
5
+fonbsmjkquwrapeczjkghtvdxx
6
+yonbsmjyquwrapecgikghtvdxc
7
+donbsmjyquqrapeczikghtadxl
8
+monbsmjyquprgpeczikghtvdxl
9
+fonbsmjyquwvapecqgkghtvdxl
10
+fonbsmjyquwrkphczikghsvdxl
11
+fonbomjyeuwvapeczikghtvdxl
12
+fonwsmjyjuwrapoczikghtvdxl
13
+foybsmjyquwcapeczikghsvdxl
14
+fonbsmjyquwrtaeczikgptvdxl
15
+ponbsmpyquwjapeczikghtvdxl
16
+flnbcmjyquwrqpeczikghtvdxl
17
+fonbsmjyquwrapegzikvbtvdxl
18
+fonbjmjyqgwrazeczikghtvdxl
19
+zoabsmjyquwkapeczikghtvdxl
20
+fonbsmjyquwrapecziktxkvdxl
21
+fonbsxjyrpwrapeczikghtvdxl
22
+fonbsmjbquwqapeciikghtvdxl
23
+lonbsmjyquwraphczikghtvdul
24
+ftnbsmjyquwrapcczikghtxdxl
25
+fonbsmjyqgwrapeczikghtldxc
26
+fonbsmjsquwmapeyzikghtvdxl
27
+fonbsmjyqfwrapecziqghtgdxl
28
+yonbsmjyquwraveczikgftvdxl
29
+fovbsmjyquwrapeczikggkvdxl
30
+fonbsmjyquwrapezzikghbvdvl
31
+fonzsmxyquwrapeczukghtvdxl
32
+fonbemjyquwrapevzikghtvrxl
33
+conbsxjxquwrapeczikghtvdxl
34
+fonbsmjsmewrapeczikghtvdxl
35
+folbsmjyqhwrapqczikghtvdxl
36
+fonbsmjyquwrzneczikghtvdxn
37
+fonbsmjyquirapeczikjhtvdll
38
+fontsmgyquwrgpeczikghtvdxl
39
+fonbsmjyauwrapeczbfghtvdxl
40
+ftnbsmjyquwrapecpifghtvdxl
41
+fonvsmjyqewrapeczikghlvdxl
42
+fonbsljyquwrapecziklhtvdxw
43
+fonbbmjyquwrapeczikghvadxl
44
+ponbsmjyquwrspeczikghivdxl
45
+fonbsmjcquwrapeccikghtvuxl
46
+fonbsmjnquwrapetzikghtvlxl
47
+fonbsmjymuwrapeczieghtvdxr
48
+ffnbsxnyquwrapeczikghtvdxl
49
+fonbsmjytuwrajeczzkghtvdxl
50
+fonssmjyquwhapeczikghkvdxl
51
+fonbsajyuuwrapeczikghlvdxl
52
+fonbsmjyquwrapeczihghtcixl
53
+fohbsmjyquwrapzczirghtvdxl
54
+fonbsmjyquwrapecjqnghtvdxl
55
+fonbsmjytuhrapeczihghtvdxl
56
+foabumjyquwrapeczikghtvdxz
57
+conbsmjyqtwrapeczikggtvdxl
58
+fonbsmjyiqwrapeczokghtvdxl
59
+fondsmjypuwrapeczikghtvjxl
60
+fonbswjyquwrapeczikgvtydxl
61
+fonbsmjyqqwrapeczikkhtvdbl
62
+fonbsmjyquwrapemzitghtvdsl
63
+fonbsmjyquwrspecziegxtvdxl
64
+fonbsmpyquwrgpeczikghtwdxl
65
+fodbsmjqquwrapeczmkghtvdxl
66
+fonbsmjkquwrapeczikghpvdxr
67
+fonbsmjyquwrapeczikshzvmxl
68
+fznbsmjyqulrapeczikghkvdxl
69
+fonbsmjyquwripeczikghtbdjl
70
+fcnbsmjyquzrapecyikghtvdxl
71
+ronbxmjyquwrapeczikghgvdxl
72
+fonbsmuyvuwrgpeczikghtvdxl
73
+fonbsmjyyuwraplczikghtudxl
74
+poxbsmjyqewrapeczikghtvdxl
75
+foabsmjyquwrapecziqghtvpxl
76
+ponbsmjrquwrapeczikchtvdxl
77
+fonzzmjyquwrapeczikghtvdxs
78
+wonbsmjyquwghpeczikghtvdxl
79
+fofbsejyquwrapeczikgctvdxl
80
+ponbsmjyquwrayegzikghtvdxl
81
+fonbumjyquwripeczikghtvdxf
82
+fonbsmqyquwrapeczikgftvdxv
83
+qonbsmjyquwraplczitghtvdxl
84
+fmnbsajdquwrapeczikghtvdxl
85
+fonbsrjyquwrapempikghtvdxl
86
+fonbsmjyquwrapeczikgotudxw
87
+fonbsmtyquwrapeflikghtvdxl
88
+fzqbsmjyquwrapecjikghtvdxl
89
+fdnbsmjyquwraqeclikghtvdxl
90
+fvnbsijyquwrapechikghtvdxl
91
+fovbsmjyquwsapeczikghqvdxl
92
+ffjbsmjyqgwrapeczikghtvdxl
93
+fonbsmjyquwrapeczvkhhivdxl
94
+forbamjjquwrapeczikghtvdxl
95
+fonbwmjyquwtapeyzikghtvdxl
96
+fonvsmjyquwrapeczikglnvdxl
97
+fonnsmjyguwrapeczikghtvxxl
98
+fopbsmjyquwrapeczikghtvaxz
99
+fonbsmjyquwiapeczikrhavdxl
100
+fonbsujyquwrapeczikthtvdjl
101
+fonpsmkyeuwrapeczikghtvdxl
102
+fonbsmjyquwrapeczqkgttvdxk
103
+fonbsmjyqzwrapeczikgrtddxl
104
+fokbsmjiquwrapeczikgltvdxl
105
+fonbsmjyqbwrapeczikghttdxo
106
+fonbsejyquwrapeczikghbvdal
107
+fonblmjyquwyaveczikghtvdxl
108
+fonbsmjyquwlzpepzikghtvdxl
109
+fonbsmjyqulrapbczigghtvdxl
110
+fonbsmjyxuwrapecziyghtvsxl
111
+fonbyjjyquwrapeczikghtvdxn
112
+fonbhmjyquwrapeczikghtjhxl
113
+fonbspjykuwraieczikghtvdxl
114
+aonbsmjyquwwapeczikchtvdxl
115
+fombsmjyquwyapeczikghtvdll
116
+fonbsmjynuwrapeczivgbtvdxl
117
+xonbsmjfquwrapeczikghqvdxl
118
+fonbyzjyquwzapeczikghtvdxl
119
+fbnbsmjyquwrapeczimgvtvdxl
120
+qonbsmjyquwraoeczikgftvdxl
121
+fonbsrjyquwrapeczikghtvjxm
122
+fonbsmjyquwrapxjzykghtvdxl
123
+fonbwgjyquwrapecziklhtvdxl
124
+fonjcmjyouwrapeczikghtvdxl
125
+fonbsmjyquwrapefzisuhtvdxl
126
+fonbsmjyqywrspeczikghtvnxl
127
+qonbsmjyquwrapeczlkuhtvdxl
128
+fonbsmjyqlprapeczikghtvdbl
129
+fonbsmjzquwrapedzikfhtvdxl
130
+fonbsmjyquwrapeczizghtvjxq
131
+fonbsmxyquwrrpeczikghtvcxl
132
+fonpsmjyquwoapeczikghjvdxl
133
+fonbshkyauwrapeczikghtvdxl
134
+fonbsmjysuwrapeczilghpvdxl
135
+fovwsxjyquwrapeczikghtvdxl
136
+fonbsmjyquwrppecnikghmvdxl
137
+fonbkmjyiuwrrpeczikghtvdxl
138
+gonbsmjyquwrapeczikphtudxl
139
+foncsmjyqlwrapeczimghtvdxl
140
+fonbsmjhquwrtpeczikghtvdxg
141
+fogbsmjyquarapeczikghtvdil
142
+fonbsmjyquwraperzekghwvdxl
143
+fonbstjyquwrapeczicghtedxl
144
+fonbsmjoquhrapeczikgotvdxl
145
+fonbsmjykuwrareczikgdtvdxl
146
+fonbsmjyvuwrayeczivghtvdxl
147
+fonbzmgyquwraptczikghtvdxl
148
+fonbsmjyqubrapeczikgftvdxb
149
+fonbgmjyjuwrapeczikghtvdul
150
+fonbsmjzqurrapeczikghtvfxl
151
+fonbsmjyiuwrapeczikgstvtxl
152
+fpnbstjyquwrapeczikghtvdcl
153
+fonbpmjyquwrapeczivghtndxl
154
+fonbsmjyquwrapeczilgptvvxl
155
+fonbsmjyqdwripecbikghtvdxl
156
+fonbsmjytuwgapnczikghtvdxl
157
+fonbsejyquwrapedzikghtvdml
158
+fonbsojyqdwrapeczikghtgdxl
159
+fonbsmjykuwrayeczicghtvdxl
160
+foubsmtyquwrapeczikchtvdxl
161
+fonbqmjyqukrapeyzikghtvdxl
162
+fonbsmjyquwaapenzikghtvdwl
163
+fonbsmeyquwrapeyzixghtvdxl
164
+fonusmjyquhrapeczikgytvdxl
165
+fonbsmjyquwrapwazikqhtvdxl
166
+fonwsmeyquwrapeczikghhvdxl
167
+fonmsmjyquxrspeczikghtvdxl
168
+fonqsmjyqxwrapeczikghtvdml
169
+fonfsmjyquwrapeuzikgatvdxl
170
+fonvsmjyquwrapeczikgrtvdul
171
+fonbsmayquwrapeczikihtvdxm
172
+fonbsmnyquwrapecdifghtvdxl
173
+fonbsmjyeuwraseczikghtvdxo
174
+fonbvvjyquwrapeczikghtvdxi
175
+fonbsmjyquwrapeczbkghtorxl
176
+tonbsmjyqvwrapeczikghtvdcl
177
+fonbsmjyquwrapeczhkgbtvdkl
178
+fonqsmjyquwrapenzibghtvdxl
179
+fontsmeyqudrapeczikghtvdxl
180
+qonbsmjyauwrapeczikghtvdbl
181
+fynbsmjyluwrapeczekghtvdxl
182
+fonbsmjhquwrappczikghtvdxt
183
+conbsmjyquwrapeczikahtvdxz
184
+fonbsmjyquorapeczikvftvdxl
185
+fonbsriyquwrapeczikchtvdxl
186
+yonfsmjyquwrapeczikghtvdxq
187
+fonaomjyquwrapecziwghtvdxl
188
+fonbsxsyqdwrapeczikghtvdxl
189
+fonbsqjyouwrapeczikgltvdxl
190
+fonbstsyquwraleczikghtvdxl
191
+fonbsmjyquwraoecztkghtvdsl
192
+fonbsmjyquwrapezzjkghmvdxl
193
+fonbwmjyqnwrapecpikghtvdxl
194
+fonbsmvyqbwrapeczikghtvdsl
195
+fonbsijyquwrazeczikghtvdwl
196
+fonbsmjyouwrapewzikghtldxl
197
+xonbsmjyqcwrapeczikghtvdul
198
+fonbgmjxquwrajeczikghtvdxl
199
+fokbsmjyquwrapechikghtrdxl
200
+fonbqmjyqawrapeczikghtrdxl
201
+fonbwmjzquwtapeyzikghtvdxl
202
+fonbsmjyquwrapecdikgatvdnl
203
+fonbsmjyqowrkpeczikghtvdxj
204
+fonbsmjyquwkapejzikuhtvdxl
205
+fonbsmjyquwrabeozikghtmdxl
206
+fonbsijyeuwrapeczikghtvdxh
207
+fonbsmjhquprapeczizghtvdxl
208
+fonesmjyquwrapcczikghtvdxh
209
+fonbamjyquwrapeczifrhtvdxl
210
+foabsmjyquwpapeczikghtvdxs
211
+fonbsmjyquwrapeczukghivdxh
212
+fonbsejyoulrapeczikghtvdxl
213
+fonbsmjyquwraceczikgdmvdxl
214
+eonbsmjyquerppeczikghtvdxl
215
+ffnzsmjyquwgapeczikghtvdxl
216
+donbsmyyquwrapeczirghtvdxl
217
+fjnbsmjyqufrapeczikghtwdxl
218
+fonfsmjyquwrareczigghtvdxl
219
+fonusmjyquwrapeczikgetvexl
220
+tonbsmjyqpwrapeczikghtjdxl
221
+fonbsmjhqukkapeczikghtvdxl
222
+fonbsmjyqusraseczikghtvzxl
223
+fonbsmjyquygapeczxkghtvdxl
224
+folbsmjyquwraqeczikghjvdxl
225
+fonbsmjyquwrppecjinghtvdxl
226
+fonbsmjyquwraepczhkghtvdxl
227
+fonbfmjyquwrapeczisghtrdxl
228
+fsnbsmjwqubrapeczikghtvdxl
229
+fonbspjyquwrapjczikghtedxl
230
+fowbsmjyquwrapeczikghtbdbl
231
+fonbymjyquwrapeczikghlvdrl
232
+fonbsmjyruwrapecbikghtvixl
233
+fonyqmjyqufrapeczikghtvdxl
234
+focbscjyquwrapeczmkghtvdxl
235
+fonbsmjyqtwnkpeczikghtvdxl
236
+eonbsmjyquwrameczizghtvdxl
237
+zonbsmjyqcwrapeczikghtvhxl
238
+foubsmjyquwrapehzikghtvnxl
239
+ffnbsmjyquwrapetzikghtjdxl
240
+fonbjgjyquwrapkczikghtvdxl
241
+fonbwmjyquwqapeczdkghtvdxl
242
+forbsmjyquwrapeczikkhtvdml
243
+fonbsmjyiuwrapeczivghevdxl
244
+fonbsmjyquwrapeglikghwvdxl
245
+fopgsmjyquwrapegzikghtvdxl
246
+fonbsmjyqzwrajeczikghtldxl
247
+fonbsmjyruwrapexzmkghtvdxl
248
+fonbsmjyquwrdpeczikxstvdxl
249
+fonbsmjyquwrapeezivghtvdql
250
+fonbdmjyqujsapeczikghtvdxl