Move 2015 to haskellAoC & change Days listing

This commit is contained in:
Xavier Morel
2020-12-03 11:29:19 +01:00
parent 7dc5d839af
commit 74f24e0dfb
11 changed files with 79 additions and 51 deletions

View File

@@ -1,12 +0,0 @@
import Data.List
day1steps :: [Char] -> [Int]
day1steps xs = [if x == '(' then 1 else -1 | x <- xs]
day1p1 :: [Char] -> Int
day1p1 xs = sum(day1steps xs)
day1p2 :: [Char] -> Maybe Int
day1p2 xs = findIndex (== entrance) steps
where steps = scanl (\acc x -> acc + x) 0 (day1steps xs)
entrance = -1

View File

@@ -1,47 +0,0 @@
import Data.List.Split
computeSides :: [Int] -> [Int]
computeSides dims = [l * w, w * h, h * l]
-- Not really safe...
where l = dims !! 0
w = dims !! 1
h = dims !! 2
computeWrapping :: [Int] -> Int
computeWrapping dims = sum (map (* 2) sides)
where sides = computeSides dims
computeSlack :: [Int] -> Int
computeSlack dims = minimum sides
where sides = computeSides dims
computeRibbonWrap :: [Int] -> Int
computeRibbonWrap dims = (sum dims - maximum dims) * 2
computeRibbonBow :: [Int] -> Int
computeRibbonBow dims = product dims
parseDims :: String -> [Int]
parseDims input = map read (splitOn "x" input)
part1computation :: [Int] -> Int
part1computation dims = computeWrapping dims + computeSlack dims
part2computation :: [Int] -> Int
part2computation dims = computeRibbonWrap dims + computeRibbonBow dims
day2 :: ([Int] -> Int) -> String -> Int
day2 fn input = sum paper
where entries = lines input
dims = map parseDims entries
paper = map fn dims
main :: IO ()
main = do
input <- getContents
putStr "Part1: "
print (day2 part1computation input)
putStrLn ""
putStr "Part2: "
print (day2 part2computation input)
putStrLn ""

View File

@@ -1,28 +0,0 @@
import Data.List
move :: [(Int, Int)] -> Char -> [(Int, Int)]
move ((x, y):xs) '>' = (x + 1, y):((x, y):xs)
move ((x, y):xs) '<' = (x - 1, y):((x, y):xs)
move ((x, y):xs) '^' = (x, y + 1):((x, y):xs)
move ((x, y):xs) 'v' = (x, y - 1):((x, y):xs)
houses :: [Char] -> [(Int, Int)]
houses input = foldl move [(0, 0)] input
foldlBoth :: (a -> b -> a) -> (a, a) -> [b] -> (a, a)
foldlBoth f (l1, l2) (x1:(x2:xs)) = foldlBoth f (f l1 x1, f l2 x2) xs
foldlBoth f (l1, l2) (x1:_) = foldlBoth f (f l1 x1, l2) []
foldlBoth f (l1, l2) [] = (l1, l2)
housesWithBot :: [Char] -> [(Int, Int)]
housesWithBot input = santaHouses ++ botHouses
where (santaHouses, botHouses) = foldlBoth move ([(0, 0)], [(0, 0)]) input
uniqueHouses :: [(Int, Int)] -> [((Int, Int), Int)]
uniqueHouses input = (map (\xs -> (head xs, length xs)) . group . sort) input
day3part1 :: [Char] -> Int
day3part1 input = length (uniqueHouses (houses input))
day3part2 :: [Char] -> Int
day3part2 input = length (uniqueHouses (housesWithBot input))