Luubstar

Dia13 -- AoC 2023

Dec 13th, 2023 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | Source Code | 0 0
  1. module Dia13 where
  2. -- ##############################################
  3. -- ## Imports ##
  4. -- ##############################################
  5.  
  6. import Data.List (transpose, groupBy)
  7.  
  8. -- ##############################################
  9. -- ## Función main ##
  10. -- ##############################################
  11.  
  12. main = do
  13. contents <- (readFile "Input13.txt")
  14. let lin = separarLineasVacias $ lines contents
  15.  
  16. putStrLn $ show $ sum $ map part1 lin
  17. putStrLn "--------"
  18. putStrLn $ show $ sum $ map part2 lin
  19.  
  20. -- ##############################################
  21. -- ## Funciones de la 1º parte ##
  22. -- ##############################################
  23.  
  24. part1 :: [String] -> Int
  25. part1 l = (inRow (Left (transpose l))) + (100*(inRow (Left l)))
  26.  
  27. inRow :: Either [String] ([String], Int) -> Int
  28. inRow (Left l) = inRow (Right (l, 0))
  29. inRow (Right (l, x))
  30. | (x+1) >= length l = 0
  31. | l!!x == l!!(x+1) = if checkMirror x 1 l then x+1 else inRow (Right(l,(x+1)))
  32. | otherwise = inRow (Right (l, (x+1)))
  33.  
  34. checkMirror :: Int -> Int -> [String] -> Bool
  35. checkMirror x dx l
  36. | (x+dx+1) >= length l || (x-dx) < 0 = True
  37. | l!!(x-dx) == l!!(x+1+dx) = checkMirror x (dx+1) l
  38. | otherwise = False
  39.  
  40. -- ##############################################
  41. -- ## Funciones de la 2º parte ##
  42. -- ##############################################
  43.  
  44. part2 :: [String] -> Int
  45. part2 l = 100 * inRow'(Left (l)) + inRow'(Left (transpose l))
  46.  
  47. inRow' :: Either [String] ([String], Int) -> Int
  48. inRow' (Left l) = inRow' (Right (l, 0))
  49. inRow' (Right (l, x))
  50. | (x+1) >= length l = 0
  51. | l!!x == l!!(x+1) = if checkMirror' x 1 False l then x+1 else inRow' (Right(l,(x+1)))
  52. | (length $ diff (Left (l!!x, l!!(x+1)))) == 1 = if checkMirror' x 1 True l then x+1 else inRow' (Right(l,(x+1)))
  53. | otherwise = inRow' (Right (l, (x+1)))
  54.  
  55. checkMirror' :: Int -> Int -> Bool -> [String] -> Bool
  56. checkMirror' x dx errado l
  57. | (x+dx+1) >= length l || (x-dx) < 0 = errado
  58. | l!!(x-dx) == l!!(x+1+dx) = checkMirror' x (dx+1) errado l
  59. | (length $ diff (Left (l!!(x-dx), l!!(x+1+dx)))) == 1 && (not $ errado) = checkMirror' x (dx+1) True l
  60. | otherwise = False
  61.  
  62. -- ##############################################
  63. -- ## Funciones generales ##
  64. -- ##############################################
  65.  
  66. separarLineasVacias :: [String] -> [[String]]
  67. separarLineasVacias = filter (\ c -> length c /= 1) . (groupBy (\a b -> not (null a) && not (null b)))
  68.  
  69. diff :: Either (String, String) (String, String, Int) -> [Int]
  70. diff (Right ((x:xs),(y:ys),p))
  71. | x /= y = [p] ++ diff (Right (xs,ys,(p+1)))
  72. | otherwise = diff (Right (xs,ys,(p+1)))
  73. diff (Right ([], [], _)) = []
  74. diff (Left (l, s)) = diff (Right (l,s,0))
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment