Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Day 10 #18

Merged
merged 9 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion AdventOfCode2020.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: c776df2c7388f0018c9a5988ce515debb8e3d8c2349f47541399322c6a62ae17
-- hash: 8fed5561cfbd7ec02e1d7161aba7d34f6bf96fdf146af3749ba482dd2fe3905b

name: AdventOfCode2020
version: 2.0.2.0
Expand Down Expand Up @@ -41,6 +41,7 @@ library
Day08.Utils
Day09.Solution
Day09.Utils
Day10.Solution
Day11.Solution
Practice.Foldable
Template.Solution
Expand Down Expand Up @@ -76,6 +77,7 @@ test-suite AdventOfCode2020-test
Day08.UtilsSpec
Day09.SolutionSpec
Day09.UtilsSpec
Day10.SolutionSpec
Day11.SolutionSpec
Practice.FoldableSpec
Template.SolutionSpec
Expand Down
3 changes: 3 additions & 0 deletions src/Advent/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ occurrences target = foldr go 0
readInt :: String -> Int
readInt n = read n :: Int

parseInts :: String -> [Int]
parseInts = map readInt . lines

rightToMaybe :: Either a b -> Maybe b
rightToMaybe = either (const Nothing) Just

Expand Down
8 changes: 4 additions & 4 deletions src/Day09/Solution.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Day09.Solution (part1, part2, xmasCypher, encryptionWeakness, rollingChunks') where

import Advent.Utils (combinations)
import Advent.Utils (combinations, parseInts)
import Data.Foldable (find)
import Data.Maybe (fromJust)
import Day09.Utils (parseNumbers, rollingChunks)
import Day09.Utils (rollingChunks)

part1 :: String -> String
part1 = show . fromJust . xmasCypher 25

part2 :: String -> String
part2 = show . (encryptionWeakness <$> (fromJust . xmasCypher 25) <*> parseNumbers)
part2 = show . (encryptionWeakness <$> (fromJust . xmasCypher 25) <*> parseInts)

xmasCypher :: Int -> String -> Maybe Int
xmasCypher size = fmap snd . find (uncurry followsPreamble) . rollingChunks' size . parseNumbers
xmasCypher size = fmap snd . find (uncurry followsPreamble) . rollingChunks' size . parseInts
where
followsPreamble :: [Int] -> Int -> Bool
followsPreamble preamble target = not $ any ((== target) . sum) (combinations 2 preamble)
Expand Down
5 changes: 0 additions & 5 deletions src/Day09/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module Day09.Utils where

import Advent.Utils (readInt)

parseNumbers :: String -> [Int]
parseNumbers = map readInt . lines

rollingChunks :: Int -> [a] -> [[a]]
rollingChunks size xs =
[ take size . drop i $ xs
Expand Down
153 changes: 153 additions & 0 deletions src/Day10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
## 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?_

## 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?_

## Link

[https://adventofcode.com/2020/day/10][1]

[1]: https://adventofcode.com/2020/day/10
49 changes: 49 additions & 0 deletions src/Day10/Solution.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Day10.Solution (joltageJumps, part1, part2, possibilitiesTree) where

import Advent.Utils (parseInts)
import Data.List (sort)
import Data.Tree (Tree, unfoldTree)

part1 :: String -> String
part1 = show . (\(a, _, c) -> a * c) . joltageJumps

part2 :: String -> String
part2 = show . succ . sum . possibilitiesTree

joltageJumps :: String -> (Int, Int, Int)
joltageJumps = go (0, 0, 0) . sort . withMinMax . parseInts
where
go :: (Int, Int, Int) -> [Int] -> (Int, Int, Int)
go jumps [_] = jumps
go (a, b, c) (x : x' : xs) = go nextJumps (x' : xs)
where
nextJumps = case x' - x of
1 -> (succ a, b, c)
2 -> (a, succ b, c)
3 -> (a, b, succ c)

possibilitiesTree :: String -> Tree Int
possibilitiesTree = unfoldTree go . sort . withMinMax . parseInts
where
go :: [Int] -> (Int, [[Int]])
go [] = (0, [])
go [_] = (0, [])
go (x : xs) =
(pred $ length possibilities, possibilities)
where
possibilities :: [[Int]]
possibilities =
[ xs'
| i <- [0 .. 2],
let xs' = next x (drop i xs),
not (null xs')
]

next :: Int -> [Int] -> [Int]
next _ [] = []
next y ys
| head ys - y <= 3 = ys
| otherwise = []

withMinMax :: [Int] -> [Int]
withMinMax = (0 :) . ((:) =<< (3 +) . maximum)
5 changes: 5 additions & 0 deletions test/Advent/UtilsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Advent.Utils
fromRightOrShowError,
isBetween,
occurrences,
parseInts,
readInt,
rightToMaybe,
)
Expand Down Expand Up @@ -32,6 +33,10 @@ spec = parallel $ do
it "is an int" $
readInt "123" `shouldBe` 123

describe "parseInts" $ do
it "parses a string into numbers" $
parseInts "1\n3\n5\n7\n11\n13\n17\n" `shouldBe` [1, 3, 5, 7, 11, 13, 17]

describe "isBetween" $
context "given a range of 1 and 13" $
let lower = 1 :: Int
Expand Down
4 changes: 2 additions & 2 deletions test/Day09/SolutionSpec.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Day09.SolutionSpec (spec) where

import Advent.Utils (parseInts)
import Day09.Solution (encryptionWeakness, part1, part2, rollingChunks', xmasCypher)
import Day09.Utils (parseNumbers)
import Test.Hspec

spec :: Spec
Expand All @@ -18,7 +18,7 @@ spec = parallel $ do
xmasCypher 5 input `shouldBe` Just 127
describe "encryptionWeakness" $ do
it "finds the contiguous set of numbers that sum to the target" $ do
input <- parseNumbers <$> readFile "./test/Day09/example.txt"
input <- parseInts <$> readFile "./test/Day09/example.txt"
encryptionWeakness 127 input `shouldBe` 62
describe "rollingChunks'" $ do
it "iterates through a list for case 1" $ do
Expand Down
5 changes: 1 addition & 4 deletions test/Day09/UtilsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Day09.UtilsSpec (spec) where

import Day09.Utils (parseNumbers, rollingChunks)
import Day09.Utils (rollingChunks)
import Test.Hspec

spec :: Spec
Expand All @@ -10,6 +10,3 @@ spec = parallel $ do
rollingChunks 2 "abcd" `shouldBe` ["ab", "bc", "cd"]
it "iterates through a list for case 2" $
rollingChunks 3 "abcdef" `shouldBe` ["abc", "bcd", "cde", "def"]
describe "parseNumbers" $ do
it "parses a string into numbers" $
parseNumbers "1\n3\n5\n7\n11\n13\n17\n" `shouldBe` [1, 3, 5, 7, 11, 13, 17]
40 changes: 40 additions & 0 deletions test/Day10/SolutionSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Day10.SolutionSpec (spec) where

import Day10.Solution (joltageJumps, part1, part2, possibilitiesTree)
import Test.Hspec

spec :: Spec
spec = parallel $ do
it "solves Part 1" $ do
input <- readFile "./test/Day10/input.txt"
part1 input `shouldBe` "2376"

xit "solves Part 2" $ do
input <- readFile "./test/Day10/input.txt"
part2 input `shouldBe` "hello santa"
manuphatak marked this conversation as resolved.
Show resolved Hide resolved
describe "joltageJumps" $ do
context "given the file example-1.txt" $ do
let expected = (7, 0, 5)
it ("has joltage jumps of" ++ show expected) $ do
input <- readFile "./test/Day10/example-1.txt"

joltageJumps input `shouldBe` expected
context "given the file example-2.txt" $ do
let expected = (22, 0, 10)
it ("has joltage jumps of" ++ show expected) $ do
input <- readFile "./test/Day10/example-2.txt"

joltageJumps input `shouldBe` expected
describe "possibleArrangements" $ do
context "given the file example-1.txt" $ do
let count = 8
it ("has " ++ show count ++ " possible arrangements") $ do
input <- readFile "./test/Day10/example-1.txt"

(succ . sum . possibilitiesTree) input `shouldBe` count
context "given the file example-2.txt" $ do
let count = 19208
it ("has " ++ show count ++ " possible arrangements") $ do
input <- readFile "./test/Day10/example-2.txt"

(succ . sum . possibilitiesTree) input `shouldBe` count
11 changes: 11 additions & 0 deletions test/Day10/example-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4
Loading