diff --git a/AdventOfCode.cabal b/AdventOfCode.cabal index be838f7..ad35cec 100644 --- a/AdventOfCode.cabal +++ b/AdventOfCode.cabal @@ -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 homepage: https://github.com/bionikspoon/HaskellAdventOfCode2020#readme bug-reports: https://github.com/bionikspoon/HaskellAdventOfCode2020/issues @@ -27,6 +27,7 @@ source-repository head library exposed-modules: + Day01.Solution Template.Solution other-modules: Paths_AdventOfCode @@ -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: diff --git a/README.md b/README.md index ca3a44e..2250107 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Day01/README.md b/src/Day01/README.md new file mode 100644 index 0000000..c3d5573 --- /dev/null +++ b/src/Day01/README.md @@ -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 diff --git a/src/Day01/Solution.hs b/src/Day01/Solution.hs new file mode 100644 index 0000000..6b25686 --- /dev/null +++ b/src/Day01/Solution.hs @@ -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'] diff --git a/src/Template/Solution.hs b/src/Template/Solution.hs index 014e14f..17be2a2 100644 --- a/src/Template/Solution.hs +++ b/src/Template/Solution.hs @@ -1,4 +1,4 @@ -module Template.Solution where +module Template.Solution (part1, part2) where part1 :: String -> String part1 = id diff --git a/test/Day01/SolutionSpec.hs b/test/Day01/SolutionSpec.hs new file mode 100644 index 0000000..583ff58 --- /dev/null +++ b/test/Day01/SolutionSpec.hs @@ -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) \ No newline at end of file diff --git a/test/Day01/input.txt b/test/Day01/input.txt new file mode 100644 index 0000000..5e3f00a --- /dev/null +++ b/test/Day01/input.txt @@ -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 diff --git a/test/Template/SolutionSpec.hs b/test/Template/SolutionSpec.hs index 07da0ce..2717063 100644 --- a/test/Template/SolutionSpec.hs +++ b/test/Template/SolutionSpec.hs @@ -1,6 +1,6 @@ module Template.SolutionSpec (spec) where -import Template.Solution +import Template.Solution (part1, part2) import Test.Hspec spec :: Spec