From 44a48767ef7bf7814aa5dd46b76f7a139631d2aa Mon Sep 17 00:00:00 2001 From: shu Date: Thu, 10 Dec 2020 08:05:45 +0100 Subject: [PATCH] Day 10 This code is hilarious --- 2020/day10/day10.hs | 21 ++++++++++ 2020/day10/input | 95 +++++++++++++++++++++++++++++++++++++++++++ 2020/day10/testinput | 11 +++++ 2020/day10/testinput2 | 31 ++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 2020/day10/day10.hs create mode 100644 2020/day10/input create mode 100644 2020/day10/testinput create mode 100644 2020/day10/testinput2 diff --git a/2020/day10/day10.hs b/2020/day10/day10.hs new file mode 100644 index 0000000..4bae31d --- /dev/null +++ b/2020/day10/day10.hs @@ -0,0 +1,21 @@ +import Control.Arrow +import Control.Monad +import Data.List + +main :: IO () +main = do + input <- map read . lines <$> readFile "input" + print $ (product . map length . group . sort . jolts) input + print $ + (product . map (arrangements . sum) . filter (elem 1) . group . jolts) input + +arrangements :: Int -> Int +arrangements n = + length $ + filter (== n) $ map sum $ nub $ map (filter (/= 0)) $ replicateM n [0 .. 3] + +jolts :: [Int] -> [Int] +jolts = + map (uncurry subtract) . + ap zip tail . + uncurry (++) . (id &&& (flip (:) [] . (+ 3) . last)) . (:) 0 . sort diff --git a/2020/day10/input b/2020/day10/input new file mode 100644 index 0000000..f0aa5d3 --- /dev/null +++ b/2020/day10/input @@ -0,0 +1,95 @@ +99 +3 +1 +11 +48 +113 +131 +43 +82 +19 +4 +153 +105 +52 +56 +109 +27 +119 +147 +31 +34 +13 +129 +17 +61 +10 +29 +24 +12 +104 +152 +103 +80 +116 +79 +73 +21 +133 +44 +18 +74 +112 +136 +30 +146 +100 +39 +130 +91 +124 +70 +115 +81 +28 +151 +2 +122 +87 +143 +62 +7 +126 +95 +75 +20 +123 +63 +125 +53 +45 +141 +14 +67 +69 +60 +114 +57 +142 +150 +42 +78 +132 +66 +88 +140 +139 +106 +38 +85 +37 +51 +94 +98 +86 +68 diff --git a/2020/day10/testinput b/2020/day10/testinput new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/2020/day10/testinput @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/2020/day10/testinput2 b/2020/day10/testinput2 new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/2020/day10/testinput2 @@ -0,0 +1,31 @@ +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