From 73861022e114fe3b2d6625301f4ac5fc973a8a6e Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 1 Dec 2019 09:00:57 +0000 Subject: [PATCH] Done day 1 --- README.html | 64 ++ README.md | 2 +- advent-of-code.sublime-project | 8 + advent01/package.yaml | 43 ++ advent01/src/advent01.hs | 25 +- data/advent01.txt | 1100 +++----------------------------- problems/day01.html | 143 +++++ stack.yaml | 1 + 8 files changed, 371 insertions(+), 1015 deletions(-) create mode 100644 README.html create mode 100644 advent-of-code.sublime-project create mode 100644 problems/day01.html diff --git a/README.html b/README.html new file mode 100644 index 0000000..218a7f9 --- /dev/null +++ b/README.html @@ -0,0 +1,64 @@ + + + + + + + Advent of Code 2018 + + + + + +

Code to solve the Advent of Code puzzles. This year, I'm using the puzzles to develop my skills in Haskell.

+

Learn you a Haskell, Introduction to Haskell 98, and Hackage are good resources.

+

The Stack documentation and How I Start: Haskell are good sources of using the tools.

+

Toolchain

+

I'm using the basic Haskell Platform installation, together with stack to manage the packages and dependencies (install with

+
$ sudo aptitude install haskell-platform haskell-stack
+

), then updgrade with

+
 stack upgrade --binary-only
+

as the version in the Ubuntu repos is too old to work with current Haskell Stack package sets. ##

+

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
+

If you want to pass in additional RTS parameters, do it like this:

+
stack exec -- advent01 +RTS -K0 -RTS
+

Run interactively with

+
stack ghci advent-of-code:exe:advent01
+

To profile, use

+
stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts"
+

then run with

+
stack exec -- advent01 +RTS -p -hy
+

Generate the profile graph with

+
stack exec hp2ps advent01.hp
+

Packages

+

Stack is using the 14.16-lts resolver for packages, so make sure you read the correct documentation for the packages included in it.

+

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.

+

Run it with

+
stack exec jupyter -- notebook
+

Readme

+

Build this readme file wth

+
pandoc -s README.md > README.html
+

(Using the Modest style.)

+ + diff --git a/README.md b/README.md index aa3689e..1d363c8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ --- -title: "Advent of Code 2018" +title: "Advent of Code 2019" output: html_document css: modest.css --- diff --git a/advent-of-code.sublime-project b/advent-of-code.sublime-project new file mode 100644 index 0000000..24db303 --- /dev/null +++ b/advent-of-code.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "." + } + ] +} diff --git a/advent01/package.yaml b/advent01/package.yaml index 3002417..448035a 100644 --- a/advent01/package.yaml +++ b/advent01/package.yaml @@ -5,6 +5,49 @@ name: advent01 synopsis: Advent of Code version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NegativeLiterals +- NumDecimals +- OverloadedLists +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeInType +- TypeOperators +- ViewPatterns + + executables: advent01: main: advent01.hs diff --git a/advent01/src/advent01.hs b/advent01/src/advent01.hs index e1935f4..5c70f59 100644 --- a/advent01/src/advent01.hs +++ b/advent01/src/advent01.hs @@ -1,6 +1,3 @@ -{-# LANGUAGE NegativeLiterals #-} -{-# LANGUAGE OverloadedStrings #-} - import Data.Text (Text) import qualified Data.Text.IO as TIO @@ -11,8 +8,6 @@ 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 S main :: IO () main = do @@ -23,16 +18,18 @@ main = do part1 :: [Int] -> Int -part1 = sum +part1 = sum . map fuelRequired part2 :: [Int] -> Int -part2 = snd . head . dropWhile unRepeated . scanl merge (S.empty, 0) . cycle +part2= sum . map fuelForFuel + + +fuelRequired :: Int -> Int +fuelRequired m = (m `div` 3) - 2 -merge :: (IntSet, Int) -> Int -> (IntSet, Int) -merge (frequencies, frequency) change = (S.insert frequency frequencies, frequency + change) +fuelForFuel :: Int -> Int +fuelForFuel m = sum $ takeWhile (> 0) $ drop 1 $ iterate fuelRequired m -unRepeated :: (IntSet, Int) -> Bool -unRepeated (frequencies, frequency) = frequency `S.notMember` frequencies -- Parse the input file type Parser = Parsec Void Text @@ -44,12 +41,12 @@ sc = L.space (skipSome spaceChar) CA.empty CA.empty lexeme = L.lexeme sc integer = lexeme L.decimal -signedInteger = L.signed sc integer +-- signedInteger = L.signed sc integer -changesP = many signedInteger +moduleP = many integer successfulParse :: Text -> [Int] successfulParse input = - case parse changesP "input" input of + case parse moduleP "input" input of Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err Right changes -> changes \ No newline at end of file diff --git a/data/advent01.txt b/data/advent01.txt index f907f85..9feebe2 100644 --- a/data/advent01.txt +++ b/data/advent01.txt @@ -1,1000 +1,100 @@ --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 +50350 +104487 +101866 +143582 +58497 +69981 +98300 +119291 +148489 +83005 +107291 +124738 +142256 +108102 +121054 +119697 +75546 +109022 +136754 +52073 +115235 +87668 +64523 +71179 +69071 +142380 +68233 +115226 +132656 +137007 +82838 +79339 +131726 +52295 +102941 +98297 +144374 +118998 +63910 +146772 +82916 +72068 +82855 +55915 +91663 +82917 +105876 +119551 +70639 +114459 +129235 +56041 +70031 +145187 +54913 +56928 +52159 +144384 +80104 +83932 +81334 +72693 +50595 +128895 +54138 +79126 +69930 +72896 +108357 +67415 +110581 +131477 +65517 +87912 +125782 +51785 +145472 +54358 +87715 +98067 +99791 +92502 +50750 +76614 +110137 +56118 +149501 +76542 +87183 +128333 +127657 +144246 +141704 +96873 +62434 +136609 +121829 +111796 +103936 +69807 diff --git a/problems/day01.html b/problems/day01.html new file mode 100644 index 0000000..a011978 --- /dev/null +++ b/problems/day01.html @@ -0,0 +1,143 @@ + + + + +Day 1 - Advent of Code 2019 + + + + + + + +

Advent of Code

Neil Smith 2*

      /^2019$/

+ + + +
+

--- Day 1: The Tyranny of the Rocket Equation ---

Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.

+

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!

+

The Elves quickly load you into a spacecraft and prepare to launch.

+

At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.

+

Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.

+

For example:

+
    +
  • For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
  • +
  • For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
  • +
  • For a mass of 1969, the fuel required is 654.
  • +
  • For a mass of 100756, the fuel required is 33583.
  • +
+

The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.

+

What is the sum of the fuel requirements for all of the modules on your spacecraft?

+
+

Your puzzle answer was 3212842.

--- Part Two ---

During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.

+

Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.

+

So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:

+
    +
  • A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
  • +
  • At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
  • +
  • The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
  • +
+

What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

+
+

Your puzzle answer was 4816402.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index c341fe3..115ecb8 100644 --- a/stack.yaml +++ b/stack.yaml @@ -34,6 +34,7 @@ ghc-options: # - auto-update # - wai + packages: # - . - advent01 -- 2.34.1