Compare commits

..

1 Commits

Author SHA1 Message Date
85e3fc9694 day 9
very slow very swag

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-10 00:18:59 +05:30
3 changed files with 1 additions and 42 deletions

1
README
View File

@@ -1 +0,0 @@
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.

View File

@@ -1,40 +0,0 @@
{-# 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]

View File

@@ -33,7 +33,7 @@ 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
| otherwise = (tx + signum tx, ty + signum dy) -- move diagonally
where
dx = hx - tx
dy = hy - ty