From 1e9f01ceeffca5c34bec504c950f9c75686956f2 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 16 Dec 2020 11:17:17 +0000 Subject: [PATCH] Done day 10 --- advent10/package.yaml | 59 ++++++++++ advent10/src/advent10.hs | 31 +++++ data/advent10.txt | 100 +++++++++++++++++ problems/day10.html | 236 +++++++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 5 files changed, 427 insertions(+) create mode 100644 advent10/package.yaml create mode 100644 advent10/src/advent10.hs create mode 100644 data/advent10.txt create mode 100644 problems/day10.html diff --git a/advent10/package.yaml b/advent10/package.yaml new file mode 100644 index 0000000..f696f63 --- /dev/null +++ b/advent10/package.yaml @@ -0,0 +1,59 @@ +# 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: advent10 +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 +- NamedFieldPuns +- NegativeLiterals +- NumDecimals +# - OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeFamilies +- TypeInType +- TypeOperators +- ViewPatterns + +executables: + advent10: + main: advent10.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - containers diff --git a/advent10/src/advent10.hs b/advent10/src/advent10.hs new file mode 100644 index 0000000..b0b77bf --- /dev/null +++ b/advent10/src/advent10.hs @@ -0,0 +1,31 @@ +-- import Debug.Trace + +import qualified Data.Map as M +-- import qualified Data.Set as S +import Data.List + +main :: IO () +main = + do + numStrs <- readFile "data/advent10.txt" + let nums = map (read @Int) $ lines numStrs + let device = 3 + maximum nums + let sortedNums = sort (0:device:nums) + print $ part1 sortedNums + print $ part2 sortedNums + +part1 :: [Int] -> Int +part1 nums = jolt1s * jolt3s + where jolt1s = joltNs 1 nums + jolt3s = joltNs 3 nums + +joltNs d ns = length $ filter diff $ zip ns (tail ns) + where diff (a, b) = (b - a) == d + +part2 :: [Int] -> Int +part2 nums = snd $ M.findMax $ foldl includeAdapter (M.singleton 0 1) $ tail nums + +includeAdapter arrangements adapter = M.insert adapter earlierCount arrangements + where + earlierArrangements = M.filterWithKey (\k _ -> (adapter - k) <= 3) arrangements + earlierCount = M.foldl (+) 0 earlierArrangements diff --git a/data/advent10.txt b/data/advent10.txt new file mode 100644 index 0000000..239293c --- /dev/null +++ b/data/advent10.txt @@ -0,0 +1,100 @@ +30 +73 +84 +136 +132 +117 +65 +161 +49 +68 +139 +46 +21 +127 +109 +153 +163 +160 +18 +22 +131 +146 +62 +113 +172 +150 +171 +98 +93 +130 +170 +59 +1 +110 +2 +55 +37 +44 +148 +102 +40 +28 +35 +43 +56 +169 +33 +5 +141 +83 +15 +105 +142 +36 +116 +11 +45 +82 +10 +17 +159 +140 +12 +108 +29 +72 +121 +52 +91 +166 +88 +97 +118 +99 +124 +149 +16 +9 +143 +104 +57 +79 +123 +58 +96 +24 +162 +23 +92 +69 +147 +156 +25 +133 +34 +8 +85 +76 +103 +122 diff --git a/problems/day10.html b/problems/day10.html new file mode 100644 index 0000000..33b032a --- /dev/null +++ b/problems/day10.html @@ -0,0 +1,236 @@ + + + + +Day 10 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 20*

  {:year 2020}

+ + + +
+ +

--- Day 10: Adapter Array ---

Patched into the aircraft's data port, you discover weather forecasts of a massive tropical storm. Before you can figure out whether it will impact your vacation plans, however, your device suddenly turns off!

+

Its battery is dead.

+

You'll need to plug it in. There's only one problem: the charging outlet near your seat produces the wrong number of jolts. Always prepared, you make a list of all of the joltage adapters in your bag.

+

Each of your joltage adapters is rated for a specific output joltage (your puzzle input). Any given adapter can take an input 1, 2, or 3 jolts lower than its rating and still produce its rated output joltage.

+

In addition, your device has a built-in joltage adapter rated for 3 jolts higher than the highest-rated adapter in your bag. (If your adapter list were 3, 9, and 6, your device's built-in adapter would be rated for 12 jolts.)

+

Treat the charging outlet near your seat as having an effective joltage rating of 0.

+

Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort and realize you can't even charge your device!

+

If you use every adapter in your bag at once, what is the distribution of joltage differences between the charging outlet, the adapters, and your device?

+

For example, suppose that in your bag, you have adapters with the following joltage ratings:

+
16
+10
+15
+5
+1
+11
+7
+19
+6
+12
+4
+
+

With these adapters, your device's built-in joltage adapter would be rated for 19 + 3 = 22 jolts, 3 higher than the highest-rated adapter.

+

Because adapters can only connect to a source 1-3 jolts lower than its rating, in order to use every adapter, you'd need to choose them like this:

+
    +
  • The charging outlet has an effective rating of 0 jolts, so the only adapters that could connect to it directly would need to have a joltage rating of 1, 2, or 3 jolts. Of these, only one you have is an adapter rated 1 jolt (difference of 1).
  • +
  • From your 1-jolt rated adapter, the only choice is your 4-jolt rated adapter (difference of 3).
  • +
  • From the 4-jolt rated adapter, the adapters rated 5, 6, or 7 are valid choices. However, in order to not skip any adapters, you have to pick the adapter rated 5 jolts (difference of 1).
  • +
  • Similarly, the next choices would need to be the adapter rated 6 and then the adapter rated 7 (with difference of 1 and 1).
  • +
  • The only adapter that works with the 7-jolt rated adapter is the one rated 10 jolts (difference of 3).
  • +
  • From 10, the choices are 11 or 12; choose 11 (difference of 1) and then 12 (difference of 1).
  • +
  • After 12, only valid adapter has a rating of 15 (difference of 3), then 16 (difference of 1), then 19 (difference of 3).
  • +
  • Finally, your device's built-in adapter is always 3 higher than the highest adapter, so its rating is 22 jolts (always a difference of 3).
  • +
+

In this example, when using every adapter, there are 7 differences of 1 jolt and 5 differences of 3 jolts.

+

Here is a larger example:

+
28
+33
+18
+42
+31
+14
+46
+20
+48
+47
+24
+23
+49
+45
+19
+38
+39
+11
+1
+32
+25
+35
+8
+17
+7
+9
+4
+2
+34
+10
+3
+
+

In this larger example, in a chain that uses all of the adapters, there are 22 differences of 1 jolt and 10 differences of 3 jolts.

+

Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?

+
+

Your puzzle answer was 2368.

--- Part Two ---

To completely determine whether you have enough adapters, you'll need to figure out how many different ways they can be arranged. Every arrangement needs to connect the charging outlet to your device. The previous rules about when adapters can successfully connect still apply.

+

The first example above (the one that starts with 16, 10, 15) supports the following arrangements:

+
(0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22)
+(0), 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, (22)
+(0), 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, (22)
+(0), 1, 4, 5, 7, 10, 12, 15, 16, 19, (22)
+(0), 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, (22)
+(0), 1, 4, 6, 7, 10, 12, 15, 16, 19, (22)
+(0), 1, 4, 7, 10, 11, 12, 15, 16, 19, (22)
+(0), 1, 4, 7, 10, 12, 15, 16, 19, (22)
+
+

(The charging outlet and your device's built-in adapter are shown in parentheses.) Given the adapters from the first example, the total number of arrangements that connect the charging outlet to your device is 8.

+

The second example above (the one that starts with 28, 33, 18) has many arrangements. Here are a few:

+
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
+32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 48, 49, (52)
+
+(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
+32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 49, (52)
+
+(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
+32, 33, 34, 35, 38, 39, 42, 45, 46, 48, 49, (52)
+
+(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
+32, 33, 34, 35, 38, 39, 42, 45, 46, 49, (52)
+
+(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
+32, 33, 34, 35, 38, 39, 42, 45, 47, 48, 49, (52)
+
+(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
+46, 48, 49, (52)
+
+(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
+46, 49, (52)
+
+(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
+47, 48, 49, (52)
+
+(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
+47, 49, (52)
+
+(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
+48, 49, (52)
+
+

In total, this set of adapters can connect the charging outlet to your device in 19208 distinct arrangements.

+

You glance back down at your bag and try to remember why you brought so many adapters; there must be more than a trillion valid ways to arrange them! Surely, there must be an efficient way to count the arrangements.

+

What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?

+
+

Your puzzle answer was 1727094849536.

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 1d42438..d3710dd 100644 --- a/stack.yaml +++ b/stack.yaml @@ -44,6 +44,7 @@ packages: - advent07 - advent08 - advent09 +- advent10 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as -- 2.34.1