Skip to content

Commit a40e258

Browse files
committed
day18
1 parent 023ed4e commit a40e258

File tree

9 files changed

+552
-10
lines changed

9 files changed

+552
-10
lines changed

2020/folivetti/day17/src/Main.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ parse = go 0 0 S.empty
1616
go _ _ s [] = s
1717

1818
-- * Update Automata
19-
update :: (Set Coord -> M.Map Coord Int) -> Set Coord -> Set Coord
20-
update neigh s = S.union s1 s2
19+
update :: [Coord] -> Set Coord -> Set Coord
20+
update ds s = S.union s1 s2
2121
where
22-
ns = neigh s
22+
ns = neighbors ds s
2323
isActive xs = M.keysSet . M.filter (`elem` xs)
2424
-- active neighborhood
2525
s1 = isActive [2,3] $ M.restrictKeys ns s
@@ -30,10 +30,10 @@ update neigh s = S.union s1 s2
3030
-- active coordinate and their neighbors
3131
neighbors :: [Coord] -> Set Coord -> M.Map Coord Int
3232
neighbors ds s = M.unionsWith (+)
33-
$ map (forKeys m . sumCoord) ds
33+
$ map (forKeys m . sumCoord) ds -- each map has a coord as key and the value is the number of active neighbors
3434
where
35-
forKeys = flip M.mapKeys
36-
m = M.fromSet (const 1) s
35+
forKeys = flip M.mapKeys -- for each key (coord) from a map, apply a function
36+
m = M.fromSet (const 1) s -- every active cell has value 1
3737

3838
sumCoord :: Coord -> Coord -> Coord
3939
sumCoord (x,y,z,w) (a,b,c,d) = (x+a,y+b,z+c,w+d)
@@ -50,9 +50,7 @@ main :: IO ()
5050
main = do
5151
dat <- readFile "day17.txt"
5252
let s0 = parse dat
53-
n3D = neighbors deltas
54-
n4D = neighbors deltas4D
55-
sets = iterate (update n3D) s0
56-
sets4D = iterate (update n4D) s0
53+
sets = iterate (update deltas) s0
54+
sets4D = iterate (update deltas4D) s0
5755
print $ S.size (sets !! 6)
5856
print $ S.size (sets4D !! 6)

2020/folivetti/day18/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/day18/README.md

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

2020/folivetti/day18/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/day18/day18.cabal

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: day18
2+
version: 0.1.0.0
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/githubuser/day18#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 day18
17+
hs-source-dirs: src
18+
main-is: Main.hs
19+
default-language: Haskell2010
20+
build-depends: base >= 4.7 && < 5

2020/folivetti/day18/day18.txt

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

2020/folivetti/day18/src/Main.hs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Main where
2+
3+
import Data.Char
4+
5+
data Op = Nop | Rgt | Mul | Add | Lft deriving (Show, Eq, Ord)
6+
7+
evalExpr :: Bool -> String -> Integer
8+
evalExpr p expr = go expr [] []
9+
where
10+
go [] vals [] = head vals
11+
go [] vals ops = head $ fst $ update p Nop vals ops
12+
go (c:cs) vals ops
13+
| isDigit c = go cs (read [c]:vals) ops
14+
| c == '(' = go cs vals (Lft:ops)
15+
| c == ')' = uncurry (go cs) $ update p Rgt vals ops
16+
| c == '+' = uncurry (go cs) $ update p Add vals ops
17+
| c == '*' = uncurry (go cs) $ update p Mul vals ops
18+
| otherwise = go cs vals ops
19+
20+
update :: Bool -> Op -> [Integer] -> [Op] -> ([Integer],[Op])
21+
-- make Add and Mul have the same precedence
22+
update False Add vals (Mul:ops) = update False Add (calc Mul vals) ops
23+
24+
update _ cur vals [] = (vals, [cur])
25+
update _ Rgt vals (Lft:ops) = (vals, ops)
26+
update _ cur vals (Lft:ops) = (vals, cur:Lft:ops)
27+
update p cur vals (op:ops)
28+
| op >= cur = update p cur (calc op vals) ops
29+
| otherwise = (vals, cur:op:ops)
30+
31+
calc Add (v1:v2:vs) = v1+v2:vs
32+
calc Mul (v1:v2:vs) = v1*v2:vs
33+
34+
main :: IO ()
35+
main = do
36+
exprs <- lines <$> readFile "day18.txt"
37+
print $ foldr (\e acc -> evalExpr False e + acc) 0 exprs
38+
print $ foldr (\e acc -> evalExpr True e + acc) 0 exprs

2020/folivetti/day18/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/day18/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)