From: Neil Smith Date: Mon, 7 Dec 2020 17:29:32 +0000 (+0000) Subject: Done day 1 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=d3f84f094ba00f34cd43eea17a7aab4200a8ef04;p=advent-of-code-20.git Done day 1 --- diff --git a/README.md b/README.md index 16347c8..2903791 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ stack exec hp2ps advent01.hp # Packages -Stack is using the [14.16-lts resolver](https://www.stackage.org/lts-14.16) for packages, so make sure you read the [correct documentation for the packages included in it](https://www.stackage.org/lts-14.16/docs). +Stack is using the [14.16-lts resolver](https://www.stackage.org/lts-16.25) for packages, so make sure you read the [correct documentation for the packages included in it](https://www.stackage.org/lts-16.25/docs). # Readme diff --git a/advent01/src/advent01.hs b/advent01/src/advent01.hs index 34cf113..8936844 100644 --- a/advent01/src/advent01.hs +++ b/advent01/src/advent01.hs @@ -1,152 +1,25 @@ -- import Debug.Trace -import Data.Finite (Finite, modulo, getFinite) -import GHC.TypeNats (KnownNat) - - --- import Data.Functor.Compose (Compose(..)) --- import Data.Matrix (Matrix, matrix, safeGet, (!), prettyMatrix, mapPos, fromList, toList) -import qualified Data.Matrix as X -import Data.Bool (bool) -import Data.Distributive (Distributive(..)) -import Data.Functor.Rep (Representable(..), distributeRep) -import Data.Functor.Identity (Identity(..)) -import Control.Comonad.Representable.Store (Store(..), StoreT(..), store, experiment, runStore) -import Control.Comonad (Comonad(..)) - -import Data.Maybe -import Data.List -import qualified Data.Set as S -import qualified Data.Map as M - -import Control.Concurrent (threadDelay) -import Control.Monad (forM_) - - -instance Ord Grid where - m1 `compare` m2 = (X.toList m1) `compare` (X.toList m2) - - -type Coord = (Int, Int) -type Grid = X.Matrix Bool -type StoredGrid = Store X.Matrix Bool -type Rule = StoredGrid -> Bool - -type GridCache = S.Set Grid - --- mGet :: Coord -> Matrix a -> a --- mGet (r, c) mtx = fromMaybe False $ safeGet r c mtx --- mGet rc mtx = mtx ! rc - - -validCoord :: Coord -> Bool -validCoord (r, c) = r >= 1 && r <= gridSize && c >= 1 && c <= gridSize - - -instance Distributive X.Matrix where - distribute = distributeRep - -instance Representable X.Matrix where - type Rep X.Matrix = Coord - index m c = (X.!) m c -- mGet c m - tabulate = X.matrix gridSize gridSize - -gridSize :: Int -gridSize = 5 - - -neighbourCoords :: [Coord] --- neighbourCoords = [(x, y) | x <- [-1, 0, 1], y <- [-1, 0, 1], (x, y) /= (0, 0)] -neighbourCoords = [(-1, 0), (1, 0), (0, -1), (0, 1)] - -addCoords :: Coord -> Coord -> Coord -addCoords (x, y) (x', y') = (x + x', y + y') - -basicRule :: Rule -basicRule g = (alive && numNeighboursAlive == 1) || ((not alive) && (numNeighboursAlive == 1 || numNeighboursAlive == 2)) - where - alive = extract g - neighbours = experiment ((filter validCoord) . (at neighbourCoords)) g - numNeighboursAlive = length (filter id neighbours) - -step :: Rule -> StoredGrid -> StoredGrid -step = extend - -render :: StoredGrid -> String --- render (StoreT (Identity g) _) = foldMap ((++ "\n") . foldMap (bool "." "#")) g -render grid = X.prettyMatrix $ X.mapPos (\_ c -> bool "." "#" c) g - where g = unGrid grid - - -mkGrid :: [Coord] -> StoredGrid -mkGrid xs = store (`elem` xs) (1, 1) - -unGrid :: StoredGrid -> Grid --- unGrid (StoreT (Identity g) _) = g -unGrid grid = X.fromList gridSize gridSize gridList - where (sgf, _sgl) = runStore grid - gridList = [sgf (r, c) | r <- [1..gridSize], c <- [1..gridSize]] - - -at :: [Coord] -> Coord -> [Coord] -coords `at` origin = map (addCoords origin) coords - --- glider, blinker, beacon :: [Coord] --- glider = [(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)] --- blinker = [(0, 0), (1, 0), (2, 0)] --- beacon = [(0, 0), (1, 0), (0, 1), (3, 2), (2, 3), (3, 3)] - - -tickTime :: Int -tickTime = 200000 - -start :: IO StoredGrid -start = do coords <- readGrid - return $ mkGrid coords - -- glider `at` (1, 1) - -- ++ beacon `at` (15, 5) - main :: IO () main = - do sG <- start - print $ part1 sG - -- let grids = map unGrid $ iterate (step basicRule) sG - -- forM_ (take 5 $ iterate (step basicRule) sG) $ \grid -> do - -- -- putStr "\ESC[2J" -- Clear terminal screen - -- putStrLn (render grid) - -- -- threadDelay tickTime - - -readGrid = - do gs <- readFile "data/advent24.txt" - let grid = lines gs - let isBug r c = (grid!!r)!!c == '#' - let ng = gridSize - 1 - return [(r + 1, c + 1) | r <- [0..ng], c <- [0..ng], isBug r c] - - --- part1 :: Grid -> [Grid] -part1 :: StoredGrid -> Integer --- part1 startingGrid = map fst $ takeWhile (uncurry . S.notMember) (zip grids gridCache) --- part1 startingGrid = map fst $ takeWhile (\(g, c) -> S.notMember g c) (zip grids gridCache) --- part1 startingGrid = fst $ head $ dropWhile (\(g, c) -> S.notMember g c) (zip grids gridCache) -part1 startingGrid = bioDiversity firstRepeat - where - -- grids = map unGrid $ iterate (step basicRule) startingGrid - -- gridCache = scanl' (flip . S.insert) S.empty grids - grids = fGrids startingGrid - gridCache = fGridCache grids - firstRepeat = fst $ head $ dropWhile (uncurry S.notMember) (zip grids gridCache) - -fGrids :: StoredGrid -> [Grid] -fGrids stG = map unGrid $ iterate (step basicRule) stG - -fGridCache :: [Grid] -> [S.Set Grid] -fGridCache gs = scanl' (flip S.insert) S.empty gs --- fGridCache gs = scanl' (\s g -> S.insert g s) S.empty gs - - -bioDiversity :: Grid -> Integer -bioDiversity g = sum $ map snd $ filter (id . fst) $ zip bugs $ iterate ( * 2) 1 - where bugs = X.toList g + do numStrs <- readFile "data/advent01.txt" + let nums = map (read @Int) $ lines numStrs + print $ head $ part1 nums + print $ head $ part2 nums + +part1 nums = [ x * y + | x <- nums + , y <- nums + , x < y + , x + y == 2020 + ] + +part2 nums = [ x * y * z + | x <- nums + , y <- nums + , z <- nums + , x < y + , y < z + , x + y + z == 2020 + ] \ No newline at end of file diff --git a/data/advent01.txt b/data/advent01.txt new file mode 100644 index 0000000..90f0ace --- /dev/null +++ b/data/advent01.txt @@ -0,0 +1,200 @@ +1864 +1880 +1300 +1961 +1577 +1900 +1307 +1818 +1736 +1846 +1417 +1372 +1351 +1860 +1738 +1525 +1798 +1218 +1723 +1936 +1725 +1998 +1466 +1922 +1782 +1947 +1717 +1914 +1843 +1732 +1918 +814 +1771 +1712 +1804 +1213 +1859 +1820 +1793 +1870 +1993 +1787 +1824 +1849 +1646 +1489 +1348 +1978 +1628 +1781 +2002 +1297 +1829 +1596 +1819 +1313 +1413 +1726 +1449 +1810 +1295 +1679 +1358 +1949 +1644 +1825 +1891 +490 +1962 +1939 +1228 +1889 +1977 +1980 +1763 +1752 +1983 +1785 +1678 +2000 +1857 +1658 +1863 +1330 +1380 +1799 +1789 +1633 +1663 +296 +1985 +1117 +1239 +1854 +1960 +2004 +1940 +1876 +1739 +1858 +1283 +1423 +1982 +1836 +1451 +1840 +1347 +1652 +1695 +1210 +1861 +1199 +1346 +1786 +1814 +1958 +1853 +1974 +1917 +1308 +654 +1743 +1847 +1367 +1559 +1614 +1897 +2003 +1886 +1885 +1682 +1204 +1986 +1816 +1994 +1817 +1751 +1701 +1619 +1970 +816 +1852 +1832 +1631 +703 +1604 +1444 +1842 +1984 +1259 +1948 +1620 +1681 +1822 +1865 +1521 +1741 +1455 +1909 +1764 +261 +1464 +1905 +1325 +1766 +1749 +1292 +1874 +1267 +1269 +1969 +1991 +1219 +1345 +1976 +1369 +1942 +1388 +1776 +1629 +1987 +1684 +1813 +1203 +1965 +1729 +1930 +1609 +1801 +1402 +121 +1833 +1898 +1957 +1051 +1430 +1893 +1784 +1800 +1910 diff --git a/stack.yaml b/stack.yaml index 4a79eef..a9a0f89 100644 --- a/stack.yaml +++ b/stack.yaml @@ -20,6 +20,10 @@ resolver: url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/25.yaml +ghc-options: + $locals: -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N + + # User packages to be built. # Various formats can be used as shown in the example below. # @@ -30,7 +34,9 @@ resolver: # - auto-update # - wai packages: -- . +# - . +- advent01 + # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as # forks / in-progress versions pinned to a git hash. For example: