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,5 +1,6 @@
module Lib (pickYear) where
import Y2015.Days
import Y2019.Days
import Y2020.Days
@@ -11,22 +12,9 @@ import System.Environment
type DayFun = [String] -> (String, String)
getDayFun :: String -> String -> DayFun
getDayFun "2019" "01" = y19day01
getDayFun "2019" "02" = y19day02
getDayFun "2019" "03" = y19day03
getDayFun "2019" "04" = y19day04
getDayFun "2019" "05" = y19day05
getDayFun "2019" "06" = y19day06
getDayFun "2019" "07" = y19day07
getDayFun "2019" "08" = y19day08
getDayFun "2019" "09" = y19day09
getDayFun "2019" "10" = y19day10
getDayFun "2019" "11" = y19day11
getDayFun "2019" "12" = y19day12
getDayFun "2019" "13" = y19day13
getDayFun "2020" "01" = y20day01
getDayFun "2020" "02" = y20day02
getDayFun "2020" "03" = y20day03
getDayFun "2015" = year2015
getDayFun "2019" = year2019
getDayFun "2020" = year2020
callDailyFun :: String -> DayFun -> String -> IO ()
callDailyFun year fn name = do

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

View File

@@ -1,7 +1,4 @@
module Y2019.Days (y19day01, y19day02, y19day03, y19day04, y19day05, y19day06,
y19day07, y19day08, y19day09, y19day10, y19day11, y19day12,
y19day13
) where
module Y2019.Days (year2019) where
import Y2019.Day01
import Y2019.Day02
@@ -17,16 +14,17 @@ import Y2019.Day11
import Y2019.Day12
import Y2019.Day13
y19day01 = day1
y19day02 = day2
y19day03 = day3
y19day04 = day4
y19day05 = day5
y19day06 = day6
y19day07 = day7
y19day08 = day8
y19day09 = day9
y19day10 = day10
y19day11 = day11
y19day12 = day12
y19day13 = day13
year2019 :: String -> [String] -> (String, String)
year2019 "01" = day1
year2019 "02" = day2
year2019 "03" = day3
year2019 "04" = day4
year2019 "05" = day5
year2019 "06" = day6
year2019 "07" = day7
year2019 "08" = day8
year2019 "09" = day9
year2019 "10" = day10
year2019 "11" = day11
year2019 "12" = day12
year2019 "13" = day13

View File

@@ -1,5 +1,11 @@
module Y2020.Days (y20day01, y20day02, y20day03) where
module Y2020.Days (year2020) where
import Y2020.Day01
import Y2020.Day02
import Y2020.Day03
year2020 :: String -> [String] -> (String, String)
year2020 "01" = y20day01
year2020 "02" = y20day02
year2020 "03" = y20day03