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

@@ -0,0 +1,19 @@
module Y2015.Day01 (y15day01) where
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
y15day01 :: [String] -> (String, String)
y15day01 (input:_) = (part1, part2)
where part1 = show $ day1p1 input
part2 = show $ day1p2 input

View File

@@ -0,0 +1,39 @@
module Y2015.Day02 (y15day02) where
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
y15day02 :: [String] -> (String, String)
y15day02 input = (part1, part2)
where part1 = show $ compute part1computation
part2 = show $ compute part2computation
compute fn = sum $ map fn $ map parseDims input

View File

@@ -0,0 +1,35 @@
module Y2015.Day03 (y15day03) where
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))
y15day03 :: [String] -> (String, String)
y15day03 (input:_) = (part1, part2)
where part1 = show $ day3part1 input
part2 = show $ day3part2 input

View File

@@ -0,0 +1,17 @@
module Y2015.Day04 (y15day04) where
import Data.ByteString.Lazy.UTF8 (fromString)
import Data.Digest.Pure.MD5
getLeadingZeroes :: String -> Int -> Int
getLeadingZeroes base nb = nbZeroes
where pass = base ++ (show nb)
md5hash = show $ md5 $ fromString pass
nbZeroes = length $ takeWhile (== '0') md5hash
-- Crappy bruteforce solution...
y15day04 :: [String] -> (String, String)
y15day04 (input:_) = (part1, part2)
where part1 = show $ firstWithNzeroes 5
part2 = show $ firstWithNzeroes 6
firstWithNzeroes n = head $ take 1 $ dropWhile (\h -> getLeadingZeroes input h /= n) $ iterate (+1) 0

View File

@@ -0,0 +1,13 @@
module Y2015.Days (year2015) where
import Y2015.Day01
import Y2015.Day02
import Y2015.Day03
import Y2015.Day04
year2015 :: String -> [String] -> (String, String)
year2015 "01" = y15day01
year2015 "02" = y15day02
year2015 "03" = y15day03
year2015 "04" = y15day04