Browse Source

Day 1

master
Chris Smith 5 years ago
commit
10e27d1a26
5 changed files with 1257 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 170
    0
      2018.ipynb
  3. 46
    0
      LICENCE.md
  4. 1037
    0
      data/01.txt
  5. 1
    0
      requirements.txt

+ 3
- 0
.gitignore View File

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

+ 170
- 0
2018.ipynb View File

@@ -0,0 +1,170 @@
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
+   "outputs": [],
28
+   "source": [
29
+    "import itertools\n",
30
+    "\n",
31
+    "\n",
32
+    "def get_input(day):\n",
33
+    "    with open(f'data/{day:02}.txt') as file:\n",
34
+    "        return file.readlines()"
35
+   ]
36
+  },
37
+  {
38
+   "cell_type": "markdown",
39
+   "metadata": {},
40
+   "source": [
41
+    "## Day 1: Chronal Calibration\n",
42
+    "\n",
43
+    "\"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",
44
+    "\n",
45
+    "\"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",
46
+    "\n",
47
+    "\"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",
48
+    "\n",
49
+    "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",
50
+    "\n",
51
+    "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",
52
+    "\n",
53
+    "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",
54
+    "\n",
55
+    "    Current frequency  0, change of +1; resulting frequency  1.\n",
56
+    "    Current frequency  1, change of -2; resulting frequency -1.\n",
57
+    "    Current frequency -1, change of +3; resulting frequency  2.\n",
58
+    "    Current frequency  2, change of +1; resulting frequency  3.\n",
59
+    "\n",
60
+    "In this example, the resulting frequency is 3.\n",
61
+    "\n",
62
+    "Here are other example situations:\n",
63
+    "\n",
64
+    "    +1, +1, +1 results in  3\n",
65
+    "    +1, +1, -2 results in  0\n",
66
+    "    -1, -2, -3 results in -6\n",
67
+    "\n",
68
+    "Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?"
69
+   ]
70
+  },
71
+  {
72
+   "cell_type": "code",
73
+   "execution_count": 2,
74
+   "metadata": {},
75
+   "outputs": [
76
+    {
77
+     "data": {
78
+      "text/plain": [
79
+       "531"
80
+      ]
81
+     },
82
+     "execution_count": 2,
83
+     "metadata": {},
84
+     "output_type": "execute_result"
85
+    }
86
+   ],
87
+   "source": [
88
+    "sum(map(int, get_input(1)))"
89
+   ]
90
+  },
91
+  {
92
+   "cell_type": "markdown",
93
+   "metadata": {},
94
+   "source": [
95
+    "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",
96
+    "\n",
97
+    "For example, using the same list of changes above, the device would loop as follows:\n",
98
+    "\n",
99
+    "    Current frequency  0, change of +1; resulting frequency  1.\n",
100
+    "    Current frequency  1, change of -2; resulting frequency -1.\n",
101
+    "    Current frequency -1, change of +3; resulting frequency  2.\n",
102
+    "    Current frequency  2, change of +1; resulting frequency  3.\n",
103
+    "    (At this point, the device continues from the start of the list.)\n",
104
+    "    Current frequency  3, change of +1; resulting frequency  4.\n",
105
+    "    Current frequency  4, change of -2; resulting frequency  2, which has already been seen.\n",
106
+    "\n",
107
+    "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",
108
+    "\n",
109
+    "Here are other examples:\n",
110
+    "\n",
111
+    "    +1, -1 first reaches 0 twice.\n",
112
+    "    +3, +3, +4, -2, -4 first reaches 10 twice.\n",
113
+    "    -6, +3, +8, +5, -6 first reaches 5 twice.\n",
114
+    "    +7, +7, -2, -7, -4 first reaches 14 twice.\n",
115
+    "\n",
116
+    "What is the first frequency your device reaches twice?"
117
+   ]
118
+  },
119
+  {
120
+   "cell_type": "code",
121
+   "execution_count": 3,
122
+   "metadata": {},
123
+   "outputs": [
124
+    {
125
+     "data": {
126
+      "text/plain": [
127
+       "76787"
128
+      ]
129
+     },
130
+     "execution_count": 3,
131
+     "metadata": {},
132
+     "output_type": "execute_result"
133
+    }
134
+   ],
135
+   "source": [
136
+    "def first_duplicate(values):\n",
137
+    "    seen = set()\n",
138
+    "    for item in values:\n",
139
+    "        if item in seen:\n",
140
+    "            return item\n",
141
+    "        seen.add(item)\n",
142
+    "    return None\n",
143
+    "\n",
144
+    "\n",
145
+    "first_duplicate(itertools.accumulate(itertools.cycle(map(int, get_input(1)))))"
146
+   ]
147
+  }
148
+ ],
149
+ "metadata": {
150
+  "kernelspec": {
151
+   "display_name": "Python 3",
152
+   "language": "python",
153
+   "name": "python3"
154
+  },
155
+  "language_info": {
156
+   "codemirror_mode": {
157
+    "name": "ipython",
158
+    "version": 3
159
+   },
160
+   "file_extension": ".py",
161
+   "mimetype": "text/x-python",
162
+   "name": "python",
163
+   "nbconvert_exporter": "python",
164
+   "pygments_lexer": "ipython3",
165
+   "version": "3.7.1"
166
+  }
167
+ },
168
+ "nbformat": 4,
169
+ "nbformat_minor": 1
170
+}

+ 46
- 0
LICENCE.md View File

@@ -0,0 +1,46 @@
1
+# CC0 1.0 Universal
2
+
3
+```
4
+CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES
5
+NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE
6
+COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND
7
+DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER.
8
+```
9
+
10
+### Statement of Purpose
11
+
12
+The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").
13
+
14
+Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others.
15
+
16
+For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights.
17
+
18
+1. __Copyright and Related Rights.__ A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following:
19
+
20
+    i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work;
21
+
22
+    ii. moral rights retained by the original author(s) and/or performer(s);
23
+
24
+    iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work;
25
+
26
+    iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below;
27
+
28
+    v. rights protecting the extraction, dissemination, use and reuse of data in a Work;
29
+
30
+    vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and
31
+
32
+    vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof.
33
+
34
+2. __Waiver.__ To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose.
35
+
36
+3. __Public License Fallback.__ Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose.
37
+
38
+4. __Limitations and Disclaimers.__
39
+
40
+    a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document.
41
+
42
+    b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.
43
+
44
+    c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work.
45
+
46
+    d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work.

+ 1037
- 0
data/01.txt
File diff suppressed because it is too large
View File


+ 1
- 0
requirements.txt View File

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