Skip to content

Commit

Permalink
Haskell/2024/05: add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Dec 5, 2024
1 parent e89ab56 commit 179843c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions Haskell/2024/05.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Data.Bifunctor
import Data.List
import Data.List.Split
import Lib

type Rule = (Int, Int)

type Input = ([Rule], [[Int]])

main :: IO ()
main = aoc 2024 5 setup solve1 solve2 ["1"]

solve1 :: Input -> Int
solve1 = uncurry ((.) (sum . map mid) . (filter . (((==) <*>) . sortUpdate)))

solve2 :: Input -> Int
solve2 = uncurry (((.) . ((.) sum . (map . ((.) mid . sortUpdate)))) <*> (filter . (((/=) <*>) . sortUpdate)))

setup :: String -> Input
setup = fork (,) (parseRules . takeWhile (not . null)) (parseUpdates . dropWhile (not . null)) . lines

parseRules :: [String] -> [Rule]
parseRules = map (listToTuple . map read . splitOn "|")

parseUpdates :: [String] -> [[Int]]
parseUpdates = map (map read . splitOn ",") . filter (not . null)

sortUpdate :: [Rule] -> [Int] -> [Int]
sortUpdate = sortBy . cmpPages

cmpPages :: [Rule] -> Int -> Int -> Ordering
cmpPages rules a b
| (a, b) `elem` rules = LT
| (b, a) `elem` rules = GT
| otherwise = EQ

mid :: [a] -> a
mid = (!!) <*> (`div` 2) . length
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
|[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of &ge;255548)|
|[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of &ge;197402)|

## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 5/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 5/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 5/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 5/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 5/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 5/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 4/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|Mo|Tu|We|Th|Fr|Sa|Su|
|-|-|-|-|-|-|-|
|||||||[**1**](https://adventofcode.com/2024/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2024/01.rs "Rust solution for 2024/01") [<img height=12 src=".assets/hs.svg">](Haskell/2024/01.hs "Haskell solution for 2024/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrCsOKKn-KNieKKnOKKnOKLleKIqeKJoEBcbixAIC4K4oipLyviioMo4pmtw5fiip7iirg9fOKMtS3iiKnijYYpCg== "Uiua solution for 2024/01") [<img height=12 src=".assets/nix.svg">](Nix/2024/01.nix "Nix solution for 2024/01") [<img height=12 src=".assets/rb.svg">](Ruby/2024/01.rb "Ruby solution for 2024/01")|
|[**2**](https://adventofcode.com/2024/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2024/02.rs "Rust solution for 2024/02") [<img height=12 src=".assets/hs.svg">](Haskell/2024/02.hs "Haskell solution for 2024/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4pah4oqc4ouVKeKIqeKJoEBcbixAIC4KCkNoZWNrIOKGkCDDl-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKeKniC0K4oipLyviiaHil4fiioMoL-KGpeKJoShDaGVja-KWvSniip7iiaAu4oeh4qe74p-cwqR8Q2hlY2spCg== "Uiua solution for 2024/02") [<img height=12 src=".assets/rb.svg">](Ruby/2024/02.rb "Ruby solution for 2024/02")|[**3**](https://adventofcode.com/2024/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2024/03.rs "Rust solution for 2024/03") [<img height=12 src=".assets/hs.svg">](Haskell/2024/03.hs "Haskell solution for 2024/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03") [<img height=12 src=".assets/rb.svg">](Ruby/2024/03.rb "Ruby solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2024/04.rs "Rust solution for 2024/04") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgripLAg4oaQIOKIqeKKleKWoeKKmSzijZzima3iipviip7iioMtK-KIqeKHocKw4oqf4oq44pazCgrin5woLyviiaHil4coLyvijJUiWE1BUyIp4oqC4p-c4o2a4oeM4oqC4oqDKOKKguKksHziioLiiKniiaHilqHin5zijYkpKQovK-KZreKniCjDl-KIqSgv4oal4omh4omN4pahIk1BUyLiioLin5zijZrih4wp4qSwKTNfMwo= "Uiua solution for 2024/04") [<img height=12 src=".assets/rb.svg">](Ruby/2024/04.rb "Ruby solution for 2024/04")|[**5**](https://adventofcode.com/2024/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2024/05.rs "Rust solution for 2024/05") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriioPihpjihpniiqLiiprijJUiXG5cbiIuCuKKmSjiipziipzii5XiiKniiaBAXG4sQHwuKQriipwo4pah4oqc4ouVKeKIqeKJoEBcbixALC4KCk1pbiAgICDihpAg4oqi4pa9wqziirjiiIg64p-cKOKJoeKKo-KWveKIiOKKmeKKuOKJoeKKoikKUmVtb3ZlIOKGkCDiioLiioMo4oaZfOKGmCsxKeKKouKKmuKKuD0KU29ydCAgIOKGkCDiipnii4Xil4zijaIo4oqD4oqC4ouFUmVtb3Zl4oqZ4pehTWlufOKJoDDip7vil4wpW10KTWlkICAgIOKGkCDiio_ijIrDtzLiirjip7sKCuKIqSgvK-KJoeKXh01pZOKWvSkgwqwsLOKKuOKJoeKJjSDin5zijZpTb3J04oqZwqQK "Uiua solution for 2024/05") [<img height=12 src=".assets/rb.svg">](Ruby/2024/05.rb "Ruby solution for 2024/05")|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)|
|[**2**](https://adventofcode.com/2024/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2024/02.rs "Rust solution for 2024/02") [<img height=12 src=".assets/hs.svg">](Haskell/2024/02.hs "Haskell solution for 2024/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4pah4oqc4ouVKeKIqeKJoEBcbixAIC4KCkNoZWNrIOKGkCDDl-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKeKniC0K4oipLyviiaHil4fiioMoL-KGpeKJoShDaGVja-KWvSniip7iiaAu4oeh4qe74p-cwqR8Q2hlY2spCg== "Uiua solution for 2024/02") [<img height=12 src=".assets/rb.svg">](Ruby/2024/02.rb "Ruby solution for 2024/02")|[**3**](https://adventofcode.com/2024/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2024/03.rs "Rust solution for 2024/03") [<img height=12 src=".assets/hs.svg">](Haskell/2024/03.hs "Haskell solution for 2024/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03") [<img height=12 src=".assets/rb.svg">](Ruby/2024/03.rb "Ruby solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2024/04.rs "Rust solution for 2024/04") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgripLAg4oaQIOKIqeKKleKWoeKKmSzijZzima3iipviip7iioMtK-KIqeKHocKw4oqf4oq44pazCgrin5woLyviiaHil4coLyvijJUiWE1BUyIp4oqC4p-c4o2a4oeM4oqC4oqDKOKKguKksHziioLiiKniiaHilqHin5zijYkpKQovK-KZreKniCjDl-KIqSgv4oal4omh4omN4pahIk1BUyLiioLin5zijZrih4wp4qSwKTNfMwo= "Uiua solution for 2024/04") [<img height=12 src=".assets/rb.svg">](Ruby/2024/04.rb "Ruby solution for 2024/04")|[**5**](https://adventofcode.com/2024/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2024/05.rs "Rust solution for 2024/05") [<img height=12 src=".assets/hs.svg">](Haskell/2024/05.hs "Haskell solution for 2024/05") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriioPihpjihpniiqLiiprijJUiXG5cbiIuCuKKmSjiipziipzii5XiiKniiaBAXG4sQHwuKQriipwo4pah4oqc4ouVKeKIqeKJoEBcbixALC4KCk1pbiAgICDihpAg4oqi4pa9wqziirjiiIg64p-cKOKJoeKKo-KWveKIiOKKmeKKuOKJoeKKoikKUmVtb3ZlIOKGkCDiioLiioMo4oaZfOKGmCsxKeKKouKKmuKKuD0KU29ydCAgIOKGkCDiipnii4Xil4zijaIo4oqD4oqC4ouFUmVtb3Zl4oqZ4pehTWlufOKJoDDip7vil4wpW10KTWlkICAgIOKGkCDiio_ijIrDtzLiirjip7sKCuKIqSgvK-KJoeKXh01pZOKWvSkgwqwsLOKKuOKJoeKJjSDin5zijZpTb3J04oqZwqQK "Uiua solution for 2024/05") [<img height=12 src=".assets/rb.svg">](Ruby/2024/05.rb "Ruby solution for 2024/05")|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)|
|[**9**](https://adventofcode.com/2024/day/9)|[**10**](https://adventofcode.com/2024/day/10)|[**11**](https://adventofcode.com/2024/day/11)|[**12**](https://adventofcode.com/2024/day/12)|[**13**](https://adventofcode.com/2024/day/13)|[**14**](https://adventofcode.com/2024/day/14)|[**15**](https://adventofcode.com/2024/day/15)|
|[**16**](https://adventofcode.com/2024/day/16)|[**17**](https://adventofcode.com/2024/day/17)|[**18**](https://adventofcode.com/2024/day/18)|[**19**](https://adventofcode.com/2024/day/19)|[**20**](https://adventofcode.com/2024/day/20)|[**21**](https://adventofcode.com/2024/day/21)|[**22**](https://adventofcode.com/2024/day/22)|
|[**23**](https://adventofcode.com/2024/day/23)|[**24**](https://adventofcode.com/2024/day/24)|[**25**](https://adventofcode.com/2024/day/25)|26|27|28|29|
Expand Down

0 comments on commit 179843c

Please sign in to comment.