Solve day 1, part 1

pull/1/head
Gaël Depreeuw 4 years ago
parent 8967218edc
commit e4878ae47c
Signed by: Mithror
GPG Key ID: 8AB218ABA4867F78
  1. 34
      AdventOfCode2020.cabal
  2. 5
      CHANGELOG.md
  3. 14
      Main.hs
  4. 2
      Setup.hs
  5. 7
      hie.yaml
  6. 200
      input/day1
  7. 19
      src/Day1.hs

@ -0,0 +1,34 @@
cabal-version: 2.4
-- Initial package description 'AdventOfCode2020.cabal' generated by 'cabal
-- init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: AdventOfCode2020
version: 0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
-- license:
license-file: LICENSE
author: Gaël Depreeuw
maintainer: gael@depreeuw.dev
-- copyright:
-- category:
extra-source-files: CHANGELOG.md, README.md
library
exposed-modules: Day1
-- other-modules:
-- other-extensions:
build-depends: base ^>=4.13.0.0
, containers ^>=0.6.4.1
hs-source-dirs: src
default-language: Haskell2010
executable AdventOfCode2020
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base ^>=4.13.0.0, AdventOfCode2020
-- hs-source-dirs:
default-language: Haskell2010

@ -0,0 +1,5 @@
# Revision history for AdventOfCode2020
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

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

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

@ -0,0 +1,7 @@
cradle:
cabal:
- path: "src"
component: "lib:AdventOfCode2020"
- path: "./Main.hs"
component: "AdventOfCode2020:exe:AdventOfCode2020"

@ -0,0 +1,200 @@
1834
1546
1119
1870
1193
1198
1542
1944
1817
1249
1361
1856
1258
1425
1835
1520
1792
1130
2004
1366
1549
1347
1507
1699
1491
1557
1865
1948
1199
1229
1598
1756
1643
1306
1838
1157
1745
1603
1972
1123
1963
1759
1118
1526
1695
1661
1262
1117
1844
1922
1997
1630
1337
1721
1147
1848
1476
1975
1942
1569
1126
1313
1449
1206
1722
1534
1706
1596
1700
1811
906
1666
1945
1271
1629
1456
1316
1636
1884
1556
1317
1393
1953
1658
2005
1252
1878
1691
60
1872
386
1369
1739
1460
1267
1935
1992
1310
1818
1320
1437
1486
1205
1286
1670
1577
1237
1558
1937
1938
1656
1220
1732
1647
1857
1446
1516
1450
1860
1625
1377
1312
1588
1895
1967
1567
1582
1428
1415
1731
1919
1651
1597
1982
1576
1172
1568
1867
1660
1754
1227
1121
1733
537
1809
1322
1876
1665
1124
1461
1888
1368
1235
1479
1529
1148
1996
1939
1340
1531
1438
1897
1152
1321
1770
897
1750
1111
1772
1615
1798
1359
1470
1610
1362
1973
1892
1830
599
1341
1681
1572
1873
42
1246
1447
1800
1524
1214
1784
1664
1882
1989
1797
1211
1170
1854
1287
1641
1760

@ -0,0 +1,19 @@
module Day1 (fixExpenseReport) where
import qualified Data.IntSet as DIS
import Data.Maybe (mapMaybe)
yearToFind :: Int
yearToFind = 2020
-- 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
-- the two numbers.
fixExpenseReport :: [Int] -> Int
fixExpenseReport expenses =
let s = DIS.fromList expenses in
head $ mapMaybe (`findOther` s) expenses
findOther :: Int -> DIS.IntSet -> Maybe Int
findOther n s = let n' = yearToFind - n in
if DIS.member n' s then Just (n*n') else Nothing
Loading…
Cancel
Save