{-# Language ImportQualifiedPost #-}
module Main where
import Advent (getInputLines)
import Control.Monad ((<=<), zipWithM_)
import Control.Monad.ST (ST, runST)
import Data.Bits (xor)
import Data.Char (ord, chr)
import Data.Foldable (for_)
import Data.List (foldl1')
import Data.List.Split (chunksOf, splitOn)
import Text.Printf (printf)
import Data.Vector.Unboxed qualified as V
import Data.Vector.Unboxed.Mutable qualified as M
import KnotHash (knotHash, tieKnots)
main :: IO ()
IO ()
main =
do [String
inputLine] <- Int -> Int -> IO [String]
getInputLines Int
2017 Int
10
String -> IO ()
putStrLn (String -> String
part1 String
inputLine)
String -> IO ()
putStrLn (String -> String
part2 String
inputLine)
part1 ::
String ->
String
part1 :: String -> String
part1 = Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> (String -> Integer) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product ([Integer] -> Integer)
-> (String -> [Integer]) -> String -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Integer) -> [Word8] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger ([Word8] -> [Integer])
-> (String -> [Word8]) -> String -> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Word8] -> [Word8]
forall a. Int -> [a] -> [a]
take Int
2 ([Word8] -> [Word8]) -> (String -> [Word8]) -> String -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [Word8]
tieKnots ([Int] -> [Word8]) -> (String -> [Int]) -> String -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Int]
part1Input
part2 ::
String ->
String
part2 :: String -> String
part2 = String -> Integer -> String
forall r. PrintfType r => String -> r
printf String
"%032x" (Integer -> String) -> (String -> Integer) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Integer
knotHash
part1Input ::
String ->
[Int]
part1Input :: String -> [Int]
part1Input = (String -> Int) -> [String] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map String -> Int
forall a. Read a => String -> a
read ([String] -> [Int]) -> (String -> [String]) -> String -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> [String]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn String
","
part2Input ::
String ->
[Int]
part2Input :: String -> [Int]
part2Input String
str = (Char -> Int) -> String -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Int
ord String
str [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int
17, Int
31, Int
73, Int
47, Int
23]