Skip to content

Commit ce8e368

Browse files
authored
Merge pull request #47 from folivetti/main
day 14
2 parents 9badc5c + 7504e82 commit ce8e368

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3054
-5
lines changed

2020/folivetti/day10/day10.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extra-source-files: README.md
1515

1616
executable day10
1717
hs-source-dirs: src
18-
other-modules: Algebra
18+
other-modules: Algebra, Representable
1919
main-is: Main.hs
2020
default-language: Haskell2010
2121
build-depends: base >= 4.7 && < 5

2020/folivetti/day10/src/Main.hs

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Main where
33
import Data.List
44
import Control.Monad
55
import Algebra
6+
import Representable
67

78
genDynTable :: CoAlgebra (ListF Int) [Int]
89
genDynTable [] = NilF
@@ -23,6 +24,15 @@ count n jolts (ConsF x xs) = tot : xs
2324
| (jolts !! ix) - jolt <= 3 = acc + (xs !! (ix-x-1))
2425
| otherwise = 0
2526

27+
f :: Int -> [Int] -> Int -> Int
28+
f n jolts ix
29+
| ix == n-2 = 1
30+
| otherwise = sum $ map (f n jolts) rng
31+
where
32+
rng = filter (\jx -> (jx < n) && within jx) [ix+1..ix+3]
33+
jolt = jolts !! ix
34+
within jx = jolts !! jx <= jolt + 3
35+
2636
main :: IO ()
2737
main = do
2838
dat <- map read . lines <$> readFile "day10.txt"
@@ -31,5 +41,8 @@ main = do
3141
n = length jolts
3242
n1 = length $ filter (==1) diff
3343
n3 = length $ filter (==3) diff
44+
t :: Stream Int
45+
t = tabulate (f n jolts)
3446
print $ n1*(n3+1)
3547
print $ head $ hylo (count n jolts) genDynTable [0 .. length jolts - 2]
48+
print $ index t 0
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# language TypeFamilies #-}
2+
module Representable where
3+
4+
class Representable f where
5+
-- a
6+
type Rep f :: *
7+
8+
-- alpha :: Reader a x -> F x
9+
tabulate :: (Rep f -> x) -> f x
10+
11+
-- beta :: F x -> Reader a x
12+
index :: f x -> Rep f -> x
13+
14+
data Stream x = Cons x (Stream x)
15+
16+
instance Functor Stream where
17+
fmap f (Cons x xs) = Cons (f x) (fmap f xs)
18+
19+
instance Representable Stream where
20+
type Rep Stream = Int
21+
tabulate f = Cons (f 0) (tabulate (f . (+1)))
22+
index (Cons b bs) n | n == 0 = b
23+
| otherwise = index bs (n-1)

2020/folivetti/day13/src/Main.hs

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ parseData dat = let (x:y:_) = lines dat
1717
sieve :: [(Integer, Integer)] -> Integer
1818
sieve = head . snd . foldl' go (1, [0])
1919
where
20-
createSeeds mult x = map (\i -> x + i*mult) [0..]
21-
filterSeeds n a = head . filter (\i -> (i+a) `mod` n == 0)
22-
nextSeeds m n a = createSeeds (m*n) . filterSeeds n a
20+
createSeeds mult x = map (\i -> x + i*mult) [0..] -- [x, x+mult..]
21+
filterSeeds n a = head . filter (\i -> (i+a) `mod` n == 0)
22+
nextSeeds m n a = createSeeds (m*n) . filterSeeds n a -- map.filter
2323

24-
go (m, seeds) (a,n) = (m*n, nextSeeds m n a seeds)
24+
go (m, seeds) (a,n) = (m*n, nextSeeds m n a seeds) -- swap
25+
26+
sieve' :: (Integer, Integer) -> (Integer, Integer) -> (Integer, Integer)
27+
sieve' (seed, mul) (offset, bus) = (smallestSolution, bus*mul)
28+
where
29+
smallestSolution = head [t | t <- [seed, seed+mul ..]
30+
, (t+offset) `rem` bus == 0]
2531

2632
main :: IO ()
2733
main = do
@@ -32,3 +38,4 @@ main = do
3238
(tf, bus) = minimumBy (comparing fst) $ zip turns buses
3339
print $ tf*bus
3440
print $ sieve offsBuses
41+
print $ foldl' sieve' (0, 1) offsBuses

2020/folivetti/day14/LICENSE

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright Author name here (c) 2020
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Author name here nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2020/folivetti/day14/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# day14

2020/folivetti/day14/Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

2020/folivetti/day14/day14.cabal

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: day14
2+
version: 0.1.0.0
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/githubuser/day14#readme
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Author name here
9+
maintainer: [email protected]
10+
copyright: 2020 Author name here
11+
category: Web
12+
build-type: Simple
13+
cabal-version: >=1.10
14+
extra-source-files: README.md
15+
16+
executable day14
17+
hs-source-dirs: src
18+
main-is: Main.hs
19+
default-language: Haskell2010
20+
build-depends: base >= 4.7 && < 5, containers, split

0 commit comments

Comments
 (0)