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 01 #2

Merged
merged 10 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions AdventOfCode.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 4ebd7eb77ecc4dac43fbdfb206ceef40dea9c6075b4bd1aab61a45f1b9af95f5
-- hash: ef04133c9d76df1416a97707cf7f3005236ecd1ba2c33c28786abfdbb619588f

name: AdventOfCode
version: 0.1.0.0
version: 2.0.2.0
description: Please see the README on GitHub at <https://github.com/bionikspoon/HaskellAdventOfCode2020#readme>
homepage: https://github.com/bionikspoon/HaskellAdventOfCode2020#readme
bug-reports: https://github.com/bionikspoon/HaskellAdventOfCode2020/issues
Expand All @@ -27,6 +27,7 @@ source-repository head

library
exposed-modules:
Day01.Solution
Template.Solution
other-modules:
Paths_AdventOfCode
Expand All @@ -45,6 +46,7 @@ test-suite AdventOfCode-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Day01.SolutionSpec
Template.SolutionSpec
Paths_AdventOfCode
hs-source-dirs:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![bionikspoon](https://circleci.com/gh/bionikspoon/HaskellAdventOfCode2020.svg?style=svg)](https://circleci.com/gh/bionikspoon/HaskellAdventOfCode2020)
[![codecov](https://codecov.io/gh/bionikspoon/HaskellAdventOfCode2020/branch/main/graph/badge.svg?token=TKOFLWZ1IE)](https://codecov.io/gh/bionikspoon/HaskellAdventOfCode2020)

# AdventOfCode 2020
Solutions to adventofcode.com/2020

Expand Down
43 changes: 43 additions & 0 deletions src/Day01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Day 1: Report Repair

After saving Christmas [five years in a row][1] , you've decided to take a vacation at a nice resort on a tropical island. Surely , Christmas will go on without you.

The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them _stars_ . None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.

To save your vacation, you need to get all _fifty stars_ by December 25th.

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!

Before you leave, the Elves in accounting just need you to fix your _expense report_ (your puzzle input); apparently, something isn't quite adding up.

Specifically, they need you to _find the two entries that sum to `2020`_ and then multiply those two numbers together.

For example, suppose your expense report contained the following:

```
1721
979
366
299
675
1456
```

In this list, the two entries that sum to `2020` are `1721` and `299` . Multiplying them together produces `1721 * 299 = 514579` , so the correct answer is `_514579_` .

Of course, your expense report is much larger. _Find the two entries that sum to `2020` ; what do you get if you multiply them together?_

## Part Two

The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find _three_ numbers in your expense report that meet the same criteria.

Using the above example again, the three entries that sum to `2020` are `979` , `366` , and `675` . Multiplying them together produces the answer, `_241861950_` .

In your expense report, _what is the product of the three entries that sum to `2020` ?_

## Link

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

[1]: /events
[2]: https://adventofcode.com/2020/day/1
22 changes: 22 additions & 0 deletions src/Day01/Solution.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Day01.Solution (part1, part2, goalSeek) where

import Control.Monad ((<=<))
import Data.Foldable (Foldable (foldl'), find)
import Data.List (tails)
import Data.Maybe (fromJust)

part1 :: String -> String
part1 = show . fromJust . goalSeek 2 2020 . map readInt . lines

part2 :: String -> String
part2 = show . fromJust . goalSeek 3 2020 . map readInt . lines

readInt :: String -> Int
readInt n = read n :: Int

goalSeek :: Int -> Int -> [Int] -> Maybe Int
goalSeek n target = Just . foldl' (*) 1 <=< find ((target ==) . sum) . combinations n

combinations :: Int -> [a] -> [[a]]
combinations 0 _ = [[]]
combinations n xs = [y : ys | y : xs' <- tails xs, ys <- combinations (n -1) xs']
2 changes: 1 addition & 1 deletion src/Template/Solution.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Template.Solution where
module Template.Solution (part1, part2) where

part1 :: String -> String
part1 = id
Expand Down
23 changes: 23 additions & 0 deletions test/Day01/SolutionSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Day01.SolutionSpec (spec) where

import Day01.Solution (goalSeek, part1, part2)
import Test.Hspec

spec :: Spec
spec = do
it "solves Part 1" $ do
input <- readFile "./test/Day01/input.txt"
part1 input `shouldBe` "299299"
it "solves Part 2" $ do
input <- readFile "./test/Day01/input.txt"
part2 input `shouldBe` "287730716"
describe "goalSeek" $ do
context "given a list of numbers" $
let needle = 2020
haystack = [1721, 979, 366, 299, 675, 1456]
in do
it "finds the product of the 2 numbers that add up to a target" $
goalSeek 2 needle haystack `shouldBe` Just (1721 * 299)

it "finds the product of the 3 numbers that add up to a target" $
goalSeek 3 needle haystack `shouldBe` Just (979 * 366 * 675)
200 changes: 200 additions & 0 deletions test/Day01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
1975
1446
1902
1261
1783
1535
1807
1606
1685
1933
1930
1813
1331
1986
1379
1649
1342
1206
1832
1464
1840
1139
1316
1366
593
1932
1553
1065
2004
1151
1345
1026
1958
1778
1987
1425
1170
1927
1487
1116
1612
2005
1977
1691
1964
398
1621
1542
1929
1102
1993
1426
1349
1280
1775
849
1344
1940
1707
1562
1979
1325
1610
559
1812
1938
1572
1949
1136
161
1893
1207
1363
1551
1333
1904
1332
1450
1773
1216
1185
1881
1835
1460
1277
1374
1568
1731
1365
1719
1749
1371
1602
1108
1030
1859
1875
1976
1837
1768
1873
1226
1533
1601
1394
1422
1219
1269
1793
1195
1234
1575
1882
1223
1826
521
1161
1738
1506
1574
1337
1509
1430
1496
1318
1400
1852
1670
1898
1858
1950
1870
1920
868
1814
1853
1911
1907
1713
1281
1759
1210
1350
1035
1585
1765
1220
1125
1714
1810
1002
1356
1192
1452
1236
1482
1716
1681
1323
1923
1876
1792
1346
1891
1721
1056
1675
1518
1540
1068
1563
1942
1668
1653
1357
1632
1128
1726
1586
1998
1138
1510
1022
1480
1434
1305
1861
1623
1009
1339
1159
1085
1578
1689
1091
1874
1043
1737
1704
1515
2 changes: 1 addition & 1 deletion test/Template/SolutionSpec.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Template.SolutionSpec (spec) where

import Template.Solution
import Template.Solution (part1, part2)
import Test.Hspec

spec :: Spec
Expand Down