Skip to content

Commit e71d8df

Browse files
committed
day17
1 parent ea63bcd commit e71d8df

File tree

9 files changed

+225
-2
lines changed

9 files changed

+225
-2
lines changed

2020/folivetti/day15/src/Main.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ next x n s =
1414
Just y -> x : next (n-y) (n+1) (insert x n s)
1515

1616
reduceM :: (Foldable t, Monad m) => b -> t a -> (b -> a -> m b) -> m b
17-
reduceM x xs f = foldM f x xs
17+
reduceM x xs f = foldM f x xs
18+
reduceM_ x xs f = foldM_ f x xs
1819

1920
nextNumber _ (-1) = 0
2021
nextNumber turn val = turn - val
2122

2223
run :: Int -> [Int] -> Int
2324
run n input = runST $ do
2425
arr <- newArray (0, n) (-1) :: ST s (STUArray s Int Int)
25-
mapM_ (uncurry $ writeArray arr) $ zip input [1..]
26+
reduceM_ 1 input (\turn x -> do writeArray arr x turn; return (turn+1))
2627
reduceM 0 [length input + 1 .. n - 1]
2728
(\next turn -> do val <- nextNumber turn <$> readArray arr next
2829
writeArray arr next turn

2020/folivetti/day17/LICENSE

Lines changed: 30 additions & 0 deletions
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/day17/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# day17

2020/folivetti/day17/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

2020/folivetti/day17/day17.cabal

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: day17
2+
version: 0.1.0.0
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/githubuser/day17#readme
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Author name here
9+
maintainer: example@example.com
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 day17
17+
hs-source-dirs: src
18+
ghc-options: -O2
19+
main-is: Main.hs
20+
default-language: Haskell2010
21+
build-depends: base >= 4.7 && < 5, containers

2020/folivetti/day17/day17.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
..##.##.
2+
#.#..###
3+
##.#.#.#
4+
#.#.##.#
5+
###..#..
6+
.#.#..##
7+
#.##.###
8+
#.#..##.

2020/folivetti/day17/src/Main.hs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module Main where
2+
3+
import Data.Set (Set(..), member)
4+
import qualified Data.Set as S
5+
6+
data Cell = Inactive | Active deriving (Eq, Show)
7+
type Coord = (Int, Int, Int)
8+
type Coord4D = (Int, Int, Int, Int)
9+
10+
-- * Parsing
11+
12+
parse :: (Int -> String -> [a]) -> String -> [a]
13+
parse pLine input = concat
14+
$ zipWith pLine [0..]
15+
$ lines input
16+
17+
parseLine :: Int -> String -> [Coord]
18+
parseLine x css = foldr getActive []
19+
$ zip [0..]
20+
$ map parseChar css
21+
where
22+
getActive (_, Inactive) xs = xs
23+
getActive (y, Active) xs = (x,y,0):xs
24+
25+
26+
parseLine4D :: Int -> String -> [Coord4D]
27+
parseLine4D x css = foldr getActive []
28+
$ zip [0..]
29+
$ map parseChar css
30+
where
31+
getActive (_, Inactive) xs = xs
32+
getActive (y, Active) xs = (x,y,0,0):xs
33+
34+
parseChar :: Char -> Cell
35+
parseChar '.' = Inactive
36+
parseChar '#' = Active
37+
parseChar _ = error "invalid char"
38+
39+
-- * Update Automata
40+
update :: (Ord a) => (a -> [a]) -> Set a -> Set a
41+
update neighFun s = S.fromList $ coordsActives ++ coordsInactives
42+
where
43+
coords = S.toList s
44+
coordsActives = filter ((`elem`[2,3]).getActiveCoords) coords
45+
coordsInactives = concatMap getInactiveCoords coords
46+
47+
getActiveCoords coord = let activeNeigh = filter (`member` s) $ neighFun coord
48+
in length activeNeigh
49+
50+
getInactiveCoords coord = let inactiveNeigh = filter (`notMember` s) $ neighFun coord
51+
in filter ((==3).getActiveCoords) inactiveNeigh
52+
53+
notMember :: Ord a => a -> Set a -> Bool
54+
notMember x s = not (member x s)
55+
56+
neighbors :: Coord -> [Coord]
57+
neighbors (x, y, z) = sum3D (x,y,z) <$> filter (/=(0,0,0)) coords
58+
where
59+
bounds = [-1..1]
60+
coords = (,,) <$> bounds <*> bounds <*> bounds
61+
sum3D (x,y,z) (a,b,c) = (x+a, y+b, z+c)
62+
63+
neighbors4D :: Coord4D -> [Coord4D]
64+
neighbors4D (x, y, z, w) = sum4D (x,y,z,w) <$> filter (/=(0,0,0,0)) coords
65+
where
66+
bounds = [-1..1]
67+
coords = (,,,) <$> bounds <*> bounds <*> bounds <*> bounds
68+
sum4D (x,y,z,w) (a,b,c,d) = (x+a, y+b, z+c, w+d)
69+
70+
example = ".#.\n..#\n###"
71+
72+
main :: IO ()
73+
main = do
74+
dat <- readFile "day17.txt"
75+
let s0 = S.fromList $ parse parseLine dat
76+
s04D = S.fromList $ parse parseLine4D dat
77+
sets = iterate (update neighbors) s0
78+
sets4D = iterate (update neighbors4D) s04D
79+
print $ S.size (sets !! 6)
80+
print $ S.size (sets4D !! 6)

2020/folivetti/day17/stack.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# https://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
#
15+
# The location of a snapshot can be provided as a file or url. Stack assumes
16+
# a snapshot provided as a file might change, whereas a url resource does not.
17+
#
18+
# resolver: ./custom-snapshot.yaml
19+
# resolver: https://example.com/snapshots/2018-01-01.yaml
20+
resolver:
21+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/26.yaml
22+
23+
# User packages to be built.
24+
# Various formats can be used as shown in the example below.
25+
#
26+
# packages:
27+
# - some-directory
28+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
29+
# subdirs:
30+
# - auto-update
31+
# - wai
32+
packages:
33+
- .
34+
# Dependency packages to be pulled from upstream that are not in the resolver.
35+
# These entries can reference officially published versions as well as
36+
# forks / in-progress versions pinned to a git hash. For example:
37+
#
38+
# extra-deps:
39+
# - acme-missiles-0.3
40+
# - git: https://github.com/commercialhaskell/stack.git
41+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
42+
#
43+
# extra-deps: []
44+
45+
# Override default flag values for local packages and extra-deps
46+
# flags: {}
47+
48+
# Extra package databases containing global packages
49+
# extra-package-dbs: []
50+
51+
# Control whether we use the GHC we find on the path
52+
# system-ghc: true
53+
#
54+
# Require a specific version of stack, using version ranges
55+
# require-stack-version: -any # Default
56+
# require-stack-version: ">=2.5"
57+
#
58+
# Override the architecture used by stack, especially useful on Windows
59+
# arch: i386
60+
# arch: x86_64
61+
#
62+
# Extra directories used by stack for building
63+
# extra-include-dirs: [/path/to/dir]
64+
# extra-lib-dirs: [/path/to/dir]
65+
#
66+
# Allow a newer minor version of GHC than the snapshot specifies
67+
# compiler-check: newer-minor

2020/folivetti/day17/stack.yaml.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file was autogenerated by Stack.
2+
# You should not edit this file by hand.
3+
# For more information, please see the documentation at:
4+
# https://docs.haskellstack.org/en/stable/lock_files
5+
6+
packages: []
7+
snapshots:
8+
- completed:
9+
size: 533252
10+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/26.yaml
11+
sha256: cdbc5db9c1afe80a5998247939027a0c7db92fa0f20b5cd01596ec3da628b622
12+
original:
13+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/26.yaml

0 commit comments

Comments
 (0)