Browse Source

Appease Shane.

master
Chris Smith 5 years ago
parent
commit
b05a2e6e57
11 changed files with 88 additions and 333 deletions
  1. 0
    1
      .dockerignore
  2. 0
    1
      .gitignore
  3. 17
    0
      01.py
  4. 27
    0
      02.py
  5. 0
    282
      2018.ipynb
  6. 0
    18
      Dockerfile
  7. 21
    0
      README.md
  8. 11
    0
      docker/Dockerfile
  9. 0
    21
      docker/spliterator.py
  10. 0
    1
      requirements.txt
  11. 12
    9
      run.sh

+ 0
- 1
.dockerignore View File

@@ -1 +0,0 @@
1
-venv

+ 0
- 1
.gitignore View File

@@ -1,3 +1,2 @@
1 1
 /.idea
2
-/.ipynb_checkpoints
3 2
 /venv

+ 17
- 0
01.py View File

@@ -0,0 +1,17 @@
1
+import itertools
2
+
3
+
4
+def first_duplicate(values):
5
+    seen = set()
6
+    for item in values:
7
+        if item in seen:
8
+            return item
9
+        seen.add(item)
10
+    return None
11
+
12
+
13
+with open('data/01.txt', 'r') as file:
14
+    frequencies = list(map(int, file.readlines()))
15
+
16
+    print(sum(frequencies))
17
+    print(first_duplicate(itertools.accumulate(itertools.cycle(frequencies))))

+ 27
- 0
02.py View File

@@ -0,0 +1,27 @@
1
+import itertools
2
+import operator
3
+
4
+
5
+def count_dupes(box):
6
+    return [any(box.count(x) == i for x in box) for i in (2, 3)]
7
+
8
+
9
+with open('data/02.txt', 'r') as file:
10
+    boxes = list(map(str.strip, file.readlines()))
11
+
12
+    print(operator.mul(*map(sum, zip(*map(count_dupes, boxes)))))
13
+
14
+    for box1, box2 in itertools.combinations(boxes, 2):
15
+        common = ''
16
+        missed = 0
17
+        for i, j in zip(box1, box2):
18
+            if i == j:
19
+                common += i
20
+            else:
21
+                missed += 1
22
+                if missed > 1:
23
+                    break
24
+
25
+        if missed == 1:
26
+            print(common)
27
+            break

+ 0
- 282
2018.ipynb View File

@@ -1,282 +0,0 @@
1
-{
2
- "cells": [
3
-  {
4
-   "cell_type": "markdown",
5
-   "metadata": {
6
-    "collapsed": true
7
-   },
8
-   "source": [
9
-    "# Advent of Code 2018\n",
10
-    "\n",
11
-    "This notebook contains my solution to 2018's [Advent of Code](https://adventofcode.com/2018) puzzles. The solutions\n",
12
-    "are all written in Python 3; one or two may require the [NumPy](http://www.numpy.org/) package, but the rest should\n",
13
-    "work out-of-the-box.\n",
14
-    "\n",
15
-    "### Licence\n",
16
-    "\n",
17
-    "To the extent possible under law, I waive all copyright and related or neighboring rights to this work. This work is\n",
18
-    "published from the United Kingdom. See [LICENCE.md](LICENCE.md) for full details.\n",
19
-    "\n",
20
-    "## Common utilities and imports"
21
-   ]
22
-  },
23
-  {
24
-   "cell_type": "code",
25
-   "execution_count": 1,
26
-   "metadata": {
27
-    "day": 0
28
-   },
29
-   "outputs": [],
30
-   "source": [
31
-    "import itertools as it\n",
32
-    "import functools as ft\n",
33
-    "import operator as op\n",
34
-    "\n",
35
-    "\n",
36
-    "def get_input(day):\n",
37
-    "    with open(f'data/{day:02}.txt') as file:\n",
38
-    "        return [l.strip() for l in file.readlines()]"
39
-   ]
40
-  },
41
-  {
42
-   "cell_type": "markdown",
43
-   "metadata": {},
44
-   "source": [
45
-    "## Day 1: Chronal Calibration\n",
46
-    "\n",
47
-    "\"We've detected some temporal anomalies,\" one of Santa's Elves at the Temporal Anomaly Research and Detection Instrument Station tells you. She sounded pretty worried when she called you down here. \"At 500-year intervals into the past, someone has been changing Santa's history!\"\n",
48
-    "\n",
49
-    "\"The good news is that the changes won't propagate to our time stream for another 25 days, and we have a device\" - she attaches something to your wrist - \"that will let you fix the changes with no such propagation delay. It's configured to send you 500 years further into the past every few days; that was the best we could do on such short notice.\"\n",
50
-    "\n",
51
-    "\"The bad news is that we are detecting roughly fifty anomalies throughout time; the device will indicate fixed anomalies with stars. The other bad news is that we only have one device and you're the best person for the job! Good lu--\" She taps a button on the device and you suddenly feel like you're falling. To save Christmas, you need to get all fifty stars by December 25th.\n",
52
-    "\n",
53
-    "Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!\n",
54
-    "\n",
55
-    "After feeling like you've been falling for a few minutes, you look at the device's tiny screen. \"Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock.\" Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.\n",
56
-    "\n",
57
-    "For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:\n",
58
-    "\n",
59
-    "    Current frequency  0, change of +1; resulting frequency  1.\n",
60
-    "    Current frequency  1, change of -2; resulting frequency -1.\n",
61
-    "    Current frequency -1, change of +3; resulting frequency  2.\n",
62
-    "    Current frequency  2, change of +1; resulting frequency  3.\n",
63
-    "\n",
64
-    "In this example, the resulting frequency is 3.\n",
65
-    "\n",
66
-    "Here are other example situations:\n",
67
-    "\n",
68
-    "    +1, +1, +1 results in  3\n",
69
-    "    +1, +1, -2 results in  0\n",
70
-    "    -1, -2, -3 results in -6\n",
71
-    "\n",
72
-    "Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?"
73
-   ]
74
-  },
75
-  {
76
-   "cell_type": "code",
77
-   "execution_count": 2,
78
-   "metadata": {
79
-    "day": 1
80
-   },
81
-   "outputs": [
82
-    {
83
-     "name": "stdout",
84
-     "output_type": "stream",
85
-     "text": [
86
-      "531\n"
87
-     ]
88
-    }
89
-   ],
90
-   "source": [
91
-    "frequencies = list(map(int, get_input(1)))\n",
92
-    "print(sum(frequencies))"
93
-   ]
94
-  },
95
-  {
96
-   "cell_type": "markdown",
97
-   "metadata": {},
98
-   "source": [
99
-    "You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.\n",
100
-    "\n",
101
-    "For example, using the same list of changes above, the device would loop as follows:\n",
102
-    "\n",
103
-    "    Current frequency  0, change of +1; resulting frequency  1.\n",
104
-    "    Current frequency  1, change of -2; resulting frequency -1.\n",
105
-    "    Current frequency -1, change of +3; resulting frequency  2.\n",
106
-    "    Current frequency  2, change of +1; resulting frequency  3.\n",
107
-    "    (At this point, the device continues from the start of the list.)\n",
108
-    "    Current frequency  3, change of +1; resulting frequency  4.\n",
109
-    "    Current frequency  4, change of -2; resulting frequency  2, which has already been seen.\n",
110
-    "\n",
111
-    "In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.\n",
112
-    "\n",
113
-    "Here are other examples:\n",
114
-    "\n",
115
-    "    +1, -1 first reaches 0 twice.\n",
116
-    "    +3, +3, +4, -2, -4 first reaches 10 twice.\n",
117
-    "    -6, +3, +8, +5, -6 first reaches 5 twice.\n",
118
-    "    +7, +7, -2, -7, -4 first reaches 14 twice.\n",
119
-    "\n",
120
-    "What is the first frequency your device reaches twice?"
121
-   ]
122
-  },
123
-  {
124
-   "cell_type": "code",
125
-   "execution_count": 3,
126
-   "metadata": {
127
-    "day": 1
128
-   },
129
-   "outputs": [
130
-    {
131
-     "name": "stdout",
132
-     "output_type": "stream",
133
-     "text": [
134
-      "76787\n"
135
-     ]
136
-    }
137
-   ],
138
-   "source": [
139
-    "def first_duplicate(values):\n",
140
-    "    seen = set()\n",
141
-    "    for item in values:\n",
142
-    "        if item in seen:\n",
143
-    "            return item\n",
144
-    "        seen.add(item)\n",
145
-    "    return None\n",
146
-    "\n",
147
-    "\n",
148
-    "print(first_duplicate(it.accumulate(it.cycle(frequencies))))"
149
-   ]
150
-  },
151
-  {
152
-   "cell_type": "markdown",
153
-   "metadata": {},
154
-   "source": [
155
-    "## Day 2: Inventory Management System\n",
156
-    "\n",
157
-    "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",
158
-    "\n",
159
-    "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",
160
-    "\n",
161
-    "\"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",
162
-    "\n",
163
-    "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",
164
-    "\n",
165
-    "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",
166
-    "\n",
167
-    "For example, if you see the following box IDs:\n",
168
-    "\n",
169
-    "    abcdef contains no letters that appear exactly two or three times.\n",
170
-    "    bababc contains two a and three b, so it counts for both.\n",
171
-    "    abbcde contains two b, but no letter appears exactly three times.\n",
172
-    "    abcccd contains three c, but no letter appears exactly two times.\n",
173
-    "    aabcdd contains two a and two d, but it only counts once.\n",
174
-    "    abcdee contains two e.\n",
175
-    "    ababab contains three a and three b, but it only counts once.\n",
176
-    "\n",
177
-    "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",
178
-    "\n",
179
-    "What is the checksum for your list of box IDs?"
180
-   ]
181
-  },
182
-  {
183
-   "cell_type": "code",
184
-   "execution_count": 4,
185
-   "metadata": {
186
-    "day": 2
187
-   },
188
-   "outputs": [
189
-    {
190
-     "name": "stdout",
191
-     "output_type": "stream",
192
-     "text": [
193
-      "4920\n"
194
-     ]
195
-    }
196
-   ],
197
-   "source": [
198
-    "boxes = list(get_input(2))\n",
199
-    "\n",
200
-    "def count_dupes(str):\n",
201
-    "    return [any(str.count(x) == i for x in str) for i in (2,3)]\n",
202
-    "\n",
203
-    "print(op.mul(*map(sum, zip(*map(count_dupes, boxes)))))"
204
-   ]
205
-  },
206
-  {
207
-   "cell_type": "markdown",
208
-   "metadata": {},
209
-   "source": [
210
-    "Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.\n",
211
-    "\n",
212
-    "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",
213
-    "\n",
214
-    "    abcde\n",
215
-    "    fghij\n",
216
-    "    klmno\n",
217
-    "    pqrst\n",
218
-    "    fguij\n",
219
-    "    axcye\n",
220
-    "    wvxyz\n",
221
-    "\n",
222
-    "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",
223
-    "\n",
224
-    "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.)"
225
-   ]
226
-  },
227
-  {
228
-   "cell_type": "code",
229
-   "execution_count": 5,
230
-   "metadata": {
231
-    "day": 2
232
-   },
233
-   "outputs": [
234
-    {
235
-     "name": "stdout",
236
-     "output_type": "stream",
237
-     "text": [
238
-      "fonbwmjquwtapeyzikghtvdxl\n"
239
-     ]
240
-    }
241
-   ],
242
-   "source": [
243
-    "for box1, box2 in it.combinations(boxes, 2):\n",
244
-    "    common = ''\n",
245
-    "    missed = 0\n",
246
-    "    for i, j in zip(box1, box2):\n",
247
-    "        if i == j:\n",
248
-    "            common += i\n",
249
-    "        else:\n",
250
-    "            missed += 1\n",
251
-    "            if missed > 1:\n",
252
-    "                break\n",
253
-    "    \n",
254
-    "    if missed == 1:\n",
255
-    "        print(common)\n",
256
-    "        break"
257
-   ]
258
-  }
259
- ],
260
- "metadata": {
261
-  "celltoolbar": "Edit Metadata",
262
-  "kernelspec": {
263
-   "display_name": "Python 3",
264
-   "language": "python",
265
-   "name": "python3"
266
-  },
267
-  "language_info": {
268
-   "codemirror_mode": {
269
-    "name": "ipython",
270
-    "version": 3
271
-   },
272
-   "file_extension": ".py",
273
-   "mimetype": "text/x-python",
274
-   "name": "python",
275
-   "nbconvert_exporter": "python",
276
-   "pygments_lexer": "ipython3",
277
-   "version": "3.7.1"
278
-  }
279
- },
280
- "nbformat": 4,
281
- "nbformat_minor": 1
282
-}

+ 0
- 18
Dockerfile View File

@@ -1,18 +0,0 @@
1
-FROM pypy:3
2
-
3
-RUN pip3 install numpy
4
-
5
-ADD docker/entrypoint.sh /entrypoint.sh
6
-ADD 2018.ipynb /code/2018.ipynb
7
-ADD data /code/data
8
-ADD docker/spliterator.py /code/spliterator.py
9
-RUN chmod +x /entrypoint.sh && \
10
-    chown -R nobody /code/ && \
11
-    chmod +x /code/spliterator.py
12
-
13
-USER nobody
14
-
15
-WORKDIR /code
16
-RUN pypy3 /code/spliterator.py
17
-
18
-CMD /entrypoint.sh

+ 21
- 0
README.md View File

@@ -0,0 +1,21 @@
1
+# Advent of Code 2018
2
+
3
+This repository contains my solution to 2018's [Advent of Code](https://adventofcode.com/2018) puzzles. The solutions
4
+are all written in Python 3; one or two may require the [NumPy](http://www.numpy.org/) package, but the rest should
5
+work out-of-the-box.
6
+
7
+If you have docker installed, simply execute `run.sh` to build a docker image and execute the latest solution
8
+using pypy3. You can specify other days by passing the script names as arguments (e.g. `run.sh 03.py`).
9
+
10
+I tend to focus on short, functional solutions where possible, so they may be a bit hard to read. Some solutions are
11
+commented to some degree to help with that.
12
+
13
+I have separate repositories for solutions from previous years:
14
+  - [2017](https://g.c5h.io/archive/aoc-2017)
15
+  - [2016](https://g.c5h.io/archive/aoc-2016)
16
+  - [2015](https://g.c5h.io/archive/aoc-2015)
17
+
18
+---
19
+
20
+To the extent possible under law, I waive all copyright and related or neighboring rights to this work. This work is
21
+published from the United Kingdom. See [LICENCE.md](LICENCE.md) for full details.

+ 11
- 0
docker/Dockerfile View File

@@ -0,0 +1,11 @@
1
+FROM pypy:3
2
+
3
+RUN pip3 install numpy
4
+
5
+ADD entrypoint.sh /entrypoint.sh
6
+RUN chmod +x /entrypoint.sh
7
+
8
+USER nobody
9
+
10
+CMD /entrypoint.sh
11
+VOLUME /code

+ 0
- 21
docker/spliterator.py View File

@@ -1,21 +0,0 @@
1
-import json
2
-
3
-with open('2018.ipynb', 'r') as f:
4
-    notebook = json.load(f)
5
-    common = ''
6
-    days = {}
7
-
8
-    for cell in notebook['cells']:
9
-        if cell['cell_type'] == 'code':
10
-            day = cell['metadata']['day']
11
-            if day == 0:
12
-                common += ''.join(cell['source']) + '\n'
13
-            else:
14
-                if day not in days:
15
-                    days[day] = common
16
-                days[day] += ''.join(cell['source']) + '\n\n'
17
-
18
-    for day, code in days.items():
19
-        with open(f"{day:02}.py", "w") as py:
20
-            print(f'Writing day {day}...')
21
-            py.write(code)

+ 0
- 1
requirements.txt View File

@@ -1 +0,0 @@
1
-jupyter

+ 12
- 9
run.sh View File

@@ -1,11 +1,14 @@
1 1
 #!/bin/bash
2 2
 
3
-IMAGE=csmith/aoc-2018
4
-
5
-echo "Building docker image..."
6
-docker build . -t $IMAGE
7
-
8
-echo "To manually run without rebuilding:"
9
-echo "   docker run --rm -it $IMAGE /entrypoint.sh $@"
10
-
11
-docker run --rm -it $IMAGE /entrypoint.sh $@
3
+IMAGE=csmith/aoc-2018-01
4
+
5
+docker image inspect $IMAGE >/dev/null 2>&1
6
+if [ $? -ne 0 ]
7
+then
8
+    echo "One time setup: building docker image..."
9
+    cd docker
10
+    docker build . -t $IMAGE
11
+    cd ..
12
+fi
13
+
14
+docker run --rm -it -v $(pwd):/code $IMAGE /entrypoint.sh $@