From c2a0cc1ad95b213ef2552d25328341b3479822f7 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 4 Dec 2019 08:01:27 +0000 Subject: [PATCH] Done day 4 --- advent04/package.yaml | 57 ++++++++++++++++++++++++++++++++++++++++ advent04/src/advent04.hs | 40 ++++++++++++++++++++++++++++ stack.yaml | 1 + 3 files changed, 98 insertions(+) create mode 100644 advent04/package.yaml create mode 100644 advent04/src/advent04.hs diff --git a/advent04/package.yaml b/advent04/package.yaml new file mode 100644 index 0000000..c8e4062 --- /dev/null +++ b/advent04/package.yaml @@ -0,0 +1,57 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent04 +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 +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeInType +- TypeOperators +- ViewPatterns + + +executables: + advent04: + main: advent04.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 diff --git a/advent04/src/advent04.hs b/advent04/src/advent04.hs new file mode 100644 index 0000000..9066cbb --- /dev/null +++ b/advent04/src/advent04.hs @@ -0,0 +1,40 @@ + +main :: IO () +main = do + print part1 + print part2 + +lowerLimit = 134792 +upperLimit = 675810 + + +part1 = length $ filter inRange $ filter adjacentSame candidates +part2 = length $ filter isolatedAdjacentSame $ filter inRange $ filter adjacentSame candidates + +inRange digits = n >= lowerLimit && n <= upperLimit + where n = numify digits + +numify :: (Int, Int, Int, Int, Int, Int) -> Int +numify (d1, d2, d3, d4, d5, d6) = + d1 * 10^5 + d2 * 10^4 + d3 * 10^3 + d4 * 10^2 + d5 * 10 + d6 + +adjacentSame (d1, d2, d3, d4, d5, d6) = + d1 == d2 || d2 == d3 || d3 == d4 || d4 == d5 || d5 == d6 + +isolatedAdjacentSame (d1, d2, d3, d4, d5, d6) = + (d1 == d2 && d2 /= d3) + || (d1 /= d2 && d2 == d3 && d3 /= d4) + || (d2 /= d3 && d3 == d4 && d4 /= d5) + || (d3 /= d4 && d4 == d5 && d5 /= d6) + || (d4 /= d5 && d5 == d6) + + +candidates = [ (d1, d2, d3, d4, d5, d6) + | d1 <- [1..6] + , d2 <- [d1..9] + , d3 <- [d2..9] + , d4 <- [d3..9] + , d5 <- [d4..9] + , d6 <- [d5..9] + ] + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 1b3c906..e0c5714 100644 --- a/stack.yaml +++ b/stack.yaml @@ -40,6 +40,7 @@ packages: - advent01 - advent02 - advent03 +- advent04 # Dependency packages to be pulled from upstream that are not in the resolver. -- 2.34.1