Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjercan committed Dec 5, 2023
1 parent 61451be commit a104ae9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
30 changes: 15 additions & 15 deletions src/Day05.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Day05 (main, part1, part2) where

import Data.List.Split (splitOn)

data MapItem = MapItem { dest :: Int, src :: Int, len :: Int } deriving (Show)
data MapItem = MapItem {dest :: Int, src :: Int, len :: Int} deriving (Show)

parseMapItem :: String -> MapItem
parseMapItem line = case map read $ words line of
Expand All @@ -14,41 +14,41 @@ parseMap = map parseMapItem . tail . lines

parseMaps :: String -> ([Int], [[MapItem]])
parseMaps input = case splitOn "\n\n" input of
(x:ls) -> (map read $ tail $ words x, map parseMap ls)
(x : ls) -> (map read $ tail $ words x, map parseMap ls)
_ -> error "Invalid input"

mapRange :: Int -> [MapItem] -> Int
mapRange x [] = x
mapRange x (MapItem d s l:xs)
mapRange x (MapItem d s l : xs)
| s <= x && x < s + l = d + x - s
| otherwise = mapRange x xs

part1 :: String -> String
part1 input = show $ minimum $ map (\x -> foldl mapRange x maps) seeds
where (seeds, maps) = parseMaps input
where
(seeds, maps) = parseMaps input

mapRange' :: (Int, Int) -> [MapItem] -> [(Int, Int)]
mapRange' x [] = [x]
mapRange' (rs, rl) (MapItem d s l:ms)
mapRange' (rs, rl) (MapItem d s l : ms)
| rs <= s + l && s < rs + rl = pre ++ curr ++ post
| otherwise = mapRange' (rs, rl) ms
where
pre = if rs < s then mapRange' (rs, s - rs) ms else []
curr = [(d + max 0 (rs - s), min rl (l - max 0 (rs - s)))]
post = if s + l < rs + rl then mapRange' (s + l, rs + rl - s - l) ms else []
where
pre = if rs < s then mapRange' (rs, s - rs) ms else []
curr = [(d + max 0 (rs - s), min rl (l - max 0 (rs - s)))]
post = if s + l < rs + rl then mapRange' (s + l, rs + rl - s - l) ms else []

pairUp :: [a] -> [(a, a)]
pairUp [] = []
pairUp (x:y:rest) = (x, y) : pairUp rest
pairUp (x : y : rest) = (x, y) : pairUp rest
pairUp _ = error "Input list should have an even number of elements."

part2 :: String -> String
part2 input = show $ fst $ minimum $ foldl mapRange'' (pairUp seeds) maps
where
(seeds, maps) = parseMaps input
mapRange'' :: [(Int, Int)] -> [MapItem] -> [(Int, Int)]
mapRange'' xs ms = concatMap (`mapRange'` ms) xs

where
(seeds, maps) = parseMaps input
mapRange'' :: [(Int, Int)] -> [MapItem] -> [(Int, Int)]
mapRange'' xs ms = concatMap (`mapRange'` ms) xs

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"
Expand Down
72 changes: 36 additions & 36 deletions test/TestDay05.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ module TestDay05 where
import Day05
import Test.Hspec

input = unlines
[
"seeds: 79 14 55 13",
"",
"seed-to-soil map:",
"50 98 2",
"52 50 48",
"",
"soil-to-fertilizer map:",
"0 15 37",
"37 52 2",
"39 0 15",
"",
"fertilizer-to-water map:",
"49 53 8",
"0 11 42",
"42 0 7",
"57 7 4",
"",
"water-to-light map:",
"88 18 7",
"18 25 70",
"",
"light-to-temperature map:",
"45 77 23",
"81 45 19",
"68 64 13",
"",
"temperature-to-humidity map:",
"0 69 1",
"1 0 69",
"",
"humidity-to-location map:",
"60 56 37",
"56 93 4"
]
input =
unlines
[ "seeds: 79 14 55 13"
, ""
, "seed-to-soil map:"
, "50 98 2"
, "52 50 48"
, ""
, "soil-to-fertilizer map:"
, "0 15 37"
, "37 52 2"
, "39 0 15"
, ""
, "fertilizer-to-water map:"
, "49 53 8"
, "0 11 42"
, "42 0 7"
, "57 7 4"
, ""
, "water-to-light map:"
, "88 18 7"
, "18 25 70"
, ""
, "light-to-temperature map:"
, "45 77 23"
, "81 45 19"
, "68 64 13"
, ""
, "temperature-to-humidity map:"
, "0 69 1"
, "1 0 69"
, ""
, "humidity-to-location map:"
, "60 56 37"
, "56 93 4"
]

test1 :: Expectation
test1 = part1 input `shouldBe` "35"
Expand Down

0 comments on commit a104ae9

Please sign in to comment.