Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add day 2 part 1 (Mulling)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mulling committed Dec 6, 2023
1 parent 7d2e50c commit df63229
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions content/aoc/aoc-2023-mulling.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
author: "Mulling"
title: "AoC 2023 - Mulling"
title: "AoC 2023"
date: 2023-12-04T23:25:02-03:00
description: "Haskell torture"
description: "Haskell type torture"
draft: false
---

- [Day 1](#day-1)
- [Day 2](#day-2)

# Day 1
This problem kinda stinks, plus I'm still very rusty on the ol' Haskell.
## Day 1
This problem kinda stinks, plus I'm still very rusty with the ol' Haskell.

```haskell
import Control.Applicative
Expand Down Expand Up @@ -37,3 +38,23 @@ main = interact (show . sum . map (liftA2 (\ x y -> read [x, y] :: Int) head las

It's quite funny that the final binary has 14Mb...

## Day 2
### Part 1
Overall the solution is a convoluted mess, only because "normal" strings don't have `splitOn` and we have to resort to using `Data.Text`. Still getting the hang of Applicatives again.

```haskell
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text as T

main :: IO ()
main = interact (show . sum . map (valid . ([(";"`T.splitOn`)]<*>) . T.splitOn ":" . T.pack). lines)
where
valid [[game], sets] | all (foldr (\xs acc -> acc && valid' (T.words xs)) True) ([(","`T.splitOn`)] <*> sets) = (read . last . words) (T.unpack game) :: Int
valid _ = 0
valid' [num, color] | color == "red" = parse num <= 12
| color == "green" = parse num <= 13
| color == "blue" = parse num <= 14
valid' _ = False
parse xs = read (T.unpack xs) :: Int
```

0 comments on commit df63229

Please sign in to comment.