Compare commits
32 Commits
day7backup
...
master
Author | SHA1 | Date | |
---|---|---|---|
aec425b1e0 | |||
9e3aeb2d92 | |||
98034f4f76 | |||
a5cda9b104 | |||
4c55fd04e8 | |||
b49722bb69 | |||
b0f6505112 | |||
196dbbff3e | |||
417c4ede04 | |||
46b9098db9 | |||
17ef30b293 | |||
5a307bfa6e | |||
ea5b4b48de | |||
6e36014987 | |||
f2f0df9142 | |||
9da7f98590 | |||
0e4bed2b26 | |||
e5adad16ed | |||
8df4b4c9c0 | |||
3fe5944338 | |||
99b6f0085b | |||
a42348fedb | |||
aae7079140 | |||
63d41f5084 | |||
c4fce9b29e | |||
7b28a5a035 | |||
4af44ebd0b | |||
4f73ff3c56 | |||
f89d165c94 | |||
ce7044ecf1 | |||
57127b88dd | |||
bfa4f0a305 |
66
day10.hs
Normal file
66
day10.hs
Normal file
@ -0,0 +1,66 @@
|
||||
import Data.List as List
|
||||
import Debug.Trace as Trace
|
||||
|
||||
main = do
|
||||
content <- getContents
|
||||
let layers = lines content
|
||||
let indexlayers = map(\x-> mapInd(\x y -> (y,x)) x ) layers
|
||||
let points = concat (mapInd(\x y->map(\x->((fst x,y),snd x))x) $ indexlayers)
|
||||
let asteroids = (map fst (filter(\(x,y) -> y == '#' ) points))
|
||||
let maximum = List.maximum(map(\x-> length(getViews (changeCoordinate (asteroids) x))) (asteroids))
|
||||
let station = head $ map(\x-> fst x) $ filter(\x-> snd x == maximum )(map(\x-> (x,( length(getViews (changeCoordinate asteroids x))))) (asteroids))
|
||||
let changedAsteroids = changeCoordinate asteroids station
|
||||
let sortedAsteroids = sortBy sortDistance changedAsteroids
|
||||
let views = getViews sortedAsteroids
|
||||
let sortedViews = sortBy sortDegree views
|
||||
let destroyed = getDestroyOrder sortedAsteroids []
|
||||
let destroyNormal = map (\(a,b)-> (((fst station) + a),((snd station) + b))) destroyed
|
||||
putStrLn(show maximum)
|
||||
putStrLn(show station)
|
||||
putStrLn(show views)
|
||||
putStrLn(show sortedViews)
|
||||
putStrLn(show $ map degree destroyed)
|
||||
putStrLn(show $ destroyNormal)
|
||||
putStrLn(show $ destroyNormal!!199)
|
||||
mapInd :: (a -> Int -> b) -> [a] -> [b]
|
||||
mapInd f l = zipWith f l [0..]
|
||||
|
||||
getDestroyOrder :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)]
|
||||
getDestroyOrder ast out
|
||||
| length ast > 0 = do
|
||||
let views = getViews ast
|
||||
let sortedViews = sortBy sortDegree views
|
||||
let newout = out ++ sortedViews
|
||||
let newast = ast \\ sortedViews
|
||||
getDestroyOrder newast newout
|
||||
| otherwise = out
|
||||
|
||||
sortDistance ((a,b)) ((a2,b2))
|
||||
| abs(a) + abs(b) > abs(a2) + abs(b2) = GT
|
||||
| abs(a) + abs(b) < abs(a2) + abs(b2) = LT
|
||||
| abs(a) + abs(b) == abs(a2) + abs(b2) = EQ
|
||||
|
||||
sortDegree a b
|
||||
| degree a < degree b = GT
|
||||
| degree a > degree b = LT
|
||||
| degree a == degree b = EQ
|
||||
|
||||
getViews :: [(Int,Int)] -> [(Int,Int)]
|
||||
getViews xs = foldl getView [] xs
|
||||
|
||||
changeCoordinate :: [(Int, Int)] -> (Int,Int) -> [(Int,Int)]
|
||||
changeCoordinate xs (a,b) = (delete (0,0)( (map(\(x,y) -> ((x-a),(y-b)))xs)))
|
||||
|
||||
getView :: [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
|
||||
getView xs y
|
||||
|notElem (reduce y) (map reduce xs) = xs ++ [y]
|
||||
|otherwise = xs
|
||||
|
||||
reduce :: (Int,Int) -> (Int,Int)
|
||||
reduce (a,b) = ((div a (gcd a b)),(div b (gcd a b)))
|
||||
|
||||
degree :: (Int,Int) -> Double
|
||||
degree (a,b) = do
|
||||
let x = fromIntegral a
|
||||
let y = fromIntegral b
|
||||
atan2 x (y)
|
228
day11.hs
Normal file
228
day11.hs
Normal file
@ -0,0 +1,228 @@
|
||||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
let brain = Amplifier software 0 0 [] [0]
|
||||
let robot = Robot brain [((0,0),1)] (0,0) 0
|
||||
let result = runRobot robot
|
||||
let endPoints = points result
|
||||
let pointsNoColor = map(\(x,y) -> x) endPoints
|
||||
let unique = nub pointsNoColor
|
||||
let mapPoint = createMap endPoints (reverse [-7..1]) []
|
||||
mapM putStrLn( map show mapPoint)
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,base :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int]
|
||||
} deriving Show
|
||||
|
||||
data Robot = Robot{ brain:: Amplifier
|
||||
,points:: [((Int,Int),Int)]
|
||||
,position:: (Int,Int)
|
||||
,direction:: Int
|
||||
} deriving Show
|
||||
|
||||
getBrain :: Robot -> Amplifier
|
||||
getBrain (Robot brain points poisition direction) = brain
|
||||
|
||||
createMap ::[((Int,Int),Int)]-> [Int] -> [[Int]] -> [[Int]]
|
||||
createMap points (y:ys) output
|
||||
|length ys > 0 = do
|
||||
let fPoints = filter(\((a,b),c) -> b ==y ) points
|
||||
let row = foldl createRow [] fPoints
|
||||
let newoutput = output ++ [(Trace.traceShowId(row))]
|
||||
createMap points ys newoutput
|
||||
|otherwise = output
|
||||
|
||||
createRow :: [Int] -> ((Int,Int),Int) -> [Int]
|
||||
createRow row ((a,b),c) = Main.insert row c a
|
||||
|
||||
runRobot :: Robot -> Robot
|
||||
runRobot (Robot brain points position direction) = do
|
||||
let currentpoint = filter(\(p,c)-> p == position) $ points
|
||||
let input =( if length currentpoint == 0
|
||||
then 0
|
||||
else snd $ head currentpoint)
|
||||
let newBrain = (step brain [(input)])
|
||||
if (output newBrain) == []
|
||||
then Robot brain points position direction
|
||||
else do
|
||||
let outColor = (output (newBrain))!!0
|
||||
let outMove = (output newBrain)!!1
|
||||
let newPoints = ( (points \\ currentpoint) ++ [(position,outColor)])
|
||||
let newDirection = changeDirection direction outMove
|
||||
let newPos = move position newDirection
|
||||
runRobot (Robot newBrain newPoints newPos newDirection)
|
||||
|
||||
stepRobot :: Robot -> Robot
|
||||
stepRobot (Robot brain points position direction) = do
|
||||
let currentpoint = filter(\(p,c)-> p == position) $ points
|
||||
let input = if length currentpoint == 0
|
||||
then 0
|
||||
else snd $ head currentpoint
|
||||
let newBrain = step brain [input]
|
||||
let outColor = (output newBrain)!!0
|
||||
let outMove = (output newBrain)!!1
|
||||
let newPoints = (points) ++ [(position,outColor)]
|
||||
let newDirection = changeDirection direction outMove
|
||||
let newPos = move position newDirection
|
||||
Robot newBrain newPoints newPos newDirection
|
||||
|
||||
move :: (Int,Int) -> Int -> (Int,Int)
|
||||
move (x,y) direction
|
||||
| direction == 0 = (x,y+1)
|
||||
| direction == 1 = (x+1,y)
|
||||
| direction == 2 = (x,y-1)
|
||||
| direction == 3 = (x-1,y)
|
||||
|
||||
changeDirection :: Int -> Int -> Int
|
||||
changeDirection direction input
|
||||
| input == 0 = changeDirection' direction (-1)
|
||||
| input == 1 = changeDirection' direction 1
|
||||
|
||||
changeDirection' :: Int -> Int -> Int
|
||||
changeDirection' direction change
|
||||
| direction + change < 0 = 3
|
||||
| otherwise = mod (direction + change) 4
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
||||
link :: Amplifier -> Amplifier -> Amplifier
|
||||
link left calc
|
||||
| null (output left) = Amplifier (state calc) (-1) (base calc) (input calc) (output calc)
|
||||
| index left == -1 = Amplifier (state calc) (-1) (base calc) (input calc) (output calc)
|
||||
| otherwise = step calc ([last $ output left])
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) (base amp) input []
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i base input output =
|
||||
Amplifier state i base input []
|
||||
operation (op:xs) state i base input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i base input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) base state
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex newstate) (newstate) newindex base newinput output
|
||||
| last (digits op) == 4 = do
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) base state
|
||||
let newinput = drop 1 input
|
||||
operation ((drop newindex state)) (state) newindex base input (newoutput)
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 9 ) = do
|
||||
let newindex = i + 2
|
||||
let fullop = (fillup (revertdigs op) 3)
|
||||
let newbase = base + (getValue (fullop!!2) (xs!!0) base state)
|
||||
(operation ((drop newindex state)) (state) newindex newbase input output)
|
||||
|
||||
add :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
add (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) + (getValue m2 p2 base state)
|
||||
|
||||
mult :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
mult (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) * (getValue m2 p2 base state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input base state =
|
||||
Main.insert state input (getIndex m1 p1 base)
|
||||
|
||||
|
||||
out :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
out (op1:op2:m1:_) output p1 base state =
|
||||
output ++ [(getValue m1 p1 base state)]
|
||||
|
||||
jumpif :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpif (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) /= 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpifnot (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) == 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
lessthan :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state) < (getValue m2 p2 base state) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
equal :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
equal (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state ) == (getValue m2 p2 base state ) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
|
||||
insert :: [Int] -> Int -> Int -> [Int]
|
||||
insert xs value index
|
||||
| index < length xs = do
|
||||
let split = splitAt index xs
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
| otherwise = do
|
||||
let longState = xs ++ (replicate (index - length xs) 0)
|
||||
let split = splitAt index longState
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
|
||||
read :: [Int] -> Int -> Int
|
||||
read xs index
|
||||
| index < length xs = xs!!index
|
||||
| otherwise = 0
|
||||
|
||||
digits :: Int -> [Int]
|
||||
digits = map Char.digitToInt . show
|
||||
|
||||
revertdigs :: Int -> [Int]
|
||||
revertdigs 0 = []
|
||||
revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
|
||||
|
||||
fillup :: [Int] -> Int -> [Int]
|
||||
fillup array x = array ++ (replicate (x - (length array)) 0)
|
||||
|
||||
getValue :: Int -> Int -> Int -> [Int] -> Int
|
||||
getValue 0 p base array = Main.read array p
|
||||
getValue 1 p base array = p
|
||||
getValue 2 p base array = Main.read array (base + p)
|
||||
|
||||
getIndex :: Int -> Int -> Int -> Int
|
||||
getIndex m p base
|
||||
| m == 0 = p
|
||||
| m == 2 = p + base
|
89
day12.hs
Normal file
89
day12.hs
Normal file
@ -0,0 +1,89 @@
|
||||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.Char as Char
|
||||
import Linear.V3
|
||||
|
||||
main = do
|
||||
moons <- map createMoon <$> map getList <$> map cleanString <$> lines <$> getContents
|
||||
--let newMoons = calculateMoons moons 1000
|
||||
--let ernergy = sum $ map getErnergy newMoons
|
||||
let xAxis = getXAxis moons
|
||||
let loopx = findLoop xAxis xAxis 0
|
||||
let yAxis = getYAxis moons
|
||||
let loopy = findLoop yAxis yAxis 0
|
||||
let zAxis = getZAxis moons
|
||||
let loopz = findLoop zAxis zAxis 0
|
||||
let result = foldl1 lcm [loopx, loopy, loopz]
|
||||
--mapM putStrLn ( map show newMoons )
|
||||
--putStrLn ( show ernergy)
|
||||
putStrLn(show result)
|
||||
|
||||
data Moon = Moon{ position :: V3 Int
|
||||
,velocity :: V3 Int
|
||||
} deriving Show
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map read . splitOn ", "
|
||||
|
||||
cleanString :: String -> String
|
||||
cleanString xs = xs \\ "<>xyz==="
|
||||
|
||||
createMoon :: [Int] -> Moon
|
||||
createMoon [x,y,z] = Moon (V3 x y z) (V3 0 0 0)
|
||||
|
||||
stepGravity :: [Moon] -> [Moon]
|
||||
stepGravity xs = map (\(p,v) -> Moon p v) $ zip (newPos) newVel
|
||||
where newVel = zipWith (+) (getVel xs) (gravity $ getPos xs)
|
||||
newPos = zipWith (+) (getPos xs) (newVel)
|
||||
|
||||
gravity :: (Num a, Eq a)=> [a] -> [a]
|
||||
gravity xs = [sum [signum $ x-y | x<-xs, y/=x] | y<-xs]
|
||||
|
||||
calculateMoons :: [Moon] -> Int -> [Moon]
|
||||
calculateMoons xs 0 = xs
|
||||
calculateMoons xs n = calculateMoons (stepGravity xs) (n-1)
|
||||
|
||||
findLoop :: ([Int],[Int]) -> ([Int],[Int]) -> Int -> Int
|
||||
findLoop pos start c
|
||||
| pos /= start || c == 0 = findLoop (stepAxis pos) start (c+1)
|
||||
| pos == start = c
|
||||
|
||||
stepAxis :: ([Int],[Int]) -> ([Int],[Int])
|
||||
stepAxis (pos,vel) = (newPos, newVel)
|
||||
where newVel = zipWith (+) (vel) (gravity pos)
|
||||
newPos = zipWith (+) (pos) (newVel)
|
||||
|
||||
getPos :: [Moon] -> [V3 Int]
|
||||
getPos xs = map(\x -> position x) xs
|
||||
|
||||
getVel :: [Moon] -> [V3 Int]
|
||||
getVel xs = map(\x -> velocity x) xs
|
||||
|
||||
getErnergy :: Moon -> Int
|
||||
getErnergy (Moon pos vel) = (sum $ abs pos) * (sum $ abs vel)
|
||||
|
||||
getXAxis :: [Moon] -> ([Int],[Int])
|
||||
getXAxis xs = (pos,vel)
|
||||
where pos = map(\x -> getX $ position x) xs
|
||||
vel = map(\x -> getX $ velocity x) xs
|
||||
|
||||
getZAxis :: [Moon] -> ([Int],[Int])
|
||||
getZAxis xs = (pos,vel)
|
||||
where pos = map(\x -> getZ $ position x) xs
|
||||
vel = map(\x -> getZ $ velocity x) xs
|
||||
|
||||
getYAxis :: [Moon] -> ([Int],[Int])
|
||||
getYAxis xs = (pos,vel)
|
||||
where pos = map(\x -> getY $ position x) xs
|
||||
vel = map(\x -> getY $ velocity x) xs
|
||||
|
||||
getX :: V3 Int -> Int
|
||||
getX (V3 x y z) = x
|
||||
|
||||
getY :: V3 Int -> Int
|
||||
getY (V3 x y z) = y
|
||||
|
||||
getZ :: V3 Int -> Int
|
||||
getZ (V3 x y z) = z
|
||||
|
||||
|
192
day13.hs
Normal file
192
day13.hs
Normal file
@ -0,0 +1,192 @@
|
||||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import qualified Data.Map.Strict as M
|
||||
import Linear.V2
|
||||
import Control.Monad
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
let arcade = Amplifier software 0 0 [] []
|
||||
let arcadeStep1 = step arcade []
|
||||
let step1Result = parseOutput M.empty (output arcadeStep1)
|
||||
let blocks =length $ M.filter(==2) step1Result
|
||||
let result = runGame arcade M.empty
|
||||
--let gameMap = createMap result [0..23] []
|
||||
let score = M.filterWithKey(\(V2 a b) _ -> a == -1 ) result
|
||||
--mapM putStrLn( map (map getSymbol) gameMap)
|
||||
--putStrLn(show score)
|
||||
putStrLn(show result)
|
||||
putStrLn "Finished"
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,base :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int]
|
||||
} deriving Show
|
||||
|
||||
runGame :: Amplifier -> M.Map (V2 Int) Int -> M.Map (V2 Int) Int
|
||||
runGame arcade gameM= do
|
||||
let newArcade = step arcade [0]
|
||||
let tiles = ((parseOutput M.empty (output newArcade)))
|
||||
let newGameM = M.union tiles gameM
|
||||
let blocks = length $ M.filter(== 2) newGameM
|
||||
if blocks == 0
|
||||
then tiles
|
||||
else runGame newArcade newGameM
|
||||
|
||||
parseOutput :: M.Map (V2 Int) Int -> [Int] -> M.Map (V2 Int) Int
|
||||
parseOutput tiles (x:y:c:xs)
|
||||
| length xs == 0 = M.insert (V2 x y) c tiles
|
||||
| length xs > 0 = parseOutput newtiles xs
|
||||
where newtiles = M.insert (V2 x y) c tiles
|
||||
|
||||
createMap :: M.Map (V2 Int) Int -> [Int] -> [[Int]] -> [[Int]]
|
||||
createMap points (y:ys) output
|
||||
|length ys > 0 = do
|
||||
let fPoints = M.filterWithKey(\(V2 a b) _ -> b ==y ) points
|
||||
let row = M.foldlWithKey createRow [] fPoints
|
||||
let newoutput = output ++ [(row)]
|
||||
createMap points ys newoutput
|
||||
|otherwise = output
|
||||
|
||||
createRow :: [Int] -> V2 Int -> Int -> [Int]
|
||||
createRow row (V2 a b) c = Main.insert row c a
|
||||
|
||||
getSymbol :: Int -> Char
|
||||
getSymbol 0 = ' '
|
||||
getSymbol 1 = '|'
|
||||
getSymbol 2 = '#'
|
||||
getSymbol 3 = '='
|
||||
getSymbol 4 = '*'
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) (base amp) input []
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i base input output =
|
||||
Amplifier state i base input output
|
||||
operation (op:xs) state i base input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i base input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) base state
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex newstate) (newstate) newindex base newinput output
|
||||
| last (digits op) == 4 = do
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) base state
|
||||
let newinput = drop 1 input
|
||||
operation ((drop newindex state)) (state) newindex base input (newoutput)
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 9 ) = do
|
||||
let newindex = i + 2
|
||||
let fullop = (fillup (revertdigs op) 3)
|
||||
let newbase = base + (getValue (fullop!!2) (xs!!0) base state)
|
||||
(operation ((drop newindex state)) (state) newindex newbase input output)
|
||||
|
||||
add :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
add (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) + (getValue m2 p2 base state)
|
||||
|
||||
mult :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
mult (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) * (getValue m2 p2 base state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input base state =
|
||||
Main.insert state input (getIndex m1 p1 base)
|
||||
|
||||
|
||||
out :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
out (op1:op2:m1:_) output p1 base state =
|
||||
output ++ [(getValue m1 p1 base state)]
|
||||
|
||||
jumpif :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpif (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) /= 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpifnot (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) == 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
lessthan :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state) < (getValue m2 p2 base state) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
equal :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
equal (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state ) == (getValue m2 p2 base state ) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
|
||||
insert :: [Int] -> Int -> Int -> [Int]
|
||||
insert xs value index
|
||||
| index < length xs = do
|
||||
let split = splitAt index xs
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
| otherwise = do
|
||||
let longState = xs ++ (replicate (index - length xs) 0)
|
||||
let split = splitAt index longState
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
|
||||
read :: [Int] -> Int -> Int
|
||||
read xs index
|
||||
| index < length xs = xs!!index
|
||||
| otherwise = 0
|
||||
|
||||
digits :: Int -> [Int]
|
||||
digits = map Char.digitToInt . show
|
||||
|
||||
revertdigs :: Int -> [Int]
|
||||
revertdigs 0 = []
|
||||
revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
|
||||
|
||||
fillup :: [Int] -> Int -> [Int]
|
||||
fillup array x = array ++ (replicate (x - (length array)) 0)
|
||||
|
||||
getValue :: Int -> Int -> Int -> [Int] -> Int
|
||||
getValue 0 p base array = Main.read array p
|
||||
getValue 1 p base array = p
|
||||
getValue 2 p base array = Main.read array (base + p)
|
||||
|
||||
getIndex :: Int -> Int -> Int -> Int
|
||||
getIndex m p base
|
||||
| m == 0 = p
|
||||
| m == 2 = p + base
|
81
day14.hs
Normal file
81
day14.hs
Normal file
@ -0,0 +1,81 @@
|
||||
import Data.List.Split
|
||||
import Data.List
|
||||
import Data.Char as Char
|
||||
import Linear.V3
|
||||
import Debug.Trace as T
|
||||
|
||||
main = do
|
||||
reactions <- map getReaction <$> lines <$> getContents
|
||||
let amout = getConstruction reactions (1,"A")
|
||||
let test = part1 [(1,"FUEL")] [] reactions
|
||||
putStrLn(show $ head reactions)
|
||||
--putStrLn(show ore)
|
||||
putStrLn(show test)
|
||||
--putStrLn(show sumResult1)
|
||||
data Reaction = Reaction { input :: [(Int, String)],
|
||||
output :: (Int,String)
|
||||
} deriving Show
|
||||
|
||||
getReaction :: String -> Reaction
|
||||
getReaction input = Reaction left right
|
||||
where split = splitOn " => " input
|
||||
left = map getElements (splitOn ", " (split!!0))
|
||||
right = getElements $ split!!1
|
||||
|
||||
getElements :: String -> (Int,String)
|
||||
getElements input = (amount,element)
|
||||
where split = splitOn " " input
|
||||
amount = read $ (split!!0)
|
||||
element = split!!1
|
||||
|
||||
getNextStep :: [Reaction] -> Reaction -> [Reaction]
|
||||
getNextStep xs (Reaction left right) = filter(\(Reaction i o) -> elem (snd o) reactElem ) xs
|
||||
where reactElem = map(\(a,b) -> b) left
|
||||
|
||||
getConstruction :: [Reaction] -> (Int,String) -> (Int,[(Int, String)])
|
||||
getConstruction reactions (amount,elem)
|
||||
| elem == "ORE" = (1,[(amount,elem)])
|
||||
| length reaction > 0 = (div amount ( fst $ output $ head $ reaction),(input $ head $ reaction))
|
||||
| otherwise = (1,[(amount,elem)])
|
||||
where reaction = filter(\(Reaction i o) -> ((snd o) == elem) && (mod amount (fst o) == 0)) reactions
|
||||
|
||||
|
||||
getConstructionRest :: [Reaction] -> (Int,String) -> (Int,[(Int, String)])
|
||||
getConstructionRest reactions (amount, elem)
|
||||
| elem == "ORE" = (1,[(amount,elem)])
|
||||
| otherwise = ((div amount ( fst $ output $ head $ reaction)) + 1,(input $ head $ reaction))
|
||||
where reaction = filter(\(Reaction i o) -> ((snd o) == elem) ) reactions
|
||||
|
||||
part1 :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
|
||||
part1 needs oldNeeds reactions
|
||||
| needs == oldNeeds = needs
|
||||
| otherwise = part1 newNeeds needs reactions
|
||||
where newNeeds = getRestOre (T.traceShowId(fullNeeds)) [] reactions
|
||||
fullNeeds = getOre (T.traceShowId(needs)) [] reactions
|
||||
|
||||
getOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
|
||||
getOre needs oldNeeds reactions
|
||||
| needs == oldNeeds = newNeeds
|
||||
| otherwise = getOre (newNeeds) needs reactions
|
||||
where newNeeds =foldl combineNeeds [] ( foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) construction))
|
||||
construction = map (getConstruction reactions) needs
|
||||
|
||||
|
||||
getRestOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
|
||||
getRestOre needs oldNeeds reactions = sumNeeds
|
||||
where pureNeeds = (T.traceShowId(head ( getPureNeeds needs reactions)))
|
||||
pureRest = foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) (map (getConstructionRest reactions) [pureNeeds]))
|
||||
sumNeeds = foldl combineNeeds [] ((needs \\ [pureNeeds]) ++ pureRest)
|
||||
|
||||
combineNeeds :: [(Int,String)] -> (Int,String) -> [(Int,String)]
|
||||
combineNeeds xs (amt, elem)
|
||||
| null oldVal = xs ++ [(amt,elem)]
|
||||
| otherwise = (xs \\ oldVal) ++ [((amt + (fst (head oldVal))),elem)]
|
||||
where oldVal = filter(\(a,e) -> e == elem) xs
|
||||
|
||||
getPureNeeds :: [(Int,String)] -> [Reaction] -> [(Int,String)]
|
||||
getPureNeeds needs reactions = filter(\(a,e) -> notElem e (impureElements) ) needs
|
||||
where impureElements = map(\(a,e) -> e) ( (foldl (++) [] ( map(\(Reaction i o) -> i)reactionList )))
|
||||
reactionList = (filter(\(Reaction i o) -> elem (snd o) elements) reactions)
|
||||
elements = (map(\(a,e) -> e) needs)
|
||||
|
224
day15.hs
Normal file
224
day15.hs
Normal file
@ -0,0 +1,224 @@
|
||||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
let brain = Amplifier software 0 0 [] [0]
|
||||
let robot = Robot brain [] (0,0) 1
|
||||
let resultRobots = (runRobot robot )
|
||||
let winRobots = filter(\(Robot br pts pos dir) -> elem 2 (map(\(p,c) -> c) pts))resultRobots
|
||||
let winRobotsLength = map(\(Robot br pts pos dir) -> length (filter(\(p,c) -> c == 1)pts))winRobots
|
||||
let winRobot = (map(\(Robot br pts pos dir) -> Robot br [] pos 1) winRobots) !! 0
|
||||
let part2Robots = (runRobot winRobot)
|
||||
let part2Length = map(\(Robot br pts pos dir) -> length (filter(\(p,c) -> c == 1)pts))part2Robots
|
||||
putStrLn(show winRobotsLength)
|
||||
putStrLn(show $ List.maximum( part2Length))
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,base :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int]
|
||||
} deriving Show
|
||||
|
||||
data Robot = Robot{ brain:: Amplifier
|
||||
,points:: [((Int,Int),Int)]
|
||||
,position:: (Int,Int)
|
||||
,direction :: Int
|
||||
} deriving Show
|
||||
|
||||
getBrain :: Robot -> Amplifier
|
||||
getBrain (Robot brain points poisition direction) = brain
|
||||
|
||||
createMap ::[((Int,Int),Int)]-> [Int] -> [[Int]] -> [[Int]]
|
||||
createMap points (y:ys) output
|
||||
|length ys > 0 = do
|
||||
let fPoints = filter(\((a,b),c) -> b ==y ) points
|
||||
let row = foldl createRow [] fPoints
|
||||
let newoutput = output ++ [(row)]
|
||||
createMap points ys newoutput
|
||||
|otherwise = output
|
||||
|
||||
createRow :: [Int] -> ((Int,Int),Int) -> [Int]
|
||||
createRow row ((a,b),c) = Main.insert row c a
|
||||
|
||||
runRobot :: Robot -> [Robot]
|
||||
runRobot robot
|
||||
| (length move) == 0 = [robot]
|
||||
| (length move) == 1 = do
|
||||
let newRobot = stepRobot robot $ move!!0
|
||||
runRobot newRobot
|
||||
| otherwise = do
|
||||
let newRobots = map(\mv -> stepRobot robot mv) move
|
||||
foldl (++) [] $ map(\robot -> runRobot robot) newRobots
|
||||
where move = getNextMove robot
|
||||
|
||||
stepRobot :: Robot -> Int -> Robot
|
||||
stepRobot (Robot brain points position direction) newDirection = do
|
||||
let newBrain = step brain [newDirection]
|
||||
let statusResponse = head(output newBrain)
|
||||
let newPos = move position newDirection
|
||||
let newPoints = (points) ++ [(newPos,statusResponse)]
|
||||
if statusResponse == 0 || statusResponse == 2
|
||||
then Robot newBrain newPoints position newDirection
|
||||
else Robot newBrain newPoints newPos newDirection
|
||||
|
||||
move :: (Int,Int) -> Int -> (Int,Int)
|
||||
move (x,y) direction
|
||||
| direction == 1 = (x,y+1)
|
||||
| direction == 4 = (x+1,y)
|
||||
| direction == 2 = (x,y-1)
|
||||
| direction == 3 = (x-1,y)
|
||||
|
||||
getNextMove :: Robot -> [Int]
|
||||
getNextMove (Robot brain points position direction)
|
||||
|length points > 0 && (snd $ last points) == 2 = []
|
||||
|otherwise = do
|
||||
filterMoves (Robot brain points position direction) [1,2,3,4]
|
||||
|
||||
filterMoves :: Robot -> [Int] -> [Int]
|
||||
filterMoves robot moves = filter(\x -> checkVisit robot x && checkWall robot x) moves
|
||||
|
||||
checkVisit :: Robot -> Int -> Bool
|
||||
checkVisit (Robot brain points position direction) mv = do
|
||||
let newPos = move position mv
|
||||
let visits = map(\(pos,c) -> pos) points
|
||||
notElem newPos visits
|
||||
|
||||
checkWall :: Robot -> Int -> Bool
|
||||
checkWall (Robot brain points position direction) mv = do
|
||||
let mvResult = head $ output (step brain [mv])
|
||||
mvResult /= 0
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) (base amp) input []
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i base input output =
|
||||
Amplifier state i base input []
|
||||
operation (op:xs) state i base input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i base input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) base state
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex newstate) (newstate) newindex base newinput output
|
||||
| last (digits op) == 4 = do
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) base state
|
||||
let newinput = drop 1 input
|
||||
operation ((drop newindex state)) (state) newindex base input (newoutput)
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 9 ) = do
|
||||
let newindex = i + 2
|
||||
let fullop = (fillup (revertdigs op) 3)
|
||||
let newbase = base + (getValue (fullop!!2) (xs!!0) base state)
|
||||
(operation ((drop newindex state)) (state) newindex newbase input output)
|
||||
|
||||
add :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
add (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) + (getValue m2 p2 base state)
|
||||
|
||||
mult :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
mult (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) * (getValue m2 p2 base state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input base state =
|
||||
Main.insert state input (getIndex m1 p1 base)
|
||||
|
||||
|
||||
out :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
out (op1:op2:m1:_) output p1 base state =
|
||||
output ++ [(getValue m1 p1 base state)]
|
||||
|
||||
jumpif :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpif (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) /= 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpifnot (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) == 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
lessthan :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state) < (getValue m2 p2 base state) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
equal :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
equal (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state ) == (getValue m2 p2 base state ) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
|
||||
insert :: [Int] -> Int -> Int -> [Int]
|
||||
insert xs value index
|
||||
| index < length xs = do
|
||||
let split = splitAt index xs
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
| otherwise = do
|
||||
let longState = xs ++ (replicate (index - length xs) 0)
|
||||
let split = splitAt index longState
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
|
||||
read :: [Int] -> Int -> Int
|
||||
read xs index
|
||||
| index < length xs = xs!!index
|
||||
| otherwise = 0
|
||||
|
||||
digits :: Int -> [Int]
|
||||
digits = map Char.digitToInt . show
|
||||
|
||||
revertdigs :: Int -> [Int]
|
||||
revertdigs 0 = []
|
||||
revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
|
||||
|
||||
fillup :: [Int] -> Int -> [Int]
|
||||
fillup array x = array ++ (replicate (x - (length array)) 0)
|
||||
|
||||
getValue :: Int -> Int -> Int -> [Int] -> Int
|
||||
getValue 0 p base array = Main.read array p
|
||||
getValue 1 p base array = p
|
||||
getValue 2 p base array = Main.read array (base + p)
|
||||
|
||||
getIndex :: Int -> Int -> Int -> Int
|
||||
getIndex m p base
|
||||
| m == 0 = p
|
||||
| m == 2 = p + base
|
39
day16.hs
Normal file
39
day16.hs
Normal file
@ -0,0 +1,39 @@
|
||||
import Data.Char
|
||||
|
||||
main = do
|
||||
cont <- getContents
|
||||
let content = drop 5977377 (concat (replicate 10000 (map digitToInt $ init cont) ))
|
||||
let test1 = doStepsP2 content 100
|
||||
putStrLn (show $ length content)
|
||||
--mapM putStrLn (map show patter)
|
||||
putStrLn (show $ take 8 test1)
|
||||
|
||||
getPatternForIndex :: [Int] -> Int -> Int -> [Int]
|
||||
getPatternForIndex patter length index = drop (1 + index) $ take (length +1) (cycle base)
|
||||
where base = concat $ map (replicate (index+1)) patter
|
||||
|
||||
getInputForIndex :: [Int] -> Int -> [Int]
|
||||
getInputForIndex xs index = drop index xs
|
||||
|
||||
get :: [Int] -> Int
|
||||
get xs = mod (abs $ (sum xs)) 10
|
||||
|
||||
step :: [Int] -> [[Int]] -> [Int]
|
||||
step xs patterns = map (\(a,b) -> get( zipWith (*) a b)) $ zip inputs patterns
|
||||
where inputs = map (getInputForIndex xs) [0..(length patterns)]
|
||||
|
||||
doSteps :: [Int] -> [[Int]] -> Int -> [Int]
|
||||
doSteps xs patterns cnt
|
||||
| cnt == 0 = xs
|
||||
| otherwise = doSteps (step xs patterns) patterns (cnt -1)
|
||||
|
||||
|
||||
stepP2 :: [Int] -> Int -> [Int] -> [Int]
|
||||
stepP2 xs sumIn acc
|
||||
| sumIn == 0 = reverse acc
|
||||
| sumIn > 0 = stepP2 (drop 1 xs) (sumIn - (head xs)) ((mod sumIn 10):acc)
|
||||
|
||||
doStepsP2 :: [Int] -> Int ->[Int]
|
||||
doStepsP2 xs cnt
|
||||
| cnt == 0 = xs
|
||||
| otherwise = doStepsP2 (stepP2 xs (sum(xs)) []) (cnt -1)
|
172
day17.hs
Normal file
172
day17.hs
Normal file
@ -0,0 +1,172 @@
|
||||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
let brain = Amplifier software 0 0 [] [0]
|
||||
let mvRoutine = [65,44,66,44,65,44,67,44,66,44,65,44,67,44,66,44,65,44,67,10]
|
||||
let mvFuncA = [76,44,49,50,44,76,44,49,50,44,76,44,54,44,76,44,54,10]
|
||||
let mvFuncB = [82,44,56,44,82,44,52,44,76,44,49,50,10]
|
||||
let mvFuncC = [76,44,49,50,44,76,44,54,44,82,44,49,50,44,82,44,56,10]
|
||||
let videoStream = [110,10]
|
||||
let input = concat [mvRoutine, mvFuncA, mvFuncB, mvFuncC, videoStream]
|
||||
let dust = (output (step brain input))
|
||||
putStrLn (show dust)
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,base :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int]
|
||||
} deriving Show
|
||||
|
||||
data Robot = Robot{ brain:: Amplifier
|
||||
,points:: [((Int,Int),Int)]
|
||||
,position:: (Int,Int)
|
||||
,direction :: Int
|
||||
} deriving Show
|
||||
|
||||
getBrain :: Robot -> Amplifier
|
||||
getBrain (Robot brain points poisition direction) = brain
|
||||
|
||||
getSymbol :: Int -> Char
|
||||
getSymbol 35 = '#'
|
||||
getSymbol 46 = '.'
|
||||
getSymbol 60 = '<'
|
||||
getSymbol 62 = '>'
|
||||
getSymbol 94 = '^'
|
||||
getSymbol 118 = 'v'
|
||||
getSymbol x = '?'
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) (base amp) input []
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i base input output =
|
||||
Amplifier state i base input output
|
||||
operation (op:xs) state i base input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i base input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) base state
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex newstate) (newstate) newindex base newinput output
|
||||
| last (digits op) == 4 = do
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) base state
|
||||
let newinput = drop 1 input
|
||||
operation ((drop newindex state)) (state) newindex base input (newoutput)
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 9 ) = do
|
||||
let newindex = i + 2
|
||||
let fullop = (fillup (revertdigs op) 3)
|
||||
let newbase = base + (getValue (fullop!!2) (xs!!0) base state)
|
||||
(operation ((drop newindex state)) (state) newindex newbase input output)
|
||||
|
||||
add :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
add (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) + (getValue m2 p2 base state)
|
||||
|
||||
mult :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
mult (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) * (getValue m2 p2 base state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input base state =
|
||||
Main.insert state input (getIndex m1 p1 base)
|
||||
|
||||
|
||||
out :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
out (op1:op2:m1:_) output p1 base state =
|
||||
output ++ [(getValue m1 p1 base state)]
|
||||
|
||||
jumpif :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpif (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) /= 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpifnot (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) == 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
lessthan :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state) < (getValue m2 p2 base state) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
equal :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
equal (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state ) == (getValue m2 p2 base state ) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
|
||||
insert :: [Int] -> Int -> Int -> [Int]
|
||||
insert xs value index
|
||||
| index < length xs = do
|
||||
let split = splitAt index xs
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
| otherwise = do
|
||||
let longState = xs ++ (replicate (index - length xs) 0)
|
||||
let split = splitAt index longState
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
|
||||
read :: [Int] -> Int -> Int
|
||||
read xs index
|
||||
| index < length xs = xs!!index
|
||||
| otherwise = 0
|
||||
|
||||
digits :: Int -> [Int]
|
||||
digits = map Char.digitToInt . show
|
||||
|
||||
revertdigs :: Int -> [Int]
|
||||
revertdigs 0 = []
|
||||
revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
|
||||
|
||||
fillup :: [Int] -> Int -> [Int]
|
||||
fillup array x = array ++ (replicate (x - (length array)) 0)
|
||||
|
||||
getValue :: Int -> Int -> Int -> [Int] -> Int
|
||||
getValue 0 p base array = Main.read array p
|
||||
getValue 1 p base array = p
|
||||
getValue 2 p base array = Main.read array (base + p)
|
||||
|
||||
getIndex :: Int -> Int -> Int -> Int
|
||||
getIndex m p base
|
||||
| m == 0 = p
|
||||
| m == 2 = p + base
|
BIN
day18
Executable file
BIN
day18
Executable file
Binary file not shown.
225
day18.hs
Normal file
225
day18.hs
Normal file
@ -0,0 +1,225 @@
|
||||
import Data.List.Split
|
||||
import Data.List.Unique
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
import Data.Maybe
|
||||
|
||||
main = do
|
||||
mapIn <- lines <$> getContents
|
||||
let robot = Robot mapIn [((5,1),64)] (5,1) 1
|
||||
let resultRobots = (runRobot robot )
|
||||
let connections = concat $ [(getConnections mapIn a b) | a <-"@abcdefghijklmnop", b <- "@abcdefghijklmnop", a /= b]
|
||||
let state = StatePath connections '@' []
|
||||
let result = getPath2 [state]
|
||||
--let nextKey = head (sortBy sortLength reachablePoints)
|
||||
--let newMap = openGate mapIn nextKey
|
||||
--let aRobotLength = List.minimum( map (length . points) $ aRobots)
|
||||
--let aRobotWin = filter(\(Robot brain points position direction) -> length points == aRobotLength) aRobots
|
||||
--let winRobots = filter(\(Robot br pts pos dir) -> elem 2 (map(\(p,c) -> c) pts))resultRobots
|
||||
--let winRobotsLength = map(\(Robot br pts pos dir) -> length (filter(\(p,c) -> c == 1)pts))winRobots
|
||||
--let winRobot = (map(\(Robot br pts pos dir) -> Robot br [] pos 1) winRobots) !! 0
|
||||
--let part2Robots = (runRobot winRobot)
|
||||
--let part2Length = map(\(Robot br pts pos dir) -> length (filter(\(p,c) -> c == 1)pts))part2Robots
|
||||
--putStrLn(show winRobotsLength)
|
||||
--putStrLn(show $ List.maximum( part2Length))
|
||||
mapM putStrLn(map show mapIn)
|
||||
mapM putStrLn(map show connections)
|
||||
putStrLn(show result)
|
||||
|
||||
--putStrLn(printKey nextKey)
|
||||
--mapM putStrLn(map show resultRobots)
|
||||
|
||||
data Robot = Robot{ brain:: [[Char]]
|
||||
,points:: [((Int,Int),Int)]
|
||||
,position:: (Int,Int)
|
||||
,direction :: Int
|
||||
} deriving Show
|
||||
|
||||
data Key = Key { id :: Int,
|
||||
pos :: (Int,Int),
|
||||
way :: Int
|
||||
} deriving (Show, Eq)
|
||||
|
||||
data State = State { m :: [[Char]],
|
||||
posi :: (Int,Int),
|
||||
keys :: [Key]
|
||||
} deriving Show
|
||||
|
||||
data StatePath = StatePath { connection :: [Connection],
|
||||
p :: Char,
|
||||
path :: [(Char,Int)]
|
||||
} deriving (Show, Eq)
|
||||
|
||||
|
||||
data Connection =Connection { key1 :: Char,
|
||||
key2 :: Char,
|
||||
l :: Int,
|
||||
block :: [Char]
|
||||
} deriving (Show, Eq)
|
||||
|
||||
getPath2 :: [StatePath] -> Int
|
||||
getPath2 states = do
|
||||
let choose = (sortBy sortL( filter(\state -> getLength state < (min)) possible))
|
||||
if null choose
|
||||
then min
|
||||
else do
|
||||
let newChoose = (getPath (head choose) min)
|
||||
getPath2 ((delete (head choose) states) ++ (newChoose))
|
||||
where min = if length completed /= 0 then List.minimum( map(getLength) completed)
|
||||
else 999
|
||||
completed = filter(\state -> length (path state) == 16) states
|
||||
possible = states \\ completed
|
||||
longest =List.maximum $ map(\state -> length (path state)) states
|
||||
|
||||
sortL :: StatePath -> StatePath -> Ordering
|
||||
sortL s1 s2
|
||||
| length (path s1) > length (path s2) = LT
|
||||
| length (path s1) < length (path s2) = GT
|
||||
| getLength s1 < getLength s2 = LT
|
||||
| getLength s1 > getLength s2 = GT
|
||||
| otherwise = EQ
|
||||
|
||||
getPath :: StatePath -> Int -> [StatePath]
|
||||
getPath(StatePath conn id path) minIn
|
||||
|length path >= 16 = [ (StatePath conn id path) ]
|
||||
|length possible == 1 = do
|
||||
let c = head possible
|
||||
let newState = stepPath (StatePath conn id path) c
|
||||
if getLength newState > minIn
|
||||
then [newState]
|
||||
else getPath newState minIn
|
||||
|otherwise = do
|
||||
let newStates = map(\c -> stepPath (StatePath conn id path) c) possible
|
||||
newStates
|
||||
where possible = filter(\(Connection k1 k2 _ bs) -> k1 == id && length bs == 0) conn
|
||||
|
||||
stepPath :: StatePath -> Connection -> StatePath
|
||||
stepPath (StatePath conn id path) c = do
|
||||
let newConn' = map(\(Connection k1 k2 l b) -> (Connection k1 k2 l (delete (toUpper (key2 c)) b))) conn
|
||||
let newConn = filter(\(Connection k1 k2 l b) -> (k1 /= (key1 c)) && (k2 /= (key1 c)) ) newConn'
|
||||
let newId = key2 c
|
||||
let newPath = (newId, l c):path
|
||||
StatePath ( newConn ) newId newPath
|
||||
|
||||
getCoordinate :: [[Char]] -> Char -> (Int,Int)
|
||||
getCoordinate mapIn id = do
|
||||
let yAxis = head $ filter(\y -> elem id y) mapIn
|
||||
let yAxisV = fromJust $ elemIndex yAxis mapIn
|
||||
let xAxisV = fromJust $ elemIndex id yAxis
|
||||
(xAxisV,yAxisV)
|
||||
|
||||
getConnections :: [[Char]] -> Char -> Char -> [Connection]
|
||||
getConnections mapIn id goal = conn
|
||||
where robot = Robot (mapIn) [((a,b),64)] (a,b) 1
|
||||
resultRobots = (runRobot robot (ord goal))
|
||||
conn = (getConnection (resultRobots) id goal)
|
||||
(a,b) = getCoordinate mapIn id
|
||||
|
||||
|
||||
getConnection :: [Robot] -> Char -> Char -> [Connection]
|
||||
getConnection robots startKey goal = conn
|
||||
where paths = map (\(Robot brain points position direction) -> points) robots
|
||||
pkPair = map(\ps -> ((snd (last ps)),ps)) paths
|
||||
gPkPair = filter(\(k,pth) -> (chr k) == goal) pkPair
|
||||
conn = map(\(k,pth) -> Connection startKey (chr k) (length pth) (blocks pth)) gPkPair
|
||||
blocks xs = map(\(_,c) -> (chr c)) $ filter(\(_,c) -> between 65 c 90) xs
|
||||
|
||||
getLength :: StatePath -> Int
|
||||
getLength (StatePath _ _ path) = do
|
||||
let keyL = map(\(_,a) -> a - 1) path
|
||||
sum (keyL)
|
||||
|
||||
|
||||
stepKey :: State -> Key -> State
|
||||
stepKey (State mapIn (a,b) keys) nextKey = do
|
||||
let newMap = openGate mapIn (nextKey)
|
||||
let newKeys = nextKey:keys
|
||||
State newMap (pos nextKey) newKeys
|
||||
|
||||
sortLength :: Key -> Key -> Ordering
|
||||
sortLength (Key _ _ way1) (Key _ _ way2)
|
||||
| way1 == way2 = EQ
|
||||
| way1 < way2 = LT
|
||||
| way1 > way2 = GT
|
||||
|
||||
openGate :: [[Char]] -> Key -> [[Char]]
|
||||
openGate mapIn (Key id (a,b) _) = result
|
||||
where result' = map( map(\c -> if c==(chr id) then '.' else c)) mapIn
|
||||
result = map( map(\c -> if c==(chr (id-32)) then '.' else c)) result'
|
||||
|
||||
printKey :: Key -> [Char]
|
||||
printKey (Key id pos way) = (show id)++" :"++(show pos)++(show way)
|
||||
|
||||
getBrain :: Robot -> [[Char]]
|
||||
getBrain (Robot brain points poisition direction) = brain
|
||||
|
||||
|
||||
getNextKey :: [Robot] -> [Key]
|
||||
getNextKey robots = nub minPts
|
||||
where kPts = map(\(Robot brain points position direction) ->(Key (snd(last points)) (fst(last points)) (length points))) robots
|
||||
minPts = map(\(Key id pos way) ->(Key id pos (min id))) keyWPr
|
||||
min x = List.minimum $ map(\(Key id pos way) -> way) $ filter(\(Key id pos way) -> id == x) keyWPr
|
||||
keys = filter(\(Key id pos way) -> id /= 46 && id /= 64 && between 97 id 122) kPts
|
||||
keyWPr = keys
|
||||
|
||||
min :: [Int] -> Int
|
||||
min xs = foldr1 (\x y -> if x < y then x else y) xs
|
||||
|
||||
runRobot :: Robot -> Int -> [Robot]
|
||||
runRobot robot goal
|
||||
| (length move) == 0 = [robot]
|
||||
| (length move) == 1 = do
|
||||
let newRobot = stepRobot robot ( move!!0 ) goal
|
||||
runRobot newRobot goal
|
||||
| otherwise = do
|
||||
let newRobots = map(\mv -> stepRobot robot mv goal) move
|
||||
foldl (++) [] $ map(\robot -> runRobot robot goal) newRobots
|
||||
where move = getNextMove robot
|
||||
|
||||
stepRobot :: Robot -> Int -> Int -> Robot
|
||||
stepRobot (Robot brain points position direction) newDirection goal = do
|
||||
let newPos = move position newDirection
|
||||
let statusResponse = ord ((brain!! (snd newPos))!! (fst newPos))
|
||||
let newPoints = (points) ++ [(newPos, statusResponse)]
|
||||
if statusResponse == 35 || statusResponse == goal
|
||||
then Robot brain newPoints position newDirection
|
||||
else Robot brain newPoints newPos newDirection
|
||||
|
||||
between :: Int -> Int -> Int -> Bool
|
||||
between x y z
|
||||
|x <= y = y <= z
|
||||
|otherwise = False
|
||||
|
||||
move :: (Int,Int) -> Int -> (Int,Int)
|
||||
move (x,y) direction
|
||||
| direction == 1 = (x,y-1)
|
||||
| direction == 2 = (x+1,y)
|
||||
| direction == 3 = (x,y+1)
|
||||
| direction == 4 = (x-1,y)
|
||||
|
||||
getNextMove :: Robot -> [Int]
|
||||
getNextMove (Robot brain points position direction)
|
||||
|length points > 0 && (snd $ last points) == 2 = []
|
||||
|otherwise = do
|
||||
filterMoves (Robot brain points position direction) [1,2,3,4]
|
||||
|
||||
filterMoves :: Robot -> [Int] -> [Int]
|
||||
filterMoves robot moves = filter(\x -> checkVisit robot x && checkWall robot x) moves
|
||||
|
||||
checkVisit :: Robot -> Int -> Bool
|
||||
checkVisit (Robot brain points position direction) mv = do
|
||||
let newPos = move position mv
|
||||
let visits = map(\(pos,c) -> pos) points
|
||||
notElem newPos visits
|
||||
|
||||
checkWall :: Robot -> Int -> Bool
|
||||
checkWall (Robot brain points position direction) mv = do
|
||||
let newPos = (move position mv)
|
||||
let mvResult = ( ord ((brain!! (snd newPos))!! (fst newPos)))
|
||||
not (mvResult == 35 )
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
192
day18rw.hs
Normal file
192
day18rw.hs
Normal file
@ -0,0 +1,192 @@
|
||||
import Data.List.Split
|
||||
import Data.List.Unique
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
import Data.Maybe
|
||||
|
||||
main = do
|
||||
mapIn <- lines <$> getContents
|
||||
let connections = concat $ [(getConnections mapIn a b) | a <-"@abcdefghijklmnop", b <- "@abcdefghijklmnop", a /= b]
|
||||
let state = State ['@'] 0 '@'
|
||||
let result = part1 [state] [] connections 999
|
||||
mapM putStrLn(map show connections)
|
||||
putStrLn(show result)
|
||||
|
||||
data Connection =Connection { key1 :: Char,
|
||||
key2 :: Char,
|
||||
l :: Int,
|
||||
block :: [Char]
|
||||
} deriving (Show, Eq)
|
||||
|
||||
data Robot = Robot{ brain :: [[Char]]
|
||||
,points :: [((Int,Int),Int)]
|
||||
,position :: (Int,Int)
|
||||
,direction :: Int
|
||||
} deriving Show
|
||||
|
||||
data State = State { elements :: [Char],
|
||||
len :: Int,
|
||||
pos :: Char
|
||||
} deriving (Show,Eq)
|
||||
|
||||
|
||||
part1 :: [State] -> [State] -> [Connection] -> Int -> Int
|
||||
part1 states deadStates conns minimum
|
||||
|length finished > 3 =
|
||||
min
|
||||
|otherwise = do
|
||||
let choose = chooseNext (filtered)(min)
|
||||
let newStates = runState (choose) deadStates conns min
|
||||
if (newStates) == choose
|
||||
then do
|
||||
let newDeadStates = newStates:deadStates
|
||||
part1 states newDeadStates conns min
|
||||
else do
|
||||
let nextStates = newStates:(states)
|
||||
part1 nextStates deadStates conns min
|
||||
where finished = (filter(\(State elm _ _) -> (length elm) >= 17) (states))
|
||||
min = if length finished > 0
|
||||
then List.minimum (map(\(State _ l _) -> l) finished)
|
||||
else minimum
|
||||
filtered = states \\ deadStates
|
||||
chooseNext :: [State] -> Int -> State
|
||||
chooseNext states min = do
|
||||
let possible' = filter(\(State elm len pos) -> (length elm) < 17) states
|
||||
let possible = filter(\(State elm len pos) -> len < min) possible'
|
||||
last ( sortBy sortElm (possible))
|
||||
|
||||
|
||||
sortLen :: State -> State -> Ordering
|
||||
sortLen (State e1 l1 p1) (State e2 l2 p2)
|
||||
| l1 > l2 = GT
|
||||
| l1 < l2 = LT
|
||||
| l1 == l2 = EQ
|
||||
|
||||
sortElm :: State -> State -> Ordering
|
||||
sortElm (State e1 l1 _) (State e2 l2 _)
|
||||
| length (e1) > length (e2) = GT
|
||||
| length (e1) < length (e2) = LT
|
||||
| l1 > l2 = GT
|
||||
| l1 < l2 = LT
|
||||
| otherwise = EQ
|
||||
|
||||
runState :: State -> [State] -> [Connection] -> Int -> State
|
||||
runState (State elm len pos) deadStates conns min
|
||||
| length possible == 0 = (State elm len pos)
|
||||
| length possible == 1 = do
|
||||
let c = head possible
|
||||
stepState (State elm len pos) c
|
||||
| otherwise = do
|
||||
let choose = head (sortBy sortConn possible)
|
||||
-- let newStates = map(\c -> stepState (State elm len pos) c) possible
|
||||
stepState (State elm len pos) choose
|
||||
where possible''' = filter(\(Connection k1 k2 l b) -> length ( b \\ notBlocked) == 0 )possible''''
|
||||
possible'' = filter(\(Connection _ _ l _) -> len + l < min) possible'''
|
||||
possible' = filter(\(Connection k1 k2 l b) -> notElem k2 (deadPath)) possible''
|
||||
possible = filter(\(Connection k1 k2 l b) -> notElem k2 elm) possible'
|
||||
possible'''' = filter(\(Connection k1 k2 l b) -> k1 == pos) conns
|
||||
notBlocked = concat $ map(\x -> (toUpper x):[x]) elm
|
||||
dead = filter(\(State el _ _) -> (tail el) == elm) deadStates
|
||||
deadPath = map(\(State el _ _) -> head el) dead
|
||||
|
||||
sortConn :: Connection -> Connection -> Ordering
|
||||
sortConn (Connection _ _ l1 _) (Connection _ _ l2 _)
|
||||
| l1 < l2 = LT
|
||||
| l1 > l2 = GT
|
||||
| l1 == l2 = EQ
|
||||
|
||||
stepState :: State -> Connection -> State
|
||||
stepState (State elm len pos) (Connection _ k2 l b) = do
|
||||
(State (k2:elm) (len + l) k2)
|
||||
|
||||
|
||||
getCoordinate :: [[Char]] -> Char -> (Int,Int)
|
||||
getCoordinate mapIn id = do
|
||||
let yAxis = head $ filter(\y -> elem id y) mapIn
|
||||
let yAxisV = fromJust $ elemIndex yAxis mapIn
|
||||
let xAxisV = fromJust $ elemIndex id yAxis
|
||||
(xAxisV,yAxisV)
|
||||
|
||||
getConnections :: [[Char]] -> Char -> Char -> [Connection]
|
||||
getConnections mapIn id goal = conn
|
||||
where robot = Robot (mapIn) [((a,b),64)] (a,b) 1
|
||||
resultRobots = (runRobot robot (ord goal))
|
||||
conn = (getConnection (resultRobots) id goal)
|
||||
(a,b) = getCoordinate mapIn id
|
||||
|
||||
getConnection :: [Robot] -> Char -> Char -> [Connection]
|
||||
getConnection robots startKey goal = conn
|
||||
where paths = map (\(Robot brain points position direction) -> points) robots
|
||||
pkPair = map(\ps -> ((snd (last ps)),ps)) paths
|
||||
gPkPair = filter(\(k,pth) -> (chr k) == goal ) pkPair
|
||||
conn = map(\(k,pth) -> Connection startKey (chr k) (length pth) (delete goal (blocks pth))) gPkPair
|
||||
blocks xs = map(\(_,c) -> (chr c)) $ filter(\(_,c) -> between 65 c 90 || between 97 c 122) xs
|
||||
|
||||
|
||||
runRobot :: Robot -> Int -> [Robot]
|
||||
runRobot robot goal
|
||||
| (length move) == 0 = [robot]
|
||||
| (length move) == 1 = do
|
||||
let newRobot = stepRobot robot ( move!!0 ) goal
|
||||
runRobot newRobot goal
|
||||
| otherwise = do
|
||||
let newRobots = map(\mv -> stepRobot robot mv goal) move
|
||||
foldl (++) [] $ map(\robot -> runRobot robot goal) newRobots
|
||||
where move = getNextMove robot
|
||||
|
||||
stepRobot :: Robot -> Int -> Int -> Robot
|
||||
stepRobot (Robot brain points position direction) newDirection goal = do
|
||||
let newPos = move position newDirection
|
||||
let statusResponse = ord ((brain!! (snd newPos))!! (fst newPos))
|
||||
let newPoints = (points) ++ [(newPos, statusResponse)]
|
||||
if statusResponse == 35 || statusResponse == goal
|
||||
then Robot brain newPoints position newDirection
|
||||
else Robot brain newPoints newPos newDirection
|
||||
|
||||
|
||||
move :: (Int,Int) -> Int -> (Int,Int)
|
||||
move (x,y) direction
|
||||
| direction == 1 = (x,y-1)
|
||||
| direction == 2 = (x+1,y)
|
||||
| direction == 3 = (x,y+1)
|
||||
| direction == 4 = (x-1,y)
|
||||
|
||||
getNextMove :: Robot -> [Int]
|
||||
getNextMove (Robot brain points position direction)
|
||||
|length points > 0 && (snd $ last points) == 2 = []
|
||||
|otherwise = do
|
||||
filterMoves (Robot brain points position direction) [1,2,3,4]
|
||||
|
||||
filterMoves :: Robot -> [Int] -> [Int]
|
||||
filterMoves robot moves = filter(\x -> checkVisit robot x && checkWall robot x) moves
|
||||
|
||||
checkVisit :: Robot -> Int -> Bool
|
||||
checkVisit (Robot brain points position direction) mv = do
|
||||
let newPos = move position mv
|
||||
let visits = map(\(pos,c) -> pos) points
|
||||
notElem newPos visits
|
||||
|
||||
checkWall :: Robot -> Int -> Bool
|
||||
checkWall (Robot brain points position direction) mv = do
|
||||
let newPos = (move position mv)
|
||||
let mvResult = ( ord ((brain!! (snd newPos))!! (fst newPos)))
|
||||
not (mvResult == 35 )
|
||||
|
||||
between :: Int -> Int -> Int -> Bool
|
||||
between x y z
|
||||
|x <= y = y <= z
|
||||
|otherwise = False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
32
day8.hs
Normal file
32
day8.hs
Normal file
@ -0,0 +1,32 @@
|
||||
import Data.List as List
|
||||
|
||||
|
||||
main = do
|
||||
content <- getContents
|
||||
let input = map (read . (:"")) content
|
||||
let layers = getLayers input (6*25) []
|
||||
let min = minimum (map (\x-> countOccurence x 0) layers)
|
||||
let layer = head (filter(\x-> countOccurence x 0 == min) layers)
|
||||
putStrLn (show $ (countOccurence layer 1) * (countOccurence layer 2))
|
||||
let picture = foldl combineLayer (head layers) (tail layers)
|
||||
mapM putStrLn (map show (getLayers picture 25 []))
|
||||
|
||||
|
||||
getLayers :: [Int] -> Int -> [[Int]] -> [[Int]]
|
||||
getLayers xs x ys= do
|
||||
if (length xs) >= x
|
||||
then do
|
||||
let split = splitAt x xs
|
||||
getLayers (snd split) x (ys ++ [fst split])
|
||||
else ys
|
||||
|
||||
countOccurence :: [Int] -> Int -> Int
|
||||
countOccurence xs x= length $ filter (x==) xs
|
||||
|
||||
compare :: Int -> Int -> Int
|
||||
compare 0 y = 0
|
||||
compare 1 y = 1
|
||||
compare 2 y = y
|
||||
|
||||
combineLayer :: [Int] -> [Int] -> [Int]
|
||||
combineLayer xs ys = zipWith Main.compare xs ys
|
155
day9.hs
Normal file
155
day9.hs
Normal file
@ -0,0 +1,155 @@
|
||||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
import Debug.Trace as Trace
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
let amp = Amplifier software 0 0 [] []
|
||||
let amp2 = step amp [2]
|
||||
putStrLn(show ( output amp2 ))
|
||||
putStrLn ("HELLO")
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,base :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int]
|
||||
} deriving Show
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map Prelude.read . splitOn ","
|
||||
|
||||
link :: Amplifier -> Amplifier -> Amplifier
|
||||
link left calc
|
||||
| null (output left) = Amplifier (state calc) (-1) (base calc) (input calc) (output calc)
|
||||
| index left == -1 = Amplifier (state calc) (-1) (base calc) (input calc) (output calc)
|
||||
| otherwise = step calc ([last $ output left])
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) (base amp) input []
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i base input output =
|
||||
Amplifier state i base input output
|
||||
operation (op:xs) state i base input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i base input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) base state
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex newstate) (newstate) newindex base newinput output
|
||||
| last (digits op) == 4 = do
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) base state
|
||||
let newinput = drop 1 input
|
||||
operation ((drop newindex state)) (state) newindex base input (newoutput)
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i base state
|
||||
operation ((drop newindex state)) (state) newindex base input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) base state
|
||||
operation ((drop newindex newstate)) (newstate) newindex base input output
|
||||
| (last (digits op) == 9 ) = do
|
||||
let newindex = i + 2
|
||||
let fullop = (fillup (revertdigs op) 3)
|
||||
let newbase = base + (getValue (fullop!!2) (xs!!0) base state)
|
||||
(operation ((drop newindex state)) (state) newindex newbase input output)
|
||||
|
||||
add :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
add (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) + (getValue m2 p2 base state)
|
||||
|
||||
mult :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
mult (op1:op2:m1:m2:m3:_) p1 p2 p3 base state =
|
||||
Main.insert state sum (getIndex m3 p3 base)
|
||||
where
|
||||
sum = (getValue m1 p1 base state) * (getValue m2 p2 base state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input base state =
|
||||
Main.insert state input (getIndex m1 p1 base)
|
||||
|
||||
|
||||
out :: [Int] -> [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
out (op1:op2:m1:_) output p1 base state =
|
||||
output ++ [(getValue m1 p1 base state)]
|
||||
|
||||
jumpif :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpif (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) /= 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> Int
|
||||
jumpifnot (op1:op2:m1:m2:_) p1 p2 index base state
|
||||
| (getValue m1 p1 base state) == 0 = getValue m2 p2 base state
|
||||
| otherwise = index + 3
|
||||
|
||||
lessthan :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state) < (getValue m2 p2 base state) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
equal :: [Int] -> Int -> Int -> Int -> Int -> [Int] -> [Int]
|
||||
equal (op1:op2:m1:m2:m3:_) p1 p2 p3 base state
|
||||
| (getValue m1 p1 base state ) == (getValue m2 p2 base state ) =
|
||||
Main.insert state 1 (getIndex m3 p3 base)
|
||||
| otherwise = Main.insert state 0 (getIndex m3 p3 base)
|
||||
|
||||
|
||||
insert :: [Int] -> Int -> Int -> [Int]
|
||||
insert xs value index
|
||||
| index < length xs = do
|
||||
let split = splitAt index xs
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
| otherwise = do
|
||||
let longState = xs ++ (replicate (index - length xs) 0)
|
||||
let split = splitAt index longState
|
||||
(fst split)++ [value] ++ (drop 1 (snd split))
|
||||
|
||||
read :: [Int] -> Int -> Int
|
||||
read xs index
|
||||
| index < length xs = xs!!index
|
||||
| otherwise = 0
|
||||
|
||||
digits :: Int -> [Int]
|
||||
digits = map Char.digitToInt . show
|
||||
|
||||
revertdigs :: Int -> [Int]
|
||||
revertdigs 0 = []
|
||||
revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
|
||||
|
||||
fillup :: [Int] -> Int -> [Int]
|
||||
fillup array x = array ++ (replicate (x - (length array)) 0)
|
||||
|
||||
getValue :: Int -> Int -> Int -> [Int] -> Int
|
||||
getValue 0 p base array = Main.read array p
|
||||
getValue 1 p base array = p
|
||||
getValue 2 p base array = Main.read array (base + p)
|
||||
|
||||
getIndex :: Int -> Int -> Int -> Int
|
||||
getIndex m p base
|
||||
| m == 0 = p
|
||||
| m == 2 = p + base
|
1
input11.txt
Normal file
1
input11.txt
Normal file
@ -0,0 +1 @@
|
||||
3,8,1005,8,309,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,29,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,51,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,72,1,1104,8,10,2,1105,15,10,2,1106,0,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,107,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,128,2,6,8,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,155,1006,0,96,2,108,10,10,1,101,4,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,188,2,1,5,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,214,2,6,18,10,1006,0,78,1,105,1,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,247,2,103,8,10,2,1002,10,10,2,106,17,10,1,1006,15,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,285,1,1101,18,10,101,1,9,9,1007,9,992,10,1005,10,15,99,109,631,104,0,104,1,21102,387507921664,1,1,21102,1,326,0,1106,0,430,21102,932826591260,1,1,21102,337,1,0,1106,0,430,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,206400850983,0,1,21101,0,384,0,1105,1,430,21102,3224464603,1,1,21102,395,1,0,1106,0,430,3,10,104,0,104,0,3,10,104,0,104,0,21102,838433657700,1,1,21102,418,1,0,1106,0,430,21101,825012007272,0,1,21101,429,0,0,1106,0,430,99,109,2,21202,-1,1,1,21101,40,0,2,21101,461,0,3,21102,1,451,0,1105,1,494,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,456,457,472,4,0,1001,456,1,456,108,4,456,10,1006,10,488,1102,1,0,456,109,-2,2106,0,0,0,109,4,1202,-1,1,493,1207,-3,0,10,1006,10,511,21101,0,0,-3,21202,-3,1,1,21201,-2,0,2,21102,1,1,3,21102,1,530,0,1106,0,535,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,558,2207,-4,-2,10,1006,10,558,22101,0,-4,-4,1106,0,626,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,577,0,1106,0,535,22102,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,596,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,618,21201,-1,0,1,21102,618,1,0,105,1,493,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
|
Loading…
Reference in New Issue
Block a user