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]
|
||||||
2
day9.hs
2
day9.hs
@@ -33,7 +33,7 @@ moveTail (hx, hy) (tx, ty)
|
|||||||
| abs dx <= 1 && abs dy <= 1 = (tx, ty) -- dont move
|
| abs dx <= 1 && abs dy <= 1 = (tx, ty) -- dont move
|
||||||
| abs dx > abs dy = (tx + signum dx, hy) -- move horizontally
|
| abs dx > abs dy = (tx + signum dx, hy) -- move horizontally
|
||||||
| abs dy > abs dx = (hx, ty + signum dy) -- move vertically
|
| abs dy > abs dx = (hx, ty + signum dy) -- move vertically
|
||||||
| otherwise = (tx + signum tx, ty + signum dy) -- move diagonally
|
| otherwise = (tx + signum dx, ty + signum dy ) -- move diagonally
|
||||||
where
|
where
|
||||||
dx = hx - tx
|
dx = hx - tx
|
||||||
dy = hy - ty
|
dy = hy - ty
|
||||||
|
|||||||
Reference in New Issue
Block a user