Skip to content

Commit 5ac4a45

Browse files
committed
Use hlint and hformat.
1 parent f8a67db commit 5ac4a45

File tree

7 files changed

+229
-160
lines changed

7 files changed

+229
-160
lines changed

2019/01.hs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import Text.ParserCombinators.Parsec
1+
import Text.ParserCombinators.Parsec
22

33
fuelRequired :: Int -> Int
44
fuelRequired mass = mass `div` 3 - 2
55

66
fuelRequired_ :: Int -> Int
7-
fuelRequired_ mass = if f > 0 then f + fuelRequired_ f else 0
8-
where f = fuelRequired mass
7+
fuelRequired_ mass =
8+
if f > 0
9+
then f + fuelRequired_ f
10+
else 0
11+
where
12+
f = fuelRequired mass
913

1014
int :: Parser Int
1115
int = read <$> many1 digit
@@ -14,5 +18,5 @@ main :: IO ()
1418
main = do
1519
contents <- getContents
1620
let ms = parse (sepEndBy int newline) "" contents
17-
putStrLn . show $ sum . map fuelRequired <$> ms
18-
putStrLn . show $ sum . map fuelRequired_ <$> ms
21+
print $ sum . map fuelRequired <$> ms
22+
print $ sum . map fuelRequired_ <$> ms

2019/02.hs

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
import qualified Data.Map.Strict as M
2-
import Text.ParserCombinators.Parsec
1+
import qualified Data.Map.Strict as M
2+
import Text.ParserCombinators.Parsec
33

4-
5-
data Instruction = Done | Add | Mul deriving Show
4+
data Instruction
5+
= Done
6+
| Add
7+
| Mul
8+
deriving (Show)
69

710
applyInstruction :: Instruction -> Int -> Int -> Maybe Int
811
applyInstruction Add x y = Just (x + y)
912
applyInstruction Mul x y = Just (x * y)
10-
applyInstruction _ _ _ = Nothing
13+
applyInstruction _ _ _ = Nothing
1114

1215
type Position = Int
13-
data Memory = Memory (M.Map Position Int) deriving Show
16+
17+
newtype Memory =
18+
Memory (M.Map Position Int)
19+
deriving (Show)
1420

1521
instructionFromNumber :: Int -> Maybe Instruction
16-
instructionFromNumber 1 = Just Add
17-
instructionFromNumber 2 = Just Mul
22+
instructionFromNumber 1 = Just Add
23+
instructionFromNumber 2 = Just Mul
1824
instructionFromNumber 99 = Just Done
19-
instructionFromNumber _ = Nothing
25+
instructionFromNumber _ = Nothing
2026

2127
instructionAt :: Position -> Memory -> Maybe Instruction
2228
instructionAt pos (Memory mem) = do
@@ -35,31 +41,35 @@ setMemory :: Position -> Int -> Memory -> Memory
3541
setMemory pos val (Memory mem) = Memory (M.insert pos val mem)
3642

3743
execute :: Position -> Memory -> Maybe Memory
38-
execute pos mem = case (instructionAt pos mem) of
39-
Nothing -> Nothing
40-
Just Done -> Just mem
41-
Just op -> do
42-
lhs <- dereferenceAt (pos + 1) mem
43-
rhs <- dereferenceAt (pos + 2) mem
44-
dst <- valueAt (pos + 3) mem
45-
val <- applyInstruction op lhs rhs
46-
execute (pos + 4) (setMemory dst val mem)
44+
execute pos mem =
45+
case instructionAt pos mem of
46+
Nothing -> Nothing
47+
Just Done -> Just mem
48+
Just op -> do
49+
lhs <- dereferenceAt (pos + 1) mem
50+
rhs <- dereferenceAt (pos + 2) mem
51+
dst <- valueAt (pos + 3) mem
52+
val <- applyInstruction op lhs rhs
53+
execute (pos + 4) (setMemory dst val mem)
4754

4855
modify :: Int -> Int -> Memory -> Memory
4956
modify noun verb (Memory mem) = Memory (M.insert 1 noun (M.insert 2 verb mem))
5057

51-
checkSolution :: Int -> Int -> Int -> Memory -> Maybe Bool
58+
checkSolution :: Int -> Int -> Int -> Memory -> Maybe Bool
5259
checkSolution target noun verb memory = do
5360
res <- execute 0 (modify noun verb memory)
5461
val <- valueAt 0 res
55-
return (if (target == val) then True else False)
62+
pure (target == val)
5663

5764
findSolution :: Int -> Memory -> (Int, Int)
58-
findSolution target mem = head (dropWhile isNotSolution [(n, v) | n <- [0..100], v <- [0..100]])
59-
where isNotSolution :: (Int, Int) -> Bool
60-
isNotSolution (noun, verb) = case (checkSolution target noun verb mem) of
61-
Nothing -> True -- meh!
62-
(Just b) -> not b
65+
findSolution target mem =
66+
head (dropWhile isNotSolution [(n, v) | n <- [0 .. 100], v <- [0 .. 100]])
67+
where
68+
isNotSolution :: (Int, Int) -> Bool
69+
isNotSolution (noun, verb) =
70+
case checkSolution target noun verb mem of
71+
Nothing -> True -- meh!
72+
(Just b) -> not b
6373

6474
-- Parser
6575
int :: Parser Int
@@ -68,16 +78,13 @@ int = read <$> many1 digit
6878
memory :: Parser Memory
6979
memory = do
7080
code <- sepBy int (char ',')
71-
return (Memory (M.fromList (zip [0..] code)))
81+
pure (Memory (M.fromList (zip [0 ..] code)))
7282

7383
-- Main
7484
main :: IO ()
7585
main = do
7686
contents <- getContents
7787
let mem = parse memory "" contents
78-
let res = (execute 0) . (modify 12 2) <$> mem
79-
putStrLn $ case res of
80-
(Left err) -> show err
81-
(Right (Just m)) -> show $ valueAt 0 m
82-
_ -> "Nothing"
83-
putStrLn . show $ findSolution 19690720 <$> mem
88+
let res = execute 0 . modify 12 2 <$> mem
89+
print $ (\mem -> valueAt 0 <$> mem) <$> res
90+
print $ findSolution 19690720 <$> mem

2019/03.hs

+48-34
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,79 @@
1-
import qualified Data.List as List
2-
import qualified Data.Map.Strict as Map
3-
import qualified Data.Set as Set
4-
import Text.ParserCombinators.Parsec
5-
6-
data Point = Point { x :: Int,
7-
y :: Int } deriving (Eq, Ord, Show)
8-
9-
data Direction = UP | RIGHT | DOWN | LEFT deriving (Eq, Ord, Show)
10-
11-
data Instruction = Instruction Direction Int deriving Show
12-
13-
data Step = Step { idx :: Int,
14-
ins :: Instruction } deriving Show
1+
import Data.Functor (($>))
2+
import qualified Data.List as List
3+
import qualified Data.Map.Strict as Map
4+
import qualified Data.Set as Set
5+
import Text.ParserCombinators.Parsec
6+
7+
data Point =
8+
Point
9+
{ x :: Int
10+
, y :: Int
11+
}
12+
deriving (Eq, Ord, Show)
13+
14+
data Direction
15+
= UP
16+
| RIGHT
17+
| DOWN
18+
| LEFT
19+
deriving (Eq, Ord, Show)
20+
21+
data Instruction =
22+
Instruction Direction Int
23+
deriving (Show)
24+
25+
data Step =
26+
Step
27+
{ idx :: Int
28+
, ins :: Instruction
29+
}
30+
deriving (Show)
1531

1632
movePoint :: Point -> Direction -> Point
17-
movePoint p UP = p { y = y p + 1}
18-
movePoint p RIGHT = p { x = x p + 1}
19-
movePoint p DOWN = p { y = y p - 1}
20-
movePoint p LEFT = p { x = x p - 1}
33+
movePoint p UP = p {y = y p + 1}
34+
movePoint p RIGHT = p {x = x p + 1}
35+
movePoint p DOWN = p {y = y p - 1}
36+
movePoint p LEFT = p {x = x p - 1}
2137

2238
unrollInstruction :: Instruction -> [Direction]
2339
unrollInstruction (Instruction dir cnt) = replicate cnt dir
2440

2541
unrollInstructions :: [Instruction] -> [Direction]
26-
unrollInstructions = concat . List.map unrollInstruction
42+
unrollInstructions = List.concatMap unrollInstruction
2743

2844
wirePoints :: [Instruction] -> [Point]
2945
wirePoints ins = scanl movePoint (Point 0 0) (unrollInstructions ins)
3046

3147
stepMap :: [Point] -> Map.Map Point Int
32-
stepMap ps = Map.fromListWith min (zip ps [1..])
48+
stepMap ps = Map.fromListWith min (zip ps [1 ..])
3349

3450
intersectStepMaps :: Map.Map Point Int -> Map.Map Point Int -> Map.Map Point Int
35-
intersectStepMaps m1 m2 = Map.intersectionWith (\a b -> a + b) m1 m2
51+
intersectStepMaps = Map.intersectionWith (+)
3652

3753
manhattenDist :: Point -> Int
38-
manhattenDist (Point x y) = (abs x) + (abs y)
54+
manhattenDist (Point x y) = abs x + abs y
3955

4056
prepareMap :: [Instruction] -> [Instruction] -> Map.Map Point Int
4157
prepareMap ins1 ins2 = intersectStepMaps m1 m2
42-
where m1 = convert ins1
43-
m2 = convert ins2
44-
convert = stepMap . tail . wirePoints
58+
where
59+
m1 = convert ins1
60+
m2 = convert ins2
61+
convert = stepMap . tail . wirePoints
4562

4663
-- Parser
4764
int :: Parser Int
4865
int = read <$> many1 digit
4966

5067
instruction :: Parser Instruction
5168
instruction = do
52-
direction <- char 'U' *> pure UP
53-
<|> char 'R' *> pure RIGHT
54-
<|> char 'D' *> pure DOWN
55-
<|> char 'L' *> pure LEFT
56-
count <- int
57-
pure (Instruction direction count)
69+
direction <-
70+
choice
71+
[char 'U' $> UP, char 'R' $> RIGHT, char 'D' $> DOWN, char 'L' $> LEFT]
72+
Instruction direction <$> int
5873

5974
instructions :: Parser [Instruction]
6075
instructions = sepBy instruction (char ',')
6176

62-
6377
-- Solvers
6478
part1 :: Map.Map Point Int -> Int
6579
part1 m = List.minimum (List.map manhattenDist (Map.keys m))
@@ -75,5 +89,5 @@ main = do
7589
let ins1 = head <$> wireInstructions
7690
let ins2 = head . tail <$> wireInstructions
7791
let smap = prepareMap <$> ins1 <*> ins2
78-
putStrLn . show $ part1 <$> smap
79-
putStrLn . show $ part2 <$> smap
92+
print $ part1 <$> smap
93+
print $ part2 <$> smap

2019/04.hs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import Text.ParserCombinators.Parsec
2-
import qualified Data.List as List
1+
import qualified Data.List as List
2+
import Text.ParserCombinators.Parsec
33

44
digits :: Int -> [Int]
55
digits i = List.reverse (rdigits i)
6-
where rdigits 0 = []
7-
rdigits x = (x `mod` 10):(rdigits (x `div` 10))
6+
where
7+
rdigits 0 = []
8+
rdigits x = x `mod` 10 : rdigits (x `div` 10)
89

910
-- Part 1
1011
groupLengths :: [Int] -> [Int]
@@ -14,24 +15,25 @@ hasMatchingAdjacentDigits :: [Int] -> Bool
1415
hasMatchingAdjacentDigits ds = List.any (>= 2) (groupLengths ds)
1516

1617
isMonotonous :: [Int] -> Bool
17-
isMonotonous (x:y:rest) = (x <= y) && (isMonotonous (y:rest))
18-
isMonotonous _ = True
18+
isMonotonous (x:y:rest) = x <= y && isMonotonous (y : rest)
19+
isMonotonous _ = True
1920

2021
isCandidate :: Int -> Bool
21-
isCandidate i = (List.length ds == 6)
22-
&& (hasMatchingAdjacentDigits ds)
23-
&& (isMonotonous ds)
24-
where ds = digits i
22+
isCandidate i =
23+
List.length ds == 6 && hasMatchingAdjacentDigits ds && isMonotonous ds
24+
where
25+
ds = digits i
2526

2627
countInRange :: (Int -> Bool) -> (Int, Int) -> Int
27-
countInRange pred (left, right) = List.sum (List.map (fromEnum . pred) [left..right])
28+
countInRange pred (left, right) =
29+
List.sum (List.map (fromEnum . pred) [left .. right])
2830

2931
-- Part 2
3032
hasTwoMatchingAdjacentDigits :: [Int] -> Bool
31-
hasTwoMatchingAdjacentDigits ds = List.any (== 2) (groupLengths ds)
33+
hasTwoMatchingAdjacentDigits ds = 2 `List.elem` groupLengths ds
3234

3335
isCandidate2 :: Int -> Bool
34-
isCandidate2 i = (isCandidate i) && (hasTwoMatchingAdjacentDigits (digits i))
36+
isCandidate2 i = isCandidate i && hasTwoMatchingAdjacentDigits (digits i)
3537

3638
-- Parser
3739
int :: Parser Int
@@ -48,5 +50,5 @@ main :: IO ()
4850
main = do
4951
contents <- getContents
5052
let r = parse range "" contents
51-
putStrLn . show $ (countInRange isCandidate) <$> r
52-
putStrLn . show $ (countInRange isCandidate2) <$> r
53+
print $ countInRange isCandidate <$> r
54+
print $ countInRange isCandidate2 <$> r

0 commit comments

Comments
 (0)