Haskell/2024/03: add solution
This commit is contained in:
parent
6acf420027
commit
0aba27c744
5 changed files with 38 additions and 3 deletions
33
Haskell/2024/03.hs
Normal file
33
Haskell/2024/03.hs
Normal file
|
@ -0,0 +1,33 @@
|
|||
import Lib
|
||||
import Text.Regex.TDFA
|
||||
|
||||
type Input = String
|
||||
|
||||
main :: IO ()
|
||||
main = aoc 2024 3 id solve1 solve2 ["1", "2"]
|
||||
|
||||
solve1 :: Input -> Int
|
||||
solve1 = sum . map handle . matches
|
||||
where
|
||||
handle :: Match -> Int
|
||||
handle (Mul a b) = a * b
|
||||
handle _ = 0
|
||||
|
||||
solve2 :: Input -> Int
|
||||
solve2 = fst . foldl handle (0, True) . matches
|
||||
where
|
||||
handle :: (Int, Bool) -> Match -> (Int, Bool)
|
||||
handle (sum, True) (Mul a b) = (sum + a * b, True)
|
||||
handle (sum, False) (Mul _ _) = (sum, False)
|
||||
handle (sum, _) Enable = (sum, True)
|
||||
handle (sum, _) Disable = (sum, False)
|
||||
|
||||
data Match = Mul Int Int | Enable | Disable deriving (Show)
|
||||
|
||||
matches :: String -> [Match]
|
||||
matches = map parseMatch . (=~ "mul\\(([0-9]+),([0-9]+)\\)|do(n't)?\\(\\)")
|
||||
|
||||
parseMatch :: [String] -> Match
|
||||
parseMatch ("do()" : _) = Enable
|
||||
parseMatch ("don't()" : _) = Disable
|
||||
parseMatch (_ : a : b : _) = Mul (read a) (read b)
|
|
@ -9,11 +9,11 @@
|
|||
|[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of ≥255548)|
|
||||
|[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of ≥197402)|
|
||||
|
||||
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 3/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 3/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 2/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 2/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): 3/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 3/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 3/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 2/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-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKSDiiaEvLeKXqzIK4oipLyviiaHil4fiioMoL-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/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4)|[**5**](https://adventofcode.com/2024/day/5)|[**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-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKSDiiaEvLeKXqzIK4oipLyviiaHil4fiioMoL-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")|[**4**](https://adventofcode.com/2024/day/4)|[**5**](https://adventofcode.com/2024/day/5)|[**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|
|
||||
|
|
|
@ -31,4 +31,4 @@ fn part2(input: &Input) -> i64 {
|
|||
out
|
||||
}
|
||||
|
||||
aoc::main!(2024, 3, ex: 1[a], 2[b]);
|
||||
aoc::main!(2024, 3, ex: 1, 2);
|
||||
|
|
1
examples/2024/3/1.2
Normal file
1
examples/2024/3/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
161
|
1
examples/2024/3/2.1
Normal file
1
examples/2024/3/2.1
Normal file
|
@ -0,0 +1 @@
|
|||
161
|
Loading…
Add table
Add a link
Reference in a new issue