day9 part1

This commit is contained in:
Gattix 2022-12-09 08:02:19 +01:00
parent b29e31d614
commit 08b17b1935
3 changed files with 2040 additions and 0 deletions

32
2022/day09/day09.hs Normal file
View File

@ -0,0 +1,32 @@
import Linear.V2
import Data.List
import Control.Lens
type Rope = (V2 Int,V2 Int)
main :: IO ()
main = do
input <- concat <$> map (parse . words) <$> lines <$> readFile "input"
print $ length $ nub $ sort $ snd $ moveAll ((V2 0 0,V2 0 0),[]) input
parse :: [String] -> [V2 Int]
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
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)
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 a b) = V2 (f a) (f b)
where f x
| abs a == abs b = 0
| abs a + abs b == 1 = 0
| otherwise = if abs x >= 2 then x `div` 2 else x

2000
2022/day09/input Normal file

File diff suppressed because it is too large Load Diff

8
2022/day09/testinput Normal file
View File

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2