From: Neil Smith <neil.git@njae.me.uk> Date: Sat, 1 Dec 2018 13:20:43 +0000 (+0000) Subject: Initial commit X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=3daf026f8f1a57fee82212aca0872eba0e71b8b4;p=advent-of-code-18.git Initial commit --- 3daf026f8f1a57fee82212aca0872eba0e71b8b4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..593051d --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# Extensionless files +* +!/**/ +!*.* + +# Haskell bits +dist +dist-* +cabal-dev +*.o +*.hi +*.chi +*.chs.h +*.dyn_o +*.dyn_hi +.hpc +.hsenv +.cabal-sandbox/ +cabal.sandbox.config +*.prof +*.aux +*.hp +*.eventlog +.stack-work/ +cabal.project.local +.HTF/ + +# IPython / IHaskell notebook checkpoints +.ipynb* + +# Sublime text +*.sublime-workspace + +# Logs +*.log + +# Profile exports +*.ps + +# KDE +.directory + diff --git a/README.html b/README.html new file mode 100644 index 0000000..8dd29bb --- /dev/null +++ b/README.html @@ -0,0 +1,58 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <meta http-equiv="Content-Style-Type" content="text/css" /> + <meta name="generator" content="pandoc" /> + <title>Advent of Code 2018</title> + <style type="text/css">code{white-space: pre;}</style> + <link rel="stylesheet" href="modest.css" type="text/css" /> +</head> +<body> +<div id="header"> +<h1 class="title">Advent of Code 2018</h1> +</div> +<p>Code to solve the <a href="http://adventofcode.com/2018/">Advent of Code</a> puzzles. This year, I'm using the puzzles to develop my skills in <a href="https://wiki.haskell.org/Haskell">Haskell</a>.</p> +<p><a href="http://learnyouahaskell.com/chapters">Learn you a Haskell</a>, <a href="https://www.haskell.org/tutorial/index.html">Introduction to Haskell 98</a>, and <a href="https://hackage.haskell.org/">Hackage</a> are good resources.</p> +<p>The <a href="https://docs.haskellstack.org/en/stable/README/">Stack documentation</a> and <a href="http://howistart.org/posts/haskell/1/">How I Start: Haskell</a> are good sources of using the tools.</p> +<h1 id="toolchain">Toolchain</h1> +<p>I'm using the basic Haskell Platform installation, togeher with <code>stack</code> to manage the packages and dependencies (install with</p> +<pre><code>$ sudo aptitude install haskell-platform haskell-stack</code></pre> +<p>).</p> +<h2 id="creating-the-repository-and-project">Creating the repository and project</h2> +<p>Create the repository as normal: create the project in Gitolite, clone it, and insert the <code>.gitignore</code> and <code>README.md</code> files.</p> +<p>There's just one package, with the code in sub-directories of the <code>src</code> directory. Each day will generate one (or more) entries in the <code>adventofcode17.cabal</code> file.</p> +<p>Create the basic <code>stack</code> project. This will create a new directory. Note that this new directory name can't have a hyphen-delimited word that's just digits, so the project will have to be <code>advent-of-code</code></p> +<pre><code>stack new advent-of-code --bare simple</code></pre> +<p>Modify the <code>stack.yaml</code> file as needed, such as adding the <code>ghc-options</code> stanza.</p> +<h2 id="creating-subsequent-days">Creating subsequent days</h2> +<p>Each day lives in a separate directory within the <code>src</code> directory. It will also need it's own stanza in <code>advent-of-code.cabal</code>.</p> +<p>Compile with</p> +<pre><code>stack build</code></pre> +<p>or</p> +<pre><code>stack build advent01</code></pre> +<p>Run with</p> +<pre><code>stack exec advent01</code></pre> +<p>Run interactively with</p> +<pre><code>stack ghci advent-of-code:exe:advent01</code></pre> +<p>To profile, use</p> +<pre><code>stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts" adventofcode1601</code></pre> +<p>then run with</p> +<pre><code>stack exec -- advent01 +RTS -p -hy</code></pre> +<h1 id="packages">Packages</h1> +<p>Stack is using the <a href="https://www.stackage.org/lts-12.20">12.20-lts resolver</a> for packages, so make sure you read the <a href="https://www.stackage.org/lts-12.20/docs">correct documentation for the packages included in it</a>.</p> +<p>When you use a new package, use</p> +<pre><code>stack solver</code></pre> +<p>to see how the <code>stack.yaml</code> file needs to change, and</p> +<pre><code>stack solver --update-yaml</code></pre> +<p>to implement the changes.</p> +<h1 id="ihaskell">IHaskell</h1> +<p>Install following the <a href="https://github.com/gibiansky/IHaskell">IHaskell instructions</a>.</p> +<p>Run it with</p> +<pre><code>stack exec jupyter -- notebook</code></pre> +<h1 id="readme">Readme</h1> +<p>Build this readme file wth</p> +<pre><code>pandoc -s README.md > README.html</code></pre> +<p>(Using the <a href="https://github.com/markdowncss/modest">Modest style</a>.)</p> +</body> +</html> diff --git a/README.md b/README.md new file mode 100644 index 0000000..e268bd4 --- /dev/null +++ b/README.md @@ -0,0 +1,97 @@ +--- +title: "Advent of Code 2018" +output: html_document +css: modest.css +--- +Code to solve the [Advent of Code](http://adventofcode.com/2018/) puzzles. This year, I'm using the puzzles to develop my skills in [Haskell](https://wiki.haskell.org/Haskell). + +[Learn you a Haskell](http://learnyouahaskell.com/chapters), [Introduction to Haskell 98](https://www.haskell.org/tutorial/index.html), and [Hackage](https://hackage.haskell.org/) are good resources. + +The [Stack documentation](https://docs.haskellstack.org/en/stable/README/) and [How I Start: Haskell](http://howistart.org/posts/haskell/1/) are good sources of using the tools. + +# Toolchain + +I'm using the basic Haskell Platform installation, togeher with `stack` to manage the packages and dependencies (install with +``` +$ sudo aptitude install haskell-platform haskell-stack +``` +). + +## Creating the repository and project +Create the repository as normal: create the project in Gitolite, clone it, and insert the `.gitignore` and `README.md` files. + +There's just one package, with the code in sub-directories of the `src` directory. Each day will generate one (or more) entries in the `adventofcode17.cabal` file. + +Create the basic `stack` project. This will create a new directory. Note that this new directory name can't have a hyphen-delimited word that's just digits, so the project will have to be `advent-of-code` + +``` +stack new advent-of-code --bare simple +``` + +Modify the `stack.yaml` file as needed, such as adding the `ghc-options` stanza. + +## Creating subsequent days + +Each day lives in a separate directory within the `src` directory. It will also need it's own stanza in `advent-of-code.cabal`. + +Compile with +``` +stack build +``` +or +``` +stack build advent01 +``` + +Run with +``` +stack exec advent01 +``` + +Run interactively with +``` +stack ghci advent-of-code:exe:advent01 +``` + +To profile, use +``` +stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts" adventofcode1601 +``` +then run with +``` +stack exec -- advent01 +RTS -p -hy +``` + +# Packages + +Stack is using the [12.20-lts resolver](https://www.stackage.org/lts-12.20) for packages, so make sure you read the [correct documentation for the packages included in it](https://www.stackage.org/lts-12.20/docs). + +When you use a new package, use + +``` +stack solver +``` +to see how the `stack.yaml` file needs to change, and +``` +stack solver --update-yaml +``` +to implement the changes. + +# IHaskell + +Install following the [IHaskell instructions](https://github.com/gibiansky/IHaskell). + +Run it with + +``` +stack exec jupyter -- notebook +``` + +# Readme + +Build this readme file wth +``` +pandoc -s README.md > README.html +``` + +(Using the [Modest style](https://github.com/markdowncss/modest).) diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/advent-of-code-18.sublime-project b/advent-of-code-18.sublime-project new file mode 100644 index 0000000..24db303 --- /dev/null +++ b/advent-of-code-18.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "." + } + ] +} diff --git a/advent-of-code.cabal b/advent-of-code.cabal new file mode 100644 index 0000000..e1d7dc8 --- /dev/null +++ b/advent-of-code.cabal @@ -0,0 +1,29 @@ +name: advent-of-code +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/neilnjae/advent-of-code#readme +license: BSD3 +license-file: LICENSE +author: Neil Smith +maintainer: noone@njae.me.uk +copyright: 2018 Neil Smith +category: None +build-type: Simple +cabal-version: >=1.10 +extra-source-files: README.md + +executable advent-of-code + hs-source-dirs: src + main-is: Main.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + +executable advent01 + hs-source-dirs: src/advent01 + main-is: advent01.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , text + , megaparsec + , containers diff --git a/data/advent01.txt b/data/advent01.txt new file mode 100644 index 0000000..f907f85 --- /dev/null +++ b/data/advent01.txt @@ -0,0 +1,1000 @@ +-16 ++12 +-18 +-1 ++5 +-8 ++9 +-15 ++12 ++6 ++11 ++7 +-9 ++13 ++5 +-4 +-4 +-2 +-5 ++19 ++4 ++14 ++7 ++8 +-16 +-9 ++16 ++8 +-11 +-7 ++12 ++8 ++13 ++11 ++12 +-19 ++11 ++7 ++9 +-7 +-16 +-5 ++11 +-1 ++8 ++5 ++12 +-1 +-1 ++6 +-2 +-12 ++6 +-18 ++11 ++5 ++13 +-12 +-15 +-8 +-13 ++2 +-11 ++1 +-14 +-6 ++11 +-15 ++11 ++8 ++18 +-8 ++18 +-7 +-9 +-24 +-12 ++2 ++20 +-9 +-5 +-14 +-6 ++16 +-1 +-12 +-16 ++8 ++9 +-15 +-8 +-2 +-4 +-16 +-18 +-8 ++10 ++10 ++2 ++3 +-8 +-10 +-1 +-2 ++20 +-5 +-4 +-13 ++10 ++9 +-12 +-19 ++15 +-4 +-13 +-11 +-9 +-4 +-12 ++3 +-7 +-4 ++13 ++8 +-5 ++10 +-11 ++7 ++10 ++13 ++10 +-17 +-21 ++11 ++3 ++9 +-15 +-11 ++15 ++10 +-3 ++17 ++6 ++11 ++16 ++19 ++8 ++8 ++4 ++9 ++7 ++15 ++2 ++15 ++22 ++19 ++11 ++3 ++19 ++9 +-4 ++9 +-19 +-16 ++6 ++2 +-3 ++5 ++5 +-18 ++1 +-14 +-6 +-20 ++10 +-13 ++19 +-18 +-17 ++5 +-39 ++2 ++33 ++2 ++24 +-16 ++45 ++9 ++21 ++6 ++20 ++4 +-12 +-7 +-3 ++8 ++12 ++5 +-1 ++8 ++18 +-13 +-9 ++3 ++16 +-4 ++15 ++13 +-10 +-8 +-13 +-21 ++15 ++17 ++13 +-4 +-18 +-15 ++3 ++11 ++5 ++3 ++10 +-11 ++9 ++19 +-2 ++9 ++3 +-13 ++2 +-7 +-14 +-2 +-7 +-19 ++7 ++3 +-13 ++6 ++10 ++5 +-9 +-20 +-8 ++5 +-17 ++11 +-12 ++17 +-12 +-14 ++24 +-22 +-13 ++15 ++15 ++1 +-4 ++13 +-30 ++12 ++17 ++8 ++45 ++4 +-3 +-5 ++3 +-9 ++13 ++8 ++14 ++2 +-12 +-3 ++9 ++20 ++7 ++1 ++1 +-17 +-10 ++3 +-2 ++4 +-6 ++16 +-8 ++15 ++16 ++8 +-11 +-14 +-4 ++2 ++15 ++19 ++12 ++14 ++17 +-18 +-18 +-18 +-1 +-1 +-7 +-17 +-2 +-18 ++15 +-3 ++5 +-20 ++39 +-1 ++6 ++8 ++10 ++16 +-19 ++11 ++19 ++15 +-6 ++19 +-8 ++6 ++1 +-2 +-20 +-4 ++18 ++2 +-17 +-10 +-10 +-12 ++18 ++2 +-16 +-8 ++14 +-15 ++12 +-14 +-19 +-16 +-21 +-17 ++21 +-12 +-34 +-22 +-25 +-15 ++14 +-12 +-10 ++11 +-20 ++4 +-6 +-11 ++14 ++21 ++74 ++40 ++3 ++15 +-4 ++16 ++12 ++3 ++6 ++2 ++11 ++10 +-3 ++17 +-5 ++10 ++13 ++5 +-13 +-4 ++13 +-8 ++17 +-2 ++6 +-12 ++7 ++2 ++13 +-2 +-9 ++18 ++15 +-11 ++4 ++3 +-21 +-17 ++3 ++12 +-1 +-12 +-3 +-20 +-10 ++13 +-6 +-4 +-20 ++7 ++15 +-6 +-15 +-26 ++13 ++6 +-23 +-9 +-1 ++44 ++8 +-14 ++26 ++13 ++5 ++31 ++24 ++7 +-2 ++7 +-4 ++17 +-16 +-3 ++36 +-10 +-22 +-11 ++62 +-7 ++21 +-17 +-10 +-18 ++11 ++4 ++5 +-23 +-26 +-40 ++2 ++32 ++62 ++23 ++45 ++109 +-37 +-10 +-99 +-210 +-21 +-33 ++379 ++262 ++66 ++66407 ++13 +-8 +-14 +-16 +-6 +-19 ++12 ++15 +-10 +-19 +-8 ++12 ++13 +-11 ++18 ++7 +-11 ++14 +-12 +-11 +-2 ++4 +-19 +-9 ++3 +-5 ++7 +-4 ++9 +-19 ++20 ++7 +-14 +-2 +-7 +-8 +-2 +-16 ++8 +-10 ++1 +-4 +-16 ++2 ++1 +-13 +-2 +-3 +-16 ++5 +-6 +-17 ++3 ++9 ++14 ++1 +-11 ++6 +-2 +-12 +-10 ++11 ++19 +-9 ++5 +-11 +-8 +-17 +-19 ++8 +-3 +-17 ++13 ++14 +-16 +-8 ++19 ++17 +-16 +-16 +-13 +-11 ++9 +-19 ++6 ++12 ++5 +-12 +-12 +-16 +-14 +-6 ++7 ++8 +-12 +-18 ++12 ++13 +-17 ++3 +-5 ++1 ++6 +-4 +-18 ++1 ++11 ++2 +-8 +-15 +-17 ++16 +-10 +-12 ++16 +-14 +-1 +-11 ++5 ++14 ++20 ++16 +-18 ++16 ++17 ++13 +-9 +-6 +-16 ++19 ++13 ++8 +-13 ++1 ++19 +-12 +-20 +-20 +-11 +-12 ++4 ++12 ++6 +-17 +-9 +-3 +-18 +-15 +-2 +-5 ++17 ++12 +-8 ++4 +-12 +-7 +-18 +-4 +-1 +-19 ++1 +-7 +-8 ++5 ++1 ++19 +-7 +-19 ++18 ++12 ++6 ++19 ++8 +-1 ++20 +-17 +-16 +-5 +-1 +-3 ++6 +-13 +-2 ++6 +-8 ++12 +-13 +-3 +-13 ++8 ++4 +-16 ++19 ++18 ++16 +-13 +-1 ++3 ++9 ++18 ++1 ++3 ++14 ++10 ++3 ++1 +-8 ++18 ++4 ++26 ++22 ++13 +-12 +-7 +-3 ++6 +-17 ++5 ++14 +-7 ++12 ++10 +-7 ++8 +-7 ++18 ++17 +-6 ++12 ++17 ++2 ++3 +-9 ++18 ++10 +-17 +-10 +-2 +-13 ++6 ++4 ++17 ++12 ++14 +-15 ++5 ++17 ++4 +-1 +-14 +-19 +-1 ++5 +-15 +-16 ++1 +-11 +-13 +-5 ++15 +-4 +-15 ++1 ++22 ++17 +-20 +-21 ++13 ++1 +-2 ++17 ++23 +-35 +-14 +-18 ++6 ++1 ++1 +-13 +-10 ++34 ++35 ++8 ++4 ++14 ++24 ++16 +-5 +-2 ++10 ++16 +-10 +-11 ++1 +-2 +-3 ++7 ++5 ++20 +-14 ++2 ++2 +-5 ++14 +-18 ++23 +-3 ++2 ++4 ++2 ++7 ++8 ++3 +-16 +-5 ++4 ++12 +-20 +-15 +-22 ++5 +-10 ++31 +-2 ++9 ++10 +-14 ++18 ++6 ++3 ++19 +-15 ++9 ++7 ++18 +-1 +-8 ++19 ++12 ++1 ++16 +-5 +-4 +-9 ++16 ++13 ++2 ++7 ++5 +-3 +-19 ++12 ++9 ++2 ++9 ++16 +-17 +-13 ++12 +-5 ++11 ++4 +-1 ++2 +-6 ++18 ++19 +-10 ++3 +-19 +-19 +-13 +-16 +-2 +-13 ++8 ++18 ++6 +-11 ++2 +-16 ++11 ++12 +-20 ++18 ++17 ++14 +-13 ++26 +-1 ++20 +-1 +-17 +-11 +-8 ++13 +-7 +-5 +-5 +-7 ++18 +-20 +-4 ++29 +-2 ++21 ++21 ++16 ++15 ++14 ++21 +-15 +-14 ++17 ++5 +-10 +-27 +-18 ++10 +-20 ++34 +-13 ++18 ++17 +-21 ++32 ++13 ++6 ++6 ++28 +-37 ++20 ++29 +-25 +-69 +-57 +-3 +-59 ++3 +-21 +-9 ++14 +-19 +-18 ++20 +-5 ++15 ++5 +-9 +-24 ++20 +-14 +-10 +-15 ++30 ++21 +-9 +-34 ++4 +-31 ++25 +-85 ++25 ++38 +-36 +-28 ++58 ++121 +-31 ++13 ++202 ++66221 +-5 +-19 +-10 +-7 +-8 +-2 ++6 +-5 +-6 ++8 +-17 ++10 ++18 ++16 ++3 +-12 +-2 +-11 ++14 ++7 ++11 +-1 +-5 ++10 ++9 +-5 +-3 +-3 +-11 +-13 ++18 +-8 +-5 ++18 ++17 +-6 ++3 ++19 +-18 ++5 ++5 +-4 +-12 ++7 ++14 ++19 +-6 ++10 +-8 +-11 ++10 +-17 ++9 ++11 ++7 +-133358 diff --git a/modest.css b/modest.css new file mode 100644 index 0000000..947a9ea --- /dev/null +++ b/modest.css @@ -0,0 +1,219 @@ +@media print { + *, + *:before, + *:after { + background: transparent !important; + color: #000 !important; + box-shadow: none !important; + text-shadow: none !important; + } + + a, + a:visited { + text-decoration: underline; + } + + a[href]:after { + content: " (" attr(href) ")"; + } + + abbr[title]:after { + content: " (" attr(title) ")"; + } + + a[href^="#"]:after, + a[href^="javascript:"]:after { + content: ""; + } + + pre, + blockquote { + border: 1px solid #999; + page-break-inside: avoid; + } + + thead { + display: table-header-group; + } + + tr, + img { + page-break-inside: avoid; + } + + img { + max-width: 100% !important; + } + + p, + h2, + h3 { + orphans: 3; + widows: 3; + } + + h2, + h3 { + page-break-after: avoid; + } +} + +pre, +code { + font-family: Menlo, Monaco, "Courier New", monospace; +} + +pre { + padding: .5rem; + line-height: 1.25; + overflow-x: scroll; +} + +a, +a:visited { + color: #3498db; +} + +a:hover, +a:focus, +a:active { + color: #2980b9; +} + +.modest-no-decoration { + text-decoration: none; +} + +html { + font-size: 12px; +} + +@media screen and (min-width: 32rem) and (max-width: 48rem) { + html { + font-size: 15px; + } +} + +@media screen and (min-width: 48rem) { + html { + font-size: 16px; + } +} + +body { + line-height: 1.85; +} + +p, +.modest-p { + font-size: 1rem; + margin-bottom: 1.3rem; +} + +h1, +.modest-h1, +h2, +.modest-h2, +h3, +.modest-h3, +h4, +.modest-h4 { + margin: 1.414rem 0 .5rem; + font-weight: inherit; + line-height: 1.42; +} + +h1, +.modest-h1 { + margin-top: 0; + font-size: 3.998rem; +} + +h2, +.modest-h2 { + font-size: 2.827rem; +} + +h3, +.modest-h3 { + font-size: 1.999rem; +} + +h4, +.modest-h4 { + font-size: 1.414rem; +} + +h5, +.modest-h5 { + font-size: 1.121rem; +} + +h6, +.modest-h6 { + font-size: .88rem; +} + +small, +.modest-small { + font-size: .707em; +} + +/* https://github.com/mrmrs/fluidity */ + +img, +canvas, +iframe, +video, +svg, +select, +textarea { + max-width: 100%; +} + +@import url(http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,300italic,700); + +@import url(http://fonts.googleapis.com/css?family=Arimo:700,700italic); + +html { + font-size: 18px; + max-width: 100%; +} + +body { + color: #444; + font-family: 'Open Sans Condensed', sans-serif; + font-weight: 300; + margin: 0 auto; + max-width: 48rem; + line-height: 1.45; + padding: .25rem; +} + +h1, +h2, +h3, +h4, +h5, +h6 { + font-family: Arimo, Helvetica, sans-serif; +} + +h1, +h2, +h3 { + border-bottom: 2px solid #fafafa; + margin-bottom: 1.15rem; + padding-bottom: .5rem; + text-align: center; +} + +blockquote { + border-left: 8px solid #fafafa; + padding: 1rem; +} + +pre, +code { + background-color: #fafafa; +} \ No newline at end of file diff --git a/problems/day01.html b/problems/day01.html new file mode 100644 index 0000000..8730f31 --- /dev/null +++ b/problems/day01.html @@ -0,0 +1,159 @@ +<!DOCTYPE html> +<html lang="en-us"> +<head> +<meta charset="utf-8"/> +<title>Day 1 - Advent of Code 2018</title> +<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]--> +<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'> +<link rel="stylesheet" type="text/css" href="/static/style.css?16"/> +<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/> +<link rel="shortcut icon" href="/favicon.png"/> +</head><!-- + + + + +Oh, hello! Funny seeing you here. + +I appreciate your enthusiasm, but you aren't going to find much down here. +There certainly aren't clues to any of the puzzles. The best surprises don't +even appear in the source until you unlock them for real. + +Please be careful with automated requests; I'm not Google, and I can only take +so much traffic. Please be considerate so that everyone gets to play. + +If you're curious about how Advent of Code works, it's running on some custom +Perl code. Other than a few integrations (auth, analytics, ads, social media), +I built the whole thing myself, including the design, animations, prose, and +all of the puzzles. + +The puzzles are most of the work; the easiest ones take 3-4 hours each, but the +harder ones take 6-8 hours, and a few even longer than that. A lot of effort +went into building this thing - I hope you're enjoying playing it as much as I +enjoyed making it for you! + +If you'd like to hang out, I'm @ericwastl on Twitter. + +- Eric Wastl + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +--> +<body> +<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2018/about">[About]</a></li><li><a href="/2018/events">[Events]</a></li><li><a href="https://teespring.com/adventofcode" target="_blank">[Shop]</a></li><li><a href="/2018/settings">[Settings]</a></li><li><a href="/2018/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <span class="star-count">2*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap"><y></span><a href="/2018">2018</a><span class="title-event-wrap"></y></span></h1><nav><ul><li><a href="/2018">[Calendar]</a></li><li><a href="/2018/support">[AoC++]</a></li><li><a href="/2018/sponsors">[Sponsors]</a></li><li><a href="/2018/leaderboard">[Leaderboard]</a></li><li><a href="/2018/stats">[Stats]</a></li></ul></nav></div></header> + +<div id="sidebar"> +<div id="sponsor"><div class="quiet">Our <a href="/2018/sponsors">sponsors</a> help make Advent of Code possible:</div><p><a href="https://www.wearedevelopers.com/world-congress/" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);" rel="noopener">WeAreDevelopers</a> - Use "AOC-25", save EUR 25 and join 10^4 devs on June 6-7 at the WeAreDevelopers World Congress in Berlin ticket.get(now)</p></div> +</div><!--/sidebar--> + +<main> +<article class="day-desc"><h2>--- Day 1: Chronal Calibration ---</h2><p>"We've detected some temporal anomalies," one of Santa's Elves at the <span title="It's about as big on the inside as you expected.">Temporal Anomaly Research and Detection Instrument Station</span> 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!"</p> +<p>"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."</p> +<p>"The bad news is that we are detecting roughly <em>fifty</em> anomalies throughout time; the device will indicate fixed anomalies with <em class="star">stars</em>. 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 <em class="star">fifty stars</em> by December 25th.</p> +<p>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 <em class="star">one star</em>. Good luck!</p> +<p>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 <code>+6</code> means the current frequency increases by <code>6</code>; a value like <code>-3</code> means the current frequency decreases by <code>3</code>.</p> +<p>For example, if the device displays frequency changes of <code>+1, -2, +3, +1</code>, then starting from a frequency of zero, the following changes would occur:</p> +<ul> +<li>Current frequency <code> 0</code>, change of <code>+1</code>; resulting frequency <code> 1</code>.</li> +<li>Current frequency <code> 1</code>, change of <code>-2</code>; resulting frequency <code>-1</code>.</li> +<li>Current frequency <code>-1</code>, change of <code>+3</code>; resulting frequency <code> 2</code>.</li> +<li>Current frequency <code> 2</code>, change of <code>+1</code>; resulting frequency <code> 3</code>.</li> +</ul> +<p>In this example, the resulting frequency is <code>3</code>.</p> +<p>Here are other example situations:</p> +<ul> +<li><code>+1, +1, +1</code> results in <code> 3</code></li> +<li><code>+1, +1, -2</code> results in <code> 0</code></li> +<li><code>-1, -2, -3</code> results in <code>-6</code></li> +</ul> +<p>Starting with a frequency of zero, <em>what is the resulting frequency</em> after all of the changes in frequency have been applied?</p> +</article> +<p>Your puzzle answer was <code>472</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>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 <em>twice</em>.</p> +<p>For example, using the same list of changes above, the device would loop as follows:</p> +<ul> +<li>Current frequency <code> 0</code>, change of <code>+1</code>; resulting frequency <code> 1</code>.</li> +<li>Current frequency <code> 1</code>, change of <code>-2</code>; resulting frequency <code>-1</code>.</li> +<li>Current frequency <code>-1</code>, change of <code>+3</code>; resulting frequency <code> 2</code>.</li> +<li>Current frequency <code> 2</code>, change of <code>+1</code>; resulting frequency <code> 3</code>.</li> +<li>(At this point, the device continues from the start of the list.)</li> +<li>Current frequency <code> 3</code>, change of <code>+1</code>; resulting frequency <code> 4</code>.</li> +<li>Current frequency <code> 4</code>, change of <code>-2</code>; resulting frequency <code> 2</code>, which has already been seen.</li> +</ul> +<p>In this example, the first frequency reached twice is <code>2</code>. 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.</p> +<p>Here are other examples:</p> +<ul> +<li><code>+1, -1</code> first reaches <code>0</code> twice.</li> +<li><code>+3, +3, +4, -2, -4</code> first reaches <code>10</code> twice.</li> +<li><code>-6, +3, +8, +5, -6</code> first reaches <code>5</code> twice.</li> +<li><code>+7, +7, -2, -7, -4</code> first reaches <code>14</code> twice.</li> +</ul> +<p><em>What is the first frequency your device reaches twice?</em></p> +</article> +<p>Your puzzle answer was <code>66932</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p> +<p>At this point, you should <a href="/2018">return to your advent calendar</a> and try another puzzle.</p> +<p>If you still want to see it, you can <a href="1/input" target="_blank">get your puzzle input</a>.</p> +<p>You can also <span class="share">[Share<span class="share-content">on + <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Chronal+Calibration%22+%2D+Day+1+%2D+Advent+of+Code+2018&url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F1&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a> + <a href="http://www.reddit.com/submit?url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F1&title=I%27ve+completed+%22Chronal+Calibration%22+%2D+Day+1+%2D+Advent+of+Code+2018" target="_blank">Reddit</a +></span>]</span> this puzzle.</p> +</main> + +<!-- ga --> +<script> +(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ +(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), +m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) +})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); +ga('create', 'UA-69522494-1', 'auto'); +ga('send', 'pageview'); +</script> +<!-- /ga --> +</body> +</html> \ No newline at end of file diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..9cd992d --- /dev/null +++ b/src/Main.hs @@ -0,0 +1,5 @@ +module Main where + +main :: IO () +main = do + putStrLn "hello world" diff --git a/src/advent01/advent01.hs b/src/advent01/advent01.hs new file mode 100644 index 0000000..3b7a71c --- /dev/null +++ b/src/advent01/advent01.hs @@ -0,0 +1,61 @@ +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE OverloadedStrings #-} + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import Data.IntSet (IntSet) +import qualified Data.IntSet as IntSet + +main :: IO () +main = do + text <- TIO.readFile "data/advent01.txt" + let changes = successfulParse text + print $ part1 changes + print $ part2 changes + + +part1 :: [Int] -> Int +part1 = sum + +part2 :: [Int] -> Int +part2 changes = snd $ head $ dropWhile unRepeated $ scanl merge (IntSet.empty, 0) $ cycle changes + + +merge :: (IntSet, Int) -> Int -> (IntSet, Int) +merge (s, f) c = (IntSet.insert f s, f+c) + +unRepeated :: (IntSet, Int) -> Bool +unRepeated (s, f) = f `IntSet.notMember` s + +-- Parse the input file +type Parser = Parsec Void Text + +sc :: Parser () +-- sc = L.space (skipSome spaceChar) CA.empty CA.empty +sc = L.space (skipSome (char ' ')) CA.empty CA.empty + + +lexeme = L.lexeme sc +integer = lexeme L.decimal +signedInteger = L.signed sc integer + +-- symb = L.symbol sc +-- comma = symb "," + + +changesP = signedInteger `sepEndBy` newline + + +successfulParse :: Text -> [Int] +successfulParse input = + case parse changesP "input" input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right changes -> changes \ No newline at end of file diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..889be00 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,68 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# https://docs.haskellstack.org/en/stable/yaml_configuration/ + +# Resolver to choose a 'specific' stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# resolver: lts-3.5 +# resolver: nightly-2015-09-21 +# resolver: ghc-7.10.2 +# resolver: ghcjs-0.1.0_ghc-7.10.2 +# +# The location of a snapshot can be provided as a file or url. Stack assumes +# a snapshot provided as a file might change, whereas a url resource does not. +# +# resolver: ./custom-snapshot.yaml +# resolver: https://example.com/snapshots/2018-01-01.yaml +resolver: lts-12.20 + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# - location: +# git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# subdirs: +# - auto-update +# - wai +packages: +- . +# Dependency packages to be pulled from upstream that are not in the resolver +# using the same syntax as the packages field. +# (e.g., acme-missiles-0.3) +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=1.7" +# +# Override the architecture used by stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor + +ghc-options: + $locals: -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N \ No newline at end of file