repositories
/
aoc20.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
e789e8b
)
make a bit better
author
Mart Lubbers
<mart@martlubbers.net>
Mon, 7 Dec 2020 06:27:55 +0000
(07:27 +0100)
committer
Mart Lubbers
<mart@martlubbers.net>
Mon, 7 Dec 2020 06:27:55 +0000
(07:27 +0100)
7/one.icl
patch
|
blob
|
history
diff --git
a/7/one.icl
b/7/one.icl
index
d1b2fba
..
0c3b8a1
100644
(file)
--- a/
7/one.icl
+++ b/
7/one.icl
@@
-2,10
+2,12
@@
module one
import StdEnv
import Data.Func
import StdEnv
import Data.Func
-import qualified Data.Map
-from Data.Map import :: Map(..), fromList, get, find, keys, foldrWithKey, findWithDefault
+import Data.Map => qualified updateAt
import Text
import Text
+
+//A bit cheaty because I removed all occurances of bag and bags from the input
+
read :: *File -> [Char]
read f
# (ok, l, f) = freadc f
read :: *File -> [Char]
read f
# (ok, l, f) = freadc f
@@
-15,15
+17,17
@@
read f
Start w
# (io, w) = stdio w
# bag = fromList (makeBag (read io))
Start w
# (io, w) = stdio w
# bag = fromList (makeBag (read io))
- = (one ['shiny gold'] bag,
two bag ['shiny gold']-1
)
+ = (one ['shiny gold'] bag,
dec $ two ['shiny gold'] bag
)
-one color bag =
length $ filter id $ map (canContain bag color) $ filter ((<>)
color) $ keys bag
-two
bag color = 1+sum[n*two bag c
\\(n, c)<-findWithDefault [] color bag]
+one color bag =
dec $ length $ filter id $ map (canContain bag
color) $ keys bag
+two
color bag = inc $ sum [n*two c bag
\\(n, c)<-findWithDefault [] color bag]
makeBag :: [Char] -> [([Char], [(Int, [Char])])]
makeBag ls = [(b, mkItem c)\\[b,c]<-map (split [' contain ']) $ split ['.\n'] ls]
where
mkItem :: [Char] -> [(Int, [Char])]
makeBag :: [Char] -> [([Char], [(Int, [Char])])]
makeBag ls = [(b, mkItem c)\\[b,c]<-map (split [' contain ']) $ split ['.\n'] ls]
where
mkItem :: [Char] -> [(Int, [Char])]
- mkItem s = [(toInt (toString x), join [' '] xs)\\[x:xs]<-map (split [' '])
(split [', '] s)
]
+ mkItem s = [(toInt (toString x), join [' '] xs)\\[x:xs]<-map (split [' '])
$ split [', '] s
]
-canContain bag color name = or [name == color:[canContain bag color child\\(n, child)<-findWithDefault [] name bag]]
+canContain :: (Map [Char] [(Int,[Char])]) [Char] [Char] -> Bool
+canContain bag color name
+ = or [name == color:map (canContain bag color o snd) $ findWithDefault [] name bag]