Compare commits
2 Commits
85e3fc9694
...
752e5c1f34
| Author | SHA1 | Date | |
|---|---|---|---|
| 752e5c1f34 | |||
| a618986704 |
1
README
Normal file
1
README
Normal file
@@ -0,0 +1 @@
|
||||
My advent of code solutions for 2022. I am trying to not use String wherever possible and use Data.Text instead. That makes the programs appear bigger than they are LOC-wise.
|
||||
40
day10.hs
Normal file
40
day10.hs
Normal file
@@ -0,0 +1,40 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T (lines, pack, unlines, words)
|
||||
import qualified Data.Text.IO as T (putStr)
|
||||
import qualified Data.Text.Read as T (decimal, signed)
|
||||
import Lib (readFile')
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
vals <-
|
||||
scanl1 (+)
|
||||
. reverse
|
||||
. foldl
|
||||
( \xs x ->
|
||||
let o : n = T.words x
|
||||
dec (T.signed T.decimal . head -> Right (m, "")) = m
|
||||
in if o == "noop" then 0 : xs else dec n : 0 : xs
|
||||
)
|
||||
[1]
|
||||
. T.lines
|
||||
<$> readFile' "day10.in"
|
||||
putStr "Q1: "
|
||||
print $ q1 vals
|
||||
putStrLn "Q2: "
|
||||
T.putStr $ q2 vals
|
||||
|
||||
q1 :: [Int] -> Int
|
||||
q1 v = foldr (\x xs -> xs + (v !! (x - 1) * x)) 0 [20, 60 .. 220]
|
||||
|
||||
q2 :: [Int] -> Text
|
||||
q2 v =
|
||||
T.unlines $
|
||||
map
|
||||
( \x ->
|
||||
T.pack $
|
||||
map (\y -> if abs (v !! (x + y) - y) <= 1 then '#' else '.') [0 .. 39]
|
||||
)
|
||||
[0, 40 .. 200]
|
||||
48
day9.hs
Normal file
48
day9.hs
Normal file
@@ -0,0 +1,48 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
|
||||
import Data.List (nub, scanl)
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T (lines, unpack, words)
|
||||
import qualified Data.Text.Read as T (decimal)
|
||||
import Lib (readFile')
|
||||
|
||||
-- a rather slow program, takes 300 ms on my system (with `time`, but idc anymore 😭
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <-
|
||||
concatMap
|
||||
((\[T.unpack -> [d], T.decimal -> Right (n, "")] -> replicate n d) . T.words)
|
||||
. T.lines
|
||||
<$> readFile' "day9.in"
|
||||
putStr "Q1: "
|
||||
print $ q 1 input
|
||||
putStr "Q2: "
|
||||
print $ q 9 input
|
||||
|
||||
moveHead :: Char -> (Int, Int) -> (Int, Int)
|
||||
moveHead d (x, y) = case d of
|
||||
'L' -> (x - 1, y)
|
||||
'R' -> (x + 1, y)
|
||||
'U' -> (x, y + 1)
|
||||
'D' -> (x, y - 1)
|
||||
|
||||
moveTail :: (Int, Int) -> (Int, Int) -> (Int, Int)
|
||||
moveTail (hx, hy) (tx, ty)
|
||||
| abs dx <= 1 && abs dy <= 1 = (tx, ty) -- dont move
|
||||
| abs dx > abs dy = (tx + signum dx, hy) -- move horizontally
|
||||
| abs dy > abs dx = (hx, ty + signum dy) -- move vertically
|
||||
| otherwise = (tx + signum dx, ty + signum dy ) -- move diagonally
|
||||
where
|
||||
dx = hx - tx
|
||||
dy = hy - ty
|
||||
|
||||
q :: Int -> [Char] -> Int
|
||||
q n =
|
||||
length . nub . map last
|
||||
. scanl
|
||||
( \(h : t) d ->
|
||||
reverse $ foldl (\(h : xs) x -> moveTail h x : (h : xs)) [moveHead d h] t
|
||||
)
|
||||
(replicate (n + 1) (0, 0))
|
||||
Reference in New Issue
Block a user