Skip to content

Commit 399ffbc

Browse files
committed
migrate over 2016
1 parent 802d58f commit 399ffbc

32 files changed

+1453
-4
lines changed

2016/AOC2016.hs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{-# OPTIONS_GHC -Wno-dodgy-exports #-}
2+
{-# OPTIONS_GHC -Wno-unused-imports #-}
3+
4+
-- |
5+
-- Module : AOC2016
6+
-- Copyright : (c) Justin Le 2021
7+
-- License : BSD3
8+
--
9+
-- Maintainer : justin@jle.im
10+
-- Stability : experimental
11+
-- Portability : non-portable
12+
--
13+
-- Gather together all challenges and collect them into a single map.
14+
module AOC2016 (
15+
module AOC,
16+
challengeBundle2016,
17+
)
18+
where
19+
20+
import AOC.Discover
21+
import AOC.Run
22+
import AOC.Run.Interactive
23+
import AOC2016.Day01 as AOC
24+
import AOC2016.Day02 as AOC
25+
import AOC2016.Day03 as AOC
26+
import AOC2016.Day04 as AOC
27+
import AOC2016.Day05 as AOC
28+
import AOC2016.Day06 as AOC
29+
import AOC2016.Day07 as AOC
30+
import AOC2016.Day08 as AOC
31+
import AOC2016.Day09 as AOC
32+
import AOC2016.Day10 as AOC
33+
import AOC2016.Day11 as AOC
34+
import AOC2016.Day12 as AOC
35+
import AOC2016.Day13 as AOC
36+
import AOC2016.Day14 as AOC
37+
import AOC2016.Day15 as AOC
38+
import AOC2016.Day16 as AOC
39+
import AOC2016.Day17 as AOC
40+
import AOC2016.Day18 as AOC
41+
import AOC2016.Day19 as AOC
42+
import AOC2016.Day20 as AOC
43+
import AOC2016.Day21 as AOC
44+
import AOC2016.Day22 as AOC
45+
import AOC2016.Day23 as AOC
46+
import AOC2016.Day24 as AOC
47+
import AOC2016.Day25 as AOC
48+
49+
challengeBundle2016 :: ChallengeBundle
50+
challengeBundle2016 = CB 2016 $ mkChallengeMap $$(solutionList)

2016/AOC2016/Day01.hs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- |
2+
-- Module : AOC2016.Day01
3+
-- License : BSD3
4+
--
5+
-- Stability : experimental
6+
-- Portability : non-portable
7+
--
8+
-- Day 1. See "AOC.Solver" for the types used in this module!
9+
module AOC2016.Day01 (
10+
day01a,
11+
day01b,
12+
) where
13+
14+
import AOC.Common (firstRepeated)
15+
import AOC.Common.Point (Dir (..), Point, dirPoint, mannDist, parseDir)
16+
import AOC.Solver ((:~>) (..))
17+
import Data.List (foldl')
18+
import Text.Read (readMaybe)
19+
20+
data Turtle = (:@)
21+
{ tLoc :: Point
22+
, tDir :: Dir
23+
}
24+
deriving stock (Show)
25+
26+
data Command
27+
= CTurn Dir
28+
| CGo
29+
deriving stock (Show)
30+
31+
stepper :: Turtle -> Command -> Turtle
32+
stepper (x :@ h) = \case
33+
CTurn d -> (x + dirPoint h') :@ h'
34+
where
35+
h' = d <> h
36+
CGo -> (x + dirPoint h) :@ h
37+
38+
day01a :: [Command] :~> Int
39+
day01a =
40+
MkSol
41+
{ sParse = parseCmd
42+
, sShow = show
43+
, sSolve =
44+
Just
45+
. mannDist 0
46+
. tLoc
47+
. foldl' stepper (0 :@ North)
48+
}
49+
50+
day01b :: [Command] :~> Int
51+
day01b =
52+
MkSol
53+
{ sParse = parseCmd
54+
, sShow = show
55+
, sSolve =
56+
fmap (mannDist 0)
57+
. firstRepeated
58+
. map tLoc
59+
. scanl stepper (0 :@ North)
60+
}
61+
62+
parseCmd :: String -> Maybe [Command]
63+
parseCmd str = Just $ do
64+
x : xs <- words . filter (/= ',') $ str
65+
Just h <- pure $ parseDir x
66+
Just n <- pure $ readMaybe xs
67+
CTurn h : replicate (n - 1) CGo

2016/AOC2016/Day02.hs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- |
2+
-- Module : AOC2016.Day02
3+
-- License : BSD3
4+
--
5+
-- Stability : experimental
6+
-- Portability : non-portable
7+
--
8+
-- Day 2. See "AOC.Solver" for the types used in this module!
9+
module AOC2016.Day02 (
10+
day02a,
11+
day02b,
12+
) where
13+
14+
import AOC.Common.Point (Dir (..), Point, dirPoint, parseDir)
15+
import AOC.Solver ((:~>) (..))
16+
import Data.List (foldl', mapAccumL)
17+
import Data.Map (Map)
18+
import qualified Data.Map as M
19+
import Data.Maybe (mapMaybe)
20+
import Linear (V2 (..))
21+
22+
stepper :: Map Point Char -> Point -> [Dir] -> (Point, Maybe Char)
23+
stepper mp x =
24+
(\r -> (r, M.lookup r mp))
25+
. foldl' move x
26+
where
27+
move p d
28+
| p' `M.member` mp = p'
29+
| otherwise = p
30+
where
31+
p' = p + dirPoint d
32+
33+
keypadA :: Map Point Char
34+
keypadA =
35+
M.fromList . flip zip ['1' ..] $
36+
[ V2 (-1) 1
37+
, V2 0 1
38+
, V2 1 1
39+
, V2 (-1) 0
40+
, V2 0 0
41+
, V2 1 0
42+
, V2 (-1) (-1)
43+
, V2 0 (-1)
44+
, V2 1 (-1)
45+
]
46+
47+
day02a :: [[Dir]] :~> String
48+
day02a =
49+
MkSol
50+
{ sParse = Just . (map . mapMaybe) parseDir . lines
51+
, sShow = id
52+
, sSolve = sequence . snd . mapAccumL (stepper keypadA) 0
53+
}
54+
55+
keypadB :: Map Point Char
56+
keypadB =
57+
M.fromList . flip zip (['1' .. '9'] ++ ['A' ..]) $
58+
[ V2 0 2
59+
, V2 (-1) 1
60+
, V2 0 1
61+
, V2 1 1
62+
, V2 (-2) 0
63+
, V2 (-1) 0
64+
, V2 0 0
65+
, V2 1 0
66+
, V2 2 0
67+
, V2 (-1) (-1)
68+
, V2 0 (-1)
69+
, V2 1 (-1)
70+
, V2 0 (-2)
71+
]
72+
73+
day02b :: [[Dir]] :~> String
74+
day02b =
75+
MkSol
76+
{ sParse = Just . (map . mapMaybe) parseDir . lines
77+
, sShow = id
78+
, sSolve = sequence . snd . mapAccumL (stepper keypadB) (V2 (-2) 0)
79+
}

2016/AOC2016/Day03.hs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- |
2+
-- Module : AOC2016.Day03
3+
-- License : BSD3
4+
--
5+
-- Stability : experimental
6+
-- Portability : non-portable
7+
--
8+
-- Day 3. See "AOC.Solver" for the types used in this module!
9+
module AOC2016.Day03 (
10+
day03a,
11+
day03b,
12+
) where
13+
14+
import AOC.Solver ((:~>) (..))
15+
import Data.List (sortBy, transpose)
16+
import Data.List.Split (chunksOf)
17+
import Text.Read (readMaybe)
18+
19+
isTriangle :: [Int] -> Bool
20+
isTriangle (sortBy (flip compare) -> (x : xs)) = sum xs > x
21+
isTriangle _ = False
22+
23+
day03a :: [[Int]] :~> Int
24+
day03a =
25+
MkSol
26+
{ sParse = traverse (traverse readMaybe . words) . lines
27+
, sShow = show
28+
, sSolve = Just . length . filter isTriangle
29+
}
30+
31+
day03b :: [[Int]] :~> _
32+
day03b =
33+
MkSol
34+
{ sParse = traverse (traverse readMaybe . words) . lines
35+
, sShow = show
36+
, sSolve =
37+
Just
38+
. length
39+
. filter isTriangle
40+
. concatMap transpose
41+
. chunksOf 3
42+
}

2016/AOC2016/Day04.hs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- |
2+
-- Module : AOC2016.Day04
3+
-- License : BSD3
4+
--
5+
-- Stability : experimental
6+
-- Portability : non-portable
7+
--
8+
-- Day 4. See "AOC.Solver" for the types used in this module!
9+
module AOC2016.Day04 (
10+
day04a,
11+
day04b,
12+
) where
13+
14+
import AOC.Common (caeser, firstJust, freqList)
15+
import AOC.Solver ((:~>) (..))
16+
import Control.Monad (guard)
17+
import Data.Finite (modulo)
18+
import Data.List (isInfixOf)
19+
import Data.List.Split (splitOneOf)
20+
import Data.Maybe (mapMaybe)
21+
import Text.Read (readMaybe)
22+
23+
data Room = Room
24+
{ rName :: [String]
25+
, rId :: Int
26+
}
27+
deriving stock (Show)
28+
29+
parseRoom :: String -> Maybe Room
30+
parseRoom str = do
31+
_ : c : n : rs <- Just . reverse . splitOneOf "-[]" $ str
32+
guard
33+
. all (uncurry (==))
34+
. zip c
35+
. map snd
36+
. freqList
37+
. concat
38+
$ rs
39+
Room (reverse rs) <$> readMaybe n
40+
41+
day04a :: _ :~> _
42+
day04a =
43+
MkSol
44+
{ sParse = Just . mapMaybe parseRoom . lines
45+
, sShow = show
46+
, sSolve = Just . sum . map rId
47+
}
48+
49+
day04b :: _ :~> _
50+
day04b =
51+
MkSol
52+
{ sParse = Just . mapMaybe parseRoom . lines
53+
, sShow = show
54+
, sSolve = firstJust $ \(Room n i) ->
55+
i <$ do
56+
guard $ "north" `isInfixOf` (concatMap . map) (caeser (modulo (fromIntegral i))) n
57+
}

2016/AOC2016/Day05.hs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- |
2+
-- Module : AOC2016.Day05
3+
-- License : BSD3
4+
--
5+
-- Stability : experimental
6+
-- Portability : non-portable
7+
--
8+
-- Day 5. See "AOC.Solver" for the types used in this module!
9+
module AOC2016.Day05 (
10+
day05a,
11+
day05b,
12+
) where
13+
14+
import AOC.Common (foldMapParChunk, hexDigit, splitWord, _ListTup)
15+
import AOC.Solver ((:~>) (..))
16+
import Control.Lens (review, view)
17+
import qualified Crypto.Hash as H
18+
import qualified Data.ByteArray as BA
19+
import qualified Data.ByteString as BS
20+
import Data.ByteString.Lens (packedChars)
21+
import Data.Finite (Finite, strengthenN)
22+
import Data.Foldable (toList)
23+
import Data.List (find, scanl')
24+
import Data.List.Split (chunksOf)
25+
import Data.Map (Map)
26+
import qualified Data.Map as M
27+
import Data.Maybe (maybeToList)
28+
29+
coolHash :: H.Context H.MD5 -> Int -> Maybe (Finite 16, Finite 16)
30+
coolHash ctx i = case concatMap (review _ListTup . splitWord) (BS.unpack hashed) of
31+
0 : 0 : 0 : 0 : 0 : x : y : _ -> Just (x, y)
32+
_ -> Nothing
33+
where
34+
hashed =
35+
BA.convert
36+
. H.hashFinalize
37+
. H.hashUpdate ctx
38+
. view (packedChars @BS.ByteString)
39+
$ show i
40+
41+
day05a :: H.Context H.MD5 :~> [Finite 16]
42+
day05a =
43+
MkSol
44+
{ sParse = Just . H.hashUpdate H.hashInit . view (packedChars @BS.ByteString)
45+
, sShow = map (review hexDigit)
46+
, sSolve = \ctx ->
47+
Just
48+
. take 8
49+
. (foldMap . foldMapParChunk 500_000)
50+
(maybeToList . fmap fst . coolHash ctx)
51+
$ chunksOf 10_000_000 [0 ..]
52+
}
53+
54+
coolHash2 :: H.Context H.MD5 -> Int -> Maybe (Finite 8, Finite 16)
55+
coolHash2 ctx i = do
56+
(x, y) <- coolHash ctx i
57+
k <- strengthenN x
58+
pure (k, y)
59+
60+
day05b :: H.Context H.MD5 :~> Map (Finite 8) (Finite 16)
61+
day05b =
62+
MkSol
63+
{ sParse = Just . H.hashUpdate H.hashInit . view (packedChars @BS.ByteString)
64+
, sShow = map (review hexDigit) . toList
65+
, sSolve = \ctx ->
66+
find ((== 8) . M.size)
67+
. scanl' (\mp (k, x) -> M.insertWith (const id) k x mp) M.empty
68+
. (foldMap . foldMapParChunk 500_000)
69+
(maybeToList . coolHash2 ctx)
70+
$ chunksOf 10_000_000 [0 ..]
71+
}

0 commit comments

Comments
 (0)