Compare commits

...

2 Commits

  1. 4
      AdventOfCode2020.cabal
  2. 13
      Main.hs
  3. 1000
      input/day2
  4. 10
      src/Day1.hs
  5. 43
      src/Day2.hs

@ -17,11 +17,13 @@ maintainer: gael@depreeuw.dev
extra-source-files: CHANGELOG.md, README.md extra-source-files: CHANGELOG.md, README.md
library library
exposed-modules: Day1 exposed-modules: Day1, Day2
-- other-modules: -- other-modules:
-- other-extensions: -- other-extensions:
build-depends: base ^>=4.13.0.0 build-depends: base ^>=4.13.0.0
, containers ^>=0.6.4.1 , containers ^>=0.6.4.1
, text ^>=1.2.4.0
, attoparsec ^>=0.13.2.4
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010

@ -1,16 +1,9 @@
module Main where module Main where
import qualified Day1 (fixExpenseReport) import Day1 (day1)
import Day2 (day2)
day1 :: IO ()
day1 = do
r <- readFile "./input/day1"
putStr "[Day 1-1] fix: "
print . Day1.fixExpenseReport 2020 2 . fmap read . lines $ r
putStr "[Day 1-2] fix: "
print . Day1.fixExpenseReport 2020 3 . fmap read . lines $ r
main :: IO () main :: IO ()
main = do main = do
day1 day1
day2

File diff suppressed because it is too large Load Diff

@ -1,4 +1,4 @@
module Day1 (fixExpenseReport) where module Day1 (day1) where
import qualified Data.IntSet as DIS import qualified Data.IntSet as DIS
import Data.Maybe (mapMaybe) import Data.Maybe (mapMaybe)
@ -7,6 +7,14 @@ type Target = Int
type Expenses = [Int] type Expenses = [Int]
type Expenses' = DIS.IntSet type Expenses' = DIS.IntSet
day1 :: IO ()
day1 = do
r <- readFile "./input/day1"
putStr "[Day 1-1] fix: "
print . fixExpenseReport 2020 2 . fmap read . lines $ r
putStr "[Day 1-2] fix: "
print . fixExpenseReport 2020 3 . fmap read . lines $ r
-- Turn the list into an IntSet. For each element in the set, look up the -- Turn the list into an IntSet. For each element in the set, look up the
-- number in the set to make it 2020. If found, return the multiplication of -- number in the set to make it 2020. If found, return the multiplication of
-- the two numbers. -- the two numbers.

@ -0,0 +1,43 @@
{-# LANGUAGE OverloadedStrings #-}
module Day2 (day2) where
import qualified Data.Text as T
import qualified Data.Attoparsec.Text as P
type Max = Integer
type Min = Integer
type Letter = Char
data Policy = Policy { lBound :: Min
, uBound :: Max
, letter :: Letter
}
parserPolicy :: P.Parser Policy
parserPolicy = do
lB <- P.decimal
_ <- P.char '-'
uB <- P.decimal
_ <- P.char ' '
le <- P.letter
_ <- P.string ": "
return $ Policy lB uB le
validatePolicy :: Policy -> String -> Bool
validatePolicy p s =
let l = toInteger . length $ filter (== letter p) s in
l >= lBound p && l <= uBound p
validateLine :: String -> Bool
validateLine s =
case P.parse parserPolicy (T.pack s) of
P.Fail {} -> error "day2 - parser error"
P.Partial _ -> error "day2 - input error"
P.Done i r -> validatePolicy r (T.unpack i)
day2 :: IO ()
day2 = do
r <- readFile "./input/day2"
putStr "[Day 1-1] # valid passwords: "
print . length . filter (==True) . fmap validateLine . lines $ r
Loading…
Cancel
Save