projects
/
advent-of-code-18.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
63ff5f7
)
Tidying day 2
author
Neil Smith
<neil.git@njae.me.uk>
Sun, 2 Dec 2018 11:57:39 +0000
(11:57 +0000)
committer
Neil Smith
<neil.git@njae.me.uk>
Sun, 2 Dec 2018 11:57:39 +0000
(11:57 +0000)
src/advent02/advent02.hs
patch
|
blob
|
history
diff --git
a/src/advent02/advent02.hs
b/src/advent02/advent02.hs
index f2408de7352118e1496da01425d5a2e818d86c77..c2bd90850f4b2b3c2a3e9478be8fe0752977665c 100644
(file)
--- a/
src/advent02/advent02.hs
+++ b/
src/advent02/advent02.hs
@@
-1,6
+1,5
@@
import Data.List
import Data.List
-
main :: IO ()
main = do
text <- readFile "data/advent02.txt"
main :: IO ()
main = do
text <- readFile "data/advent02.txt"
@@
-17,18
+16,12
@@
letterCounts = map length . group . sort
addCounts :: (Int, Int) -> [Int] -> (Int, Int)
addCounts (twos, threes) counts = (twos', threes')
addCounts :: (Int, Int) -> [Int] -> (Int, Int)
addCounts (twos, threes) counts = (twos', threes')
- where twos' = if has2 counts then twos + 1 else twos
- threes' = if has3 counts then threes + 1 else threes
-
-has2 = elem 2
-has3 = elem 3
+ where twos' = if 2 `elem` counts then twos + 1 else twos
+ threes' = if 3 `elem` counts then threes + 1 else threes
-part2 ids = uncurry sameChars closeIds
- where closeIds = head $ filter (\ab -> uncurry differenceCount ab == 1) [(a, b) | a <- ids, b <- ids]
+part2 ids = uncurry intersect closeIds
+ where closeIds = head $ filter (\ab -> uncurry differenceCount ab == 1)
+ [(a, b) | a:rest <- tails ids, b <- rest]
differenceCount :: String -> String -> Int
differenceCount this that = length $ filter (\(a, b) -> a /= b) $ zip this that
differenceCount :: String -> String -> Int
differenceCount this that = length $ filter (\(a, b) -> a /= b) $ zip this that
-
-sameChars :: String -> String -> String
-sameChars this that = map fst $ filter (\(a, b) -> a == b) $ zip this that
-