From a7e4737b00a713ba9a4c793d3950317d0d56c326 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 10 Dec 2020 17:23:43 +0100 Subject: [PATCH] Add 2020 day 10 --- haskellAoC/inputs/2020/10 | 93 +++++++++++++++++++++++++++++++++++ haskellAoC/src/Y2020/Day10.hs | 19 +++++++ haskellAoC/src/Y2020/Days.hs | 2 + 3 files changed, 114 insertions(+) create mode 100644 haskellAoC/inputs/2020/10 create mode 100644 haskellAoC/src/Y2020/Day10.hs diff --git a/haskellAoC/inputs/2020/10 b/haskellAoC/inputs/2020/10 new file mode 100644 index 0000000..6a9eb2f --- /dev/null +++ b/haskellAoC/inputs/2020/10 @@ -0,0 +1,93 @@ +80 +87 +10 +122 +57 +142 +134 +59 +113 +139 +101 +41 +138 +112 +46 +96 +43 +125 +36 +54 +133 +17 +42 +98 +7 +114 +78 +67 +77 +28 +149 +58 +20 +105 +31 +19 +18 +27 +40 +71 +117 +66 +21 +72 +146 +90 +97 +94 +123 +1 +119 +30 +84 +61 +91 +118 +2 +29 +104 +73 +13 +76 +24 +148 +68 +111 +131 +83 +49 +8 +132 +9 +64 +79 +124 +95 +88 +135 +3 +51 +39 +6 +60 +108 +14 +35 +147 +89 +34 +65 +50 +145 +128 diff --git a/haskellAoC/src/Y2020/Day10.hs b/haskellAoC/src/Y2020/Day10.hs new file mode 100644 index 0000000..8a247ab --- /dev/null +++ b/haskellAoC/src/Y2020/Day10.hs @@ -0,0 +1,19 @@ +module Y2020.Day10 (y20day10) where + +import Data.List + +sliding :: [Int] -> [(Int, Int)] +sliding (x1:x2:xs) = ((x1, x2):(sliding (x2:xs))) +sliding (x1:[]) = ((x1, x1+3):[]) +sliding [] = [] + +combinations :: Int -> Int +combinations n = nb !! n + where nb = [1, 1, 2, 4, 7, 13] + +y20day10 :: [String] -> (String, String) +y20day10 input = (part1, part2) + where part1 = show $ product $ map length $ group $ filter (/= 2) $ sort $ diffs + part2 = show $ product $ map (combinations . length) $ filter (\(x:_) -> x /= 3) $ group $ diffs + input' = sort $ map read input :: [Int] + diffs = map (\(a, b) -> b - a) $ (0, head input'):(sliding input') diff --git a/haskellAoC/src/Y2020/Days.hs b/haskellAoC/src/Y2020/Days.hs index ce7db2d..45e2acb 100644 --- a/haskellAoC/src/Y2020/Days.hs +++ b/haskellAoC/src/Y2020/Days.hs @@ -9,6 +9,7 @@ import Y2020.Day06 import Y2020.Day07 import Y2020.Day08 import Y2020.Day09 +import Y2020.Day10 year2020 :: String -> [String] -> (String, String) year2020 "01" = y20day01 @@ -20,3 +21,4 @@ year2020 "06" = y20day06 year2020 "07" = y20day07 year2020 "08" = y20day08 year2020 "09" = y20day09 +year2020 "10" = y20day10