we hindent and hlint now
This commit is contained in:
parent
6b4dedfc17
commit
835cd57608
|
@ -1,9 +1,9 @@
|
|||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.List.Split
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map (map read . lines) <$> splitOn "\n\n" <$> readFile "input"
|
||||
input <- map (map read . lines) . splitOn "\n\n" <$> readFile "input"
|
||||
print $ day1a input
|
||||
print $ day1b input
|
||||
|
||||
|
|
|
@ -1,14 +1,19 @@
|
|||
data Hand = Rock | Paper | Scissor deriving (Eq,Enum,Bounded,Show)
|
||||
data Hand
|
||||
= Rock
|
||||
| Paper
|
||||
| Scissor
|
||||
deriving (Eq, Enum, Bounded, Show)
|
||||
|
||||
instance Ord Hand where
|
||||
compare x y = case (fromEnum x - fromEnum y) `mod` 3 of
|
||||
0 -> EQ
|
||||
1 -> GT
|
||||
_ -> LT
|
||||
compare x y =
|
||||
case (fromEnum x - fromEnum y) `mod` 3 of
|
||||
0 -> EQ
|
||||
1 -> GT
|
||||
_ -> LT
|
||||
|
||||
(+.) :: Hand -> Int -> Hand
|
||||
(+.) a b = toEnum $ flip mod 3 $ (+) (fromEnum a) b
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map (concat . words) . lines <$> readFile "input"
|
||||
|
@ -16,13 +21,17 @@ main = do
|
|||
print $ day2 realStrat input
|
||||
|
||||
toHands :: [Char] -> [Hand]
|
||||
toHands x = map toEnum $ (zipWith subtract [65,88]) (map fromEnum x)
|
||||
toHands x = map toEnum $ zipWith subtract [65, 88] (map fromEnum x)
|
||||
|
||||
realStrat :: [Hand] -> [Hand]
|
||||
realStrat (a:b:[]) = [a,a +. fromEnum (b +. 2)]
|
||||
realStrat [a, b] = [a, a +. fromEnum (b +. 2)]
|
||||
|
||||
day2 :: ([Hand] -> [Hand]) -> [[Char]] -> Int
|
||||
day2 f = sum . map (play . f . toHands)
|
||||
|
||||
play :: [Hand] -> Int
|
||||
play (x:y:[]) = fromEnum y + 1 + if x == y then 3 else fromEnum (x<y) * 6
|
||||
play [x, y] =
|
||||
fromEnum y + 1 +
|
||||
if x == y
|
||||
then 3
|
||||
else fromEnum (x < y) * 6
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import Data.List
|
||||
import Data.List.Split
|
||||
import Control.Arrow
|
||||
import Control.Lens
|
||||
import Data.List
|
||||
import Data.List.Split
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
|
@ -9,10 +9,18 @@ main = do
|
|||
print $ both %~ sum $ (day3a &&& day3b) input
|
||||
|
||||
day3a :: [String] -> [Int]
|
||||
day3a = map $ convert . uncurry intersect . (both %~ nub) . (flip splitAt <*> flip div 2 . length)
|
||||
day3a =
|
||||
map $
|
||||
convert .
|
||||
uncurry intersect . (both %~ nub) . (splitAt =<< flip div 2 . length)
|
||||
|
||||
day3b :: [String] -> [Int]
|
||||
day3b = map (convert . foldl1 intersect) . chunksOf 3
|
||||
|
||||
convert :: String -> Int
|
||||
convert = (\x->if x>90 then x - 96 else x - 38) . fromEnum . head
|
||||
convert =
|
||||
(\x ->
|
||||
if x > 90
|
||||
then x - 96
|
||||
else x - 38) .
|
||||
fromEnum . head
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.List.Split
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
|
|
|
@ -1,31 +1,37 @@
|
|||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.List.Split
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map lines <$> splitOn "\n\n" <$> readFile "input"
|
||||
input <- map lines . splitOn "\n\n" <$> readFile "input"
|
||||
let field = parseField $ head input
|
||||
let moves = parseMoves $ last input
|
||||
let moves = parseMoves $ last input
|
||||
print $ map last $ doMoves reverse moves field
|
||||
print $ map last $ doMoves id moves field
|
||||
|
||||
parseField :: [String] -> [String]
|
||||
parseField = filter (not.null) . map (takeWhile (/=' ') . reverse) . filter (null . intersect "[]") . transpose . init
|
||||
parseField =
|
||||
filter (not . null) .
|
||||
map (reverse . filter (not . flip elem "[] ")) . transpose . init
|
||||
|
||||
parseMoves :: [String] -> [[Int]]
|
||||
parseMoves = map (map (subtract 1 . read) . filter (not.null . intersect ['0'..'9']) . words)
|
||||
parseMoves =
|
||||
map
|
||||
(map (subtract 1 . read) .
|
||||
filter (not . null . intersect ['0' .. '9']) . words)
|
||||
|
||||
move :: (String -> String) -> [Int] -> [String] -> [String]
|
||||
move f (p:a:b:[]) field = newFieldA ++ [new] ++ newFieldB
|
||||
where line = field !! a
|
||||
(rmn,splt) = splitAt (length line - 1 - p) line
|
||||
new = field !! b ++ (f splt)
|
||||
(fieldA,_) = splitAt a field
|
||||
(_,fieldB) = splitAt (a+1) field
|
||||
removedField = fieldA ++ [rmn] ++ fieldB
|
||||
(newFieldA,_) = splitAt b removedField
|
||||
(_,newFieldB) = splitAt (b+1) removedField
|
||||
move f [p, a, b] field = newFieldA ++ [new] ++ newFieldB
|
||||
where
|
||||
line = field !! a
|
||||
(rmn, splt) = splitAt (length line - 1 - p) line
|
||||
new = field !! b ++ f splt
|
||||
(fieldA, _) = splitAt a field
|
||||
(_, fieldB) = splitAt (a + 1) field
|
||||
removedField = fieldA ++ [rmn] ++ fieldB
|
||||
(newFieldA, _) = splitAt b removedField
|
||||
(_, newFieldB) = splitAt (b + 1) removedField
|
||||
|
||||
doMoves :: (String -> String) -> [[Int]] -> [String] -> [String]
|
||||
doMoves f [] field = field
|
||||
doMoves _ [] field = field
|
||||
doMoves f (m:ms) field = doMoves f ms (move f m field)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.List.Split
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
|
@ -8,4 +8,4 @@ main = do
|
|||
print $ day6 14 input
|
||||
|
||||
day6 :: Int -> String -> Int
|
||||
day6 n = (+n) . head . elemIndices n . map (length.nub.sort) . divvy n 1
|
||||
day6 n = (+ n) . head . elemIndices n . map (length . nub . sort) . divvy n 1
|
||||
|
|
|
@ -1,57 +1,68 @@
|
|||
import Data.List
|
||||
|
||||
type Name = String
|
||||
|
||||
type Size = Int
|
||||
|
||||
data Inode = File Name Size | Folder Name [Inode] deriving Show
|
||||
data ICrumb = ICrumb Name [Inode] deriving Show
|
||||
data Inode
|
||||
= File Name Size
|
||||
| Folder Name [Inode]
|
||||
deriving (Show)
|
||||
|
||||
data ICrumb =
|
||||
ICrumb Name [Inode]
|
||||
deriving (Show)
|
||||
|
||||
type IZipper = (Inode, [ICrumb])
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map words <$> lines <$> readFile "input"
|
||||
let fullFS = walk (tail input) ((Folder "/" []), [])
|
||||
input <- map words . lines <$> readFile "input"
|
||||
let fullFS = walk (tail input) (Folder "/" [], [])
|
||||
let sizeList = getDirSizeList $ upTop fullFS
|
||||
print $ sum $ filter (<100000) sizeList
|
||||
let toDelete = (subtract 40000000) $ getDirSize $ upTop fullFS
|
||||
print $ sum $ filter (< 100000) sizeList
|
||||
let toDelete = subtract 40000000 $ getDirSize $ upTop fullFS
|
||||
print $ head $ dropWhile (< toDelete) $ sort sizeList
|
||||
|
||||
|
||||
up :: IZipper -> IZipper
|
||||
up (node, ICrumb name nodes:crumbs) = (Folder name (node:nodes), crumbs)
|
||||
up (node, ICrumb name nodes:crumbs) = (Folder name (node : nodes), crumbs)
|
||||
|
||||
upTop :: IZipper -> IZipper
|
||||
upTop (inode, []) = (inode, [])
|
||||
upTop z = upTop (up z)
|
||||
|
||||
newFile :: Inode -> IZipper -> IZipper
|
||||
newFile node (Folder name nodes, crumbs) = (Folder name (node:nodes), crumbs)
|
||||
newFile node (Folder name nodes, crumbs) = (Folder name (node : nodes), crumbs)
|
||||
|
||||
goTo :: Name -> IZipper -> IZipper
|
||||
goTo name (Folder fname nodes, crumbs) =
|
||||
let (ls, node:rs) = break (nameIs name) nodes
|
||||
in (node, ICrumb fname (ls++rs):crumbs)
|
||||
|
||||
let (ls, node:rs) = break (nameIs name) nodes
|
||||
in (node, ICrumb fname (ls ++ rs) : crumbs)
|
||||
|
||||
nameIs :: Name -> Inode -> Bool
|
||||
nameIs name (Folder folderName _) = name == folderName
|
||||
nameIs name (File fileName _) = name == fileName
|
||||
|
||||
walk :: [[String]] -> IZipper -> IZipper
|
||||
walk (("$":"cd":name:[]):xs) z = walk xs $ if name==".." then up z else goTo name z
|
||||
walk (("$":"ls":[]):xs) z = walk xs z
|
||||
walk (("dir":name:[]):xs) z = walk xs $ newFile (Folder name []) z
|
||||
walk ((size:name:[]):xs) z = walk xs $ newFile (File name (read size)) z
|
||||
walk (["$", "cd", name]:xs) z =
|
||||
walk xs $
|
||||
if name == ".."
|
||||
then up z
|
||||
else goTo name z
|
||||
walk (["$", "ls"]:xs) z = walk xs z
|
||||
walk (["dir", name]:xs) z = walk xs $ newFile (Folder name []) z
|
||||
walk ([size, name]:xs) z = walk xs $ newFile (File name (read size)) z
|
||||
walk _ z = z
|
||||
|
||||
|
||||
getDirSize :: IZipper -> Int
|
||||
getDirSize ((Folder _ nodes),_) = sum $ map getSize nodes
|
||||
where getSize (File _ size) = size
|
||||
getSize folder = getDirSize (folder,[])
|
||||
getDirSize (Folder _ nodes, _) = sum $ map getSize nodes
|
||||
where
|
||||
getSize (File _ size) = size
|
||||
getSize folder = getDirSize (folder, [])
|
||||
getDirSize _ = 0
|
||||
|
||||
getDirSizeList :: IZipper -> [Int]
|
||||
getDirSizeList ((Folder _ nodes),_) = getDirSize ((Folder "" nodes),[]) : concat [getDirSizeList (node,[]) | node<-nodes]
|
||||
getDirSizeList (Folder _ nodes, _) =
|
||||
getDirSize (Folder "" nodes, []) :
|
||||
concat [getDirSizeList (node, []) | node <- nodes]
|
||||
getDirSizeList _ = []
|
||||
|
|
|
@ -2,25 +2,38 @@ import Data.List
|
|||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map (map (subtract 48.fromEnum)) <$> lines <$> readFile "input"
|
||||
print $ sum $ day8 (fromEnum . any (/=0)) walkLine1 input
|
||||
input <- map (map (subtract 48 . fromEnum)) . lines <$> readFile "input"
|
||||
print $ sum $ day8 (fromEnum . any (/= 0)) walkLine1 input
|
||||
print $ maximum $ day8 product walkLine2 input
|
||||
|
||||
walkLine2 :: Int -> [Int] -> Int
|
||||
walkLine2 x (y:ys) = 1 + if y < x then walkLine2 x ys else 0
|
||||
walkLine2 x (y:ys) =
|
||||
1 +
|
||||
if y < x
|
||||
then walkLine2 x ys
|
||||
else 0
|
||||
walkLine2 _ [] = 0
|
||||
|
||||
walkLine1 :: Int -> [Int] -> Int
|
||||
walkLine1 x (y:ys) = if y < x then walkLine1 x ys else 0
|
||||
walkLine1 x (y:ys) =
|
||||
if y < x
|
||||
then walkLine1 x ys
|
||||
else 0
|
||||
walkLine1 _ [] = 1
|
||||
|
||||
checkPoint :: (Int -> [Int] -> Int) -> (Int,Int) -> [[Int]] -> [Int]
|
||||
checkPoint f (x,y) field = [f num line | line <- [lineX1,lineX2,lineY1,lineY2]]
|
||||
where num = field !! y !! x
|
||||
lineX1 = drop (x+1) $ field !! y
|
||||
lineX2 = reverse $ take x $ field !! y
|
||||
lineY1 = drop (y+1) $ (transpose field) !! x
|
||||
lineY2 = reverse $ take y $ (transpose field) !! x
|
||||
checkPoint :: (Int -> [Int] -> Int) -> (Int, Int) -> [[Int]] -> [Int]
|
||||
checkPoint f (x, y) field =
|
||||
[f num line | line <- [lineX1, lineX2, lineY1, lineY2]]
|
||||
where
|
||||
num = field !! y !! x
|
||||
lineX1 = drop (x + 1) $ field !! y
|
||||
lineX2 = reverse $ take x $ field !! y
|
||||
lineY1 = drop (y + 1) $ transpose field !! x
|
||||
lineY2 = reverse $ take y $ transpose field !! x
|
||||
|
||||
day8 :: ([Int] -> Int) -> (Int -> [Int] -> Int) -> [[Int]] -> [Int]
|
||||
day8 f1 f2 field = [f1 $ checkPoint f2 (x,y) field | x<-[0..(subtract 1 $ length $ head field)], y<-[0..(subtract 1 $ length field)]]
|
||||
day8 f1 f2 field =
|
||||
[ f1 $ checkPoint f2 (x, y) field
|
||||
| x <- [0 .. (subtract 1 $ length $ head field)]
|
||||
, y <- [0 .. (subtract 1 $ length field)]
|
||||
]
|
||||
|
|
|
@ -1,39 +1,45 @@
|
|||
import Linear.V2
|
||||
import Data.List
|
||||
import Linear.V2
|
||||
|
||||
type Rope = (V2 Int,V2 Int)
|
||||
type Rope = (V2 Int, V2 Int)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- concat <$> map (parse . words) <$> lines <$> readFile "input" --should be 4, is 1
|
||||
input <- concatMap (parse . words) . lines <$> readFile "input"
|
||||
print $ length $ nub $ day9 1 input
|
||||
print $ length $ nub $ day9 9 input
|
||||
-- putStrLn $ intercalate "\n" $ print2D 10 $ sort $ nub $ day9 3 input
|
||||
|
||||
day9 :: Int -> [V2 Int] -> [V2 Int]
|
||||
day9 n input
|
||||
| n > 1 = day9 (pred n) (zipWith (-) (tail field) field)
|
||||
| otherwise = field
|
||||
where field = concat $ map nub $ group $ reverse $ snd $ moveAll ((V2 0 0,V2 0 0),[]) input
|
||||
where
|
||||
field =
|
||||
concatMap nub $
|
||||
group $ reverse $ snd $ moveAll ((V2 0 0, V2 0 0), []) input
|
||||
|
||||
parse :: [String] -> [V2 Int]
|
||||
parse (dir:len:[])
|
||||
parse [dir, len]
|
||||
| dir == "R" = f $ V2 1 0
|
||||
| dir == "L" = f $ V2 (-1) 0
|
||||
| dir == "U" = f $ V2 0 1
|
||||
| dir == "D" = f $ V2 0 (-1)
|
||||
where f = take (read len) . repeat
|
||||
where
|
||||
f = replicate (read len)
|
||||
|
||||
moveAll :: (Rope,[V2 Int]) -> [V2 Int] -> (Rope,[V2 Int])
|
||||
moveAll :: (Rope, [V2 Int]) -> [V2 Int] -> (Rope, [V2 Int])
|
||||
moveAll r (x:xs) = moveAll (move r x) xs
|
||||
moveAll ((head,tail),xs) _ = ((head,tail),tail:xs)
|
||||
moveAll ((head, tail), xs) _ = ((head, tail), tail : xs)
|
||||
|
||||
move :: (Rope,[V2 Int]) -> V2 Int -> (Rope,[V2 Int])
|
||||
move ((head,tail),ps) m = ((head+m,newTail),tail:ps)
|
||||
where newTail = tailMove (head + m - tail) + tail
|
||||
move :: (Rope, [V2 Int]) -> V2 Int -> (Rope, [V2 Int])
|
||||
move ((head, tail), ps) m = ((head + m, newTail), tail : ps)
|
||||
where
|
||||
newTail = tailMove (head + m - tail) + tail
|
||||
|
||||
tailMove :: V2 Int -> V2 Int
|
||||
tailMove (V2 a b) = V2 (f a) (f b)
|
||||
where f x
|
||||
| abs a == abs b = if abs a == 2 then x `div` 2 else 0
|
||||
| abs a + abs b == 1 = 0
|
||||
| otherwise = if abs x == 2 then x `div` 2 else x
|
||||
where
|
||||
f x
|
||||
| abs x == 2 = signum x
|
||||
| abs a + abs b <= 2 = 0
|
||||
| otherwise = x
|
||||
|
|
|
@ -1,28 +1,36 @@
|
|||
import Data.List.Split
|
||||
|
||||
data OpCode = Noop | AddX Int deriving (Show,Eq)
|
||||
data OpCode
|
||||
= Noop
|
||||
| AddX Int
|
||||
deriving (Show, Eq)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- parse <$> lines <$> readFile "input"
|
||||
let c = 1:1:1:cycles 1 input
|
||||
let vals = [20,60,100,140,180,220]
|
||||
print $ sum [i * c !! i | i<-vals]
|
||||
input <- parse . lines <$> readFile "input"
|
||||
let c = 1 : 1 : 1 : cycles 1 input
|
||||
let vals = [20, 60, 100, 140, 180, 220]
|
||||
print $ sum [i * c !! i | i <- vals]
|
||||
putStrLn $ printscr (tail c)
|
||||
|
||||
parse :: [String] -> [OpCode]
|
||||
parse (l:ls)
|
||||
| l == "noop" = Noop : parse ls
|
||||
| head len2 == "addx" = AddX (read $ len2 !! 1) : parse ls
|
||||
where len2 = words l
|
||||
where
|
||||
len2 = words l
|
||||
parse _ = []
|
||||
|
||||
cycles :: Int -> [OpCode] -> [Int]
|
||||
cycles x (Noop:cs) = x : cycles x cs
|
||||
cycles x (AddX y:cs) = (x+y) : (x+y) : cycles (x+y) cs
|
||||
cycles x (AddX y:cs) = (x + y) : (x + y) : cycles (x + y) cs
|
||||
cycles _ _ = []
|
||||
|
||||
printscr :: [Int] -> String
|
||||
printscr pxs = concat [zipWith f (ls !! x) [0..] ++ "\n" | x<-[0..5]]
|
||||
where f x y = if abs (x - y) <= 1 then '#' else '.'
|
||||
ls = chunksOf 40 pxs
|
||||
printscr pxs = concat [zipWith f (ls !! x) [0 ..] ++ "\n" | x <- [0 .. 5]]
|
||||
where
|
||||
f x y =
|
||||
if abs (x - y) <= 1
|
||||
then '#'
|
||||
else '.'
|
||||
ls = chunksOf 40 pxs
|
||||
|
|
Loading…
Reference in New Issue
Block a user