From c7ae8ad49674c4f51945837d5ef96753b10f9cc5 Mon Sep 17 00:00:00 2001 From: Natty Date: Sat, 2 Dec 2023 14:42:04 +0100 Subject: [PATCH] Day 02 finished --- day02a/.gitignore | 2 + day02a/day02a.cabal | 27 +++++++++++ day02a/input.txt | 100 +++++++++++++++++++++++++++++++++++++++++ day02a/package.yaml | 33 ++++++++++++++ day02a/src/Main.hs | 36 +++++++++++++++ day02a/stack.yaml | 6 +++ day02a/stack.yaml.lock | 12 +++++ day02a/test.txt | 5 +++ day02b/.gitignore | 2 + day02b/day02b.cabal | 27 +++++++++++ day02b/input.txt | 100 +++++++++++++++++++++++++++++++++++++++++ day02b/package.yaml | 33 ++++++++++++++ day02b/src/Main.hs | 31 +++++++++++++ day02b/stack.yaml | 6 +++ day02b/stack.yaml.lock | 12 +++++ day02b/test.txt | 5 +++ 16 files changed, 437 insertions(+) create mode 100644 day02a/.gitignore create mode 100644 day02a/day02a.cabal create mode 100644 day02a/input.txt create mode 100644 day02a/package.yaml create mode 100644 day02a/src/Main.hs create mode 100644 day02a/stack.yaml create mode 100644 day02a/stack.yaml.lock create mode 100644 day02a/test.txt create mode 100644 day02b/.gitignore create mode 100644 day02b/day02b.cabal create mode 100644 day02b/input.txt create mode 100644 day02b/package.yaml create mode 100644 day02b/src/Main.hs create mode 100644 day02b/stack.yaml create mode 100644 day02b/stack.yaml.lock create mode 100644 day02b/test.txt diff --git a/day02a/.gitignore b/day02a/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/day02a/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/day02a/day02a.cabal b/day02a/day02a.cabal new file mode 100644 index 0000000..22e5d92 --- /dev/null +++ b/day02a/day02a.cabal @@ -0,0 +1,27 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.2. +-- +-- see: https://github.com/sol/hpack + +name: day02a +version: 0.1.0.0 +description: Please see the README at +author: Natty +maintainer: natty.sh.git@gmail.com +copyright: 2023 Natty +build-type: Simple + +executable day02a + main-is: Main.hs + other-modules: + Paths_day02a + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , containers + , megaparsec + , text + default-language: Haskell2010 diff --git a/day02a/input.txt b/day02a/input.txt new file mode 100644 index 0000000..7afc5dd --- /dev/null +++ b/day02a/input.txt @@ -0,0 +1,100 @@ +Game 1: 1 blue, 1 red; 10 red; 8 red, 1 blue, 1 green; 1 green, 5 blue +Game 2: 9 green, 11 red; 1 green, 7 red, 1 blue; 1 red, 1 blue, 1 green; 11 green, 3 red, 1 blue; 5 green, 12 red; 8 green, 1 blue, 7 red +Game 3: 16 blue, 2 red, 4 green; 8 red, 4 green; 7 green, 16 blue +Game 4: 3 green, 4 blue, 6 red; 7 red, 12 green, 5 blue; 5 green, 16 red, 8 blue +Game 5: 4 green, 4 blue, 3 red; 4 green, 7 red, 1 blue; 2 blue, 2 red, 4 green; 3 green, 7 red, 4 blue; 2 blue, 3 green, 8 red +Game 6: 3 green, 4 blue, 10 red; 13 red, 3 green, 4 blue; 11 red; 14 red, 1 green; 6 red, 2 green, 1 blue; 10 red, 1 blue, 1 green +Game 7: 2 green, 9 red, 9 blue; 12 red, 14 blue; 8 red, 3 green +Game 8: 9 green, 1 red; 18 green, 2 red, 7 blue; 1 blue, 9 green, 3 red; 3 red, 15 blue, 18 green +Game 9: 2 red, 1 blue, 10 green; 4 red, 1 blue, 5 green; 6 green, 3 red; 1 green, 2 blue; 8 red +Game 10: 9 green; 6 red, 4 green, 4 blue; 9 red, 2 blue, 9 green; 8 blue, 9 red, 12 green; 4 red, 8 green, 2 blue; 6 green, 7 blue +Game 11: 1 blue, 15 red, 2 green; 3 green, 9 red; 1 blue, 3 red, 1 green; 10 red, 1 green; 3 red +Game 12: 6 blue, 9 red, 4 green; 1 green, 8 blue; 4 green, 8 blue, 1 red; 7 green, 7 blue, 2 red +Game 13: 11 green, 8 blue, 1 red; 5 blue, 2 red, 5 green; 2 red, 18 green; 5 blue, 5 green, 2 red +Game 14: 1 red, 4 blue, 3 green; 2 red, 1 green, 1 blue; 1 red, 5 green, 1 blue; 3 red, 4 green, 4 blue +Game 15: 2 blue, 1 green; 3 red, 2 blue, 7 green; 1 red, 4 green, 15 blue; 1 green, 14 blue, 2 red +Game 16: 3 red, 10 blue, 9 green; 20 blue, 13 green, 1 red; 9 green, 11 blue, 6 red +Game 17: 4 red, 1 green, 1 blue; 1 green, 1 red, 11 blue; 1 red, 7 blue +Game 18: 5 red, 9 green; 17 green, 2 blue, 16 red; 11 red, 2 blue, 1 green +Game 19: 3 red, 14 green; 4 blue, 1 red, 17 green; 14 green, 3 red, 9 blue; 9 blue, 8 green, 8 red; 4 green, 8 blue +Game 20: 8 green, 3 blue, 3 red; 5 green, 6 red, 1 blue; 1 blue, 6 red, 13 green; 1 blue, 4 red; 4 red, 4 green, 1 blue +Game 21: 10 green, 3 blue, 7 red; 4 green, 14 blue, 11 red; 2 red, 8 green, 15 blue; 13 green, 7 blue, 4 red +Game 22: 3 blue, 11 green, 3 red; 7 green, 2 blue, 2 red; 3 red, 3 blue, 15 green; 12 green, 2 blue, 5 red +Game 23: 4 red, 3 blue; 2 green, 7 red, 6 blue; 1 green, 2 red, 3 blue +Game 24: 7 red, 3 green, 2 blue; 3 red, 9 green, 9 blue; 8 green, 6 red, 7 blue +Game 25: 2 red, 2 green, 2 blue; 3 green, 1 red, 4 blue; 4 red, 3 blue; 4 green, 1 blue, 5 red; 2 green, 4 red, 2 blue; 5 green, 3 red +Game 26: 1 red, 7 blue, 12 green; 2 blue, 2 red, 7 green; 3 green, 5 red, 6 blue; 8 green, 3 blue, 5 red; 15 green, 7 blue +Game 27: 4 blue, 7 red, 15 green; 4 red, 1 blue, 15 green; 3 red; 8 red, 4 blue, 10 green; 4 red +Game 28: 2 blue, 5 red, 8 green; 13 red, 9 green; 2 blue, 4 red, 5 green; 1 blue, 14 green, 12 red; 13 green, 11 red, 3 blue +Game 29: 7 green, 10 red; 9 green, 2 red, 1 blue; 3 blue, 13 green, 5 red +Game 30: 3 green, 6 blue, 5 red; 3 green, 1 red, 13 blue; 2 red, 2 green, 7 blue; 13 red, 2 green, 9 blue; 2 blue, 7 red, 2 green +Game 31: 8 red, 16 green, 1 blue; 10 red, 3 blue, 11 green; 2 green, 4 blue, 3 red; 2 blue, 7 red, 12 green; 16 green, 9 red, 6 blue; 8 blue, 9 red, 11 green +Game 32: 6 red, 3 green, 7 blue; 3 green, 7 blue, 3 red; 4 green, 1 red, 7 blue; 7 blue, 6 green, 5 red; 9 green, 11 red +Game 33: 8 green, 6 blue; 2 green, 8 red, 1 blue; 12 green, 4 blue, 14 red; 11 blue, 1 green, 7 red; 1 blue, 5 green, 9 red; 7 green, 7 red, 5 blue +Game 34: 5 blue, 6 green, 7 red; 6 blue, 5 green, 10 red; 9 red, 3 blue, 7 green; 9 red, 4 green; 15 red, 6 green, 4 blue +Game 35: 6 blue, 4 green, 7 red; 2 blue, 6 red; 4 green, 11 red, 2 blue; 2 red, 1 blue, 4 green; 10 red, 10 blue, 1 green; 11 blue, 4 green +Game 36: 13 blue, 6 green, 7 red; 5 red, 9 blue, 9 green; 6 red, 2 blue, 3 green; 2 blue, 7 green; 7 red, 5 blue; 12 blue, 7 red, 3 green +Game 37: 2 red, 12 green, 3 blue; 6 red, 4 blue; 3 blue, 1 green; 4 red, 5 blue, 6 green +Game 38: 3 red, 16 blue, 10 green; 6 red, 5 green, 5 blue; 7 blue, 12 red, 5 green +Game 39: 17 red, 4 green, 6 blue; 6 blue, 11 red; 18 red, 3 green, 1 blue; 3 blue, 6 red, 2 green; 19 red, 5 blue +Game 40: 1 red, 2 green, 3 blue; 6 blue, 5 green, 1 red; 5 green, 1 blue +Game 41: 7 blue, 6 green; 8 blue, 2 red, 3 green; 2 blue, 3 green; 3 green, 7 red, 17 blue +Game 42: 6 blue; 10 red, 5 blue, 14 green; 1 blue, 11 green; 6 blue, 2 green, 5 red; 7 blue, 13 green, 11 red +Game 43: 15 green, 3 blue; 7 blue, 14 green; 11 blue, 3 green; 17 green, 1 red, 1 blue; 17 green, 2 blue, 1 red +Game 44: 11 red, 4 blue, 16 green; 11 blue, 16 green; 14 red, 10 blue, 17 green +Game 45: 6 blue; 6 green, 6 blue, 1 red; 5 blue, 11 green; 4 green, 6 blue +Game 46: 6 green, 3 blue, 2 red; 9 green, 4 red, 2 blue; 9 green, 1 red, 3 blue +Game 47: 6 red, 3 blue, 9 green; 3 green, 11 blue; 4 blue, 1 red; 11 green, 11 blue +Game 48: 13 blue, 15 red; 9 red, 9 blue; 15 green, 9 red, 1 blue +Game 49: 8 green, 4 red; 1 red, 6 blue; 5 blue, 8 green; 1 red, 2 green, 3 blue; 2 red, 5 blue, 5 green +Game 50: 1 red, 8 green, 3 blue; 1 blue, 9 green; 3 green, 6 blue +Game 51: 8 green, 8 red, 8 blue; 9 green, 2 red, 6 blue; 1 green, 1 red, 1 blue; 5 blue, 1 red, 7 green; 7 blue, 5 red, 5 green +Game 52: 2 green, 1 red; 4 blue, 7 red, 2 green; 3 red, 4 blue; 1 green, 1 blue, 7 red; 8 red, 1 green, 8 blue; 4 blue, 3 green, 4 red +Game 53: 9 blue, 9 red; 3 blue, 3 green, 5 red; 1 green, 6 blue, 2 red; 4 red, 3 green, 3 blue +Game 54: 1 blue, 2 red, 2 green; 6 green, 1 blue, 3 red; 1 green, 1 blue +Game 55: 14 red, 8 blue; 9 red, 2 green, 8 blue; 6 blue, 11 red, 1 green; 10 red, 3 blue, 4 green; 13 red, 2 green, 4 blue; 5 green, 5 blue, 3 red +Game 56: 12 green, 3 blue; 1 red, 13 green, 5 blue; 17 blue, 6 red, 2 green; 4 red, 12 green, 12 blue +Game 57: 9 green, 9 red, 10 blue; 10 blue, 10 red, 9 green; 4 blue, 14 red, 8 green; 14 red, 7 green; 10 green, 3 blue, 7 red; 1 red, 2 blue, 1 green +Game 58: 1 green, 9 red, 11 blue; 5 green, 11 red, 6 blue; 2 green, 3 red, 8 blue; 2 green, 14 blue, 4 red; 9 blue, 1 red +Game 59: 10 blue, 13 green, 3 red; 4 red, 12 green, 8 blue; 4 red, 9 blue, 10 green; 17 green, 5 red, 7 blue; 4 red, 3 blue, 10 green +Game 60: 4 green; 5 blue, 8 red; 19 red, 3 blue, 2 green; 4 green, 14 blue, 3 red +Game 61: 2 red, 6 blue; 15 blue, 5 red; 1 green, 2 red; 1 green, 2 blue, 8 red; 7 red, 10 blue; 11 blue +Game 62: 10 blue, 2 green, 7 red; 2 red, 2 green, 10 blue; 5 red +Game 63: 19 red; 1 green, 2 blue, 3 red; 1 blue, 11 red +Game 64: 11 red, 3 green; 3 red, 1 blue, 3 green; 2 green, 2 red, 4 blue; 9 red, 1 green; 3 green, 7 red, 4 blue; 9 red, 2 blue, 3 green +Game 65: 2 red, 12 green; 3 red, 3 green; 8 green, 1 blue, 4 red; 2 red, 3 green +Game 66: 4 blue, 11 red, 15 green; 1 blue, 2 green, 10 red; 4 red, 5 green; 16 red, 16 green, 4 blue +Game 67: 6 red, 1 green; 1 green, 1 blue, 12 red; 11 red, 2 blue +Game 68: 4 green, 5 red; 9 blue, 1 red; 1 green, 2 red, 7 blue; 6 red, 5 green, 8 blue; 2 green, 7 red, 2 blue +Game 69: 9 red, 16 green, 12 blue; 5 red, 4 green, 3 blue; 6 green, 11 blue, 6 red; 13 green, 16 blue, 7 red; 1 red, 8 blue, 14 green +Game 70: 7 red, 5 blue, 4 green; 6 red, 12 blue, 13 green; 5 green, 4 red, 8 blue; 9 blue, 5 green, 2 red +Game 71: 15 red, 3 green, 3 blue; 1 red, 5 green, 6 blue; 6 green, 7 blue, 16 red +Game 72: 4 blue, 4 green, 6 red; 9 green, 2 blue; 15 green, 5 red +Game 73: 1 green, 7 red; 6 red, 2 blue; 5 red, 9 blue; 12 blue, 9 red +Game 74: 6 blue, 4 red, 4 green; 17 green, 1 blue; 4 blue, 7 green, 4 red +Game 75: 3 green, 2 blue, 10 red; 17 red, 14 green, 2 blue; 2 green, 1 blue, 14 red; 16 red, 1 blue, 9 green; 14 red, 1 blue, 9 green +Game 76: 1 red, 10 green, 4 blue; 4 blue, 4 red, 7 green; 1 red, 2 blue, 15 green; 5 blue, 7 green, 1 red; 5 blue, 3 red, 17 green; 18 green, 4 blue, 1 red +Game 77: 4 green, 5 blue, 7 red; 3 blue, 4 red, 5 green; 1 red, 1 green; 6 green, 4 blue, 16 red +Game 78: 2 blue, 9 red; 7 red, 3 blue, 18 green; 2 blue, 12 green +Game 79: 3 red, 10 green, 10 blue; 10 blue, 12 green; 7 green, 10 blue, 3 red +Game 80: 10 green, 2 blue, 14 red; 3 blue, 4 red; 12 green; 3 red, 2 blue, 1 green; 10 red, 12 green, 2 blue +Game 81: 1 green, 3 red, 6 blue; 5 blue, 1 red, 1 green; 6 blue, 9 red +Game 82: 3 blue, 11 red, 20 green; 15 red, 5 green, 3 blue; 4 red, 7 green, 3 blue +Game 83: 5 red, 12 blue, 6 green; 11 green, 9 blue, 6 red; 6 green, 1 red, 7 blue; 16 blue, 4 red; 3 green, 6 blue +Game 84: 3 red, 3 blue, 2 green; 3 red, 9 green; 2 blue, 11 green, 1 red; 1 red, 11 green, 1 blue +Game 85: 11 green, 1 red, 2 blue; 6 red, 8 green, 14 blue; 3 red, 6 green, 15 blue; 9 blue, 7 red, 1 green; 19 blue, 2 green, 5 red; 6 blue, 7 red, 6 green +Game 86: 11 red, 4 green; 2 green, 1 blue, 10 red; 11 red, 3 blue, 9 green +Game 87: 2 green, 17 blue; 12 blue, 3 red, 2 green; 3 green, 2 blue, 13 red; 6 red, 3 green; 13 blue, 11 red, 3 green; 5 blue, 7 red, 1 green +Game 88: 1 red, 12 green; 4 green, 3 blue; 6 blue, 12 green; 1 red, 2 blue +Game 89: 9 green, 2 red, 12 blue; 19 green, 12 blue, 3 red; 2 red, 8 blue, 11 green; 13 blue, 8 red, 9 green +Game 90: 2 red, 2 blue, 7 green; 7 green, 2 red, 12 blue; 6 red, 9 green, 5 blue +Game 91: 14 blue, 2 red, 7 green; 7 blue, 7 green; 1 red, 5 green, 10 blue; 4 red, 7 blue, 4 green; 5 green, 1 red, 13 blue; 5 green, 1 red +Game 92: 6 green, 4 blue, 7 red; 18 blue, 3 red; 6 red, 2 blue; 16 blue, 5 red, 3 green +Game 93: 6 red, 17 blue, 5 green; 14 red, 16 blue, 10 green; 11 blue, 7 red, 11 green; 6 blue, 7 green, 3 red; 17 blue, 1 green +Game 94: 4 green, 9 blue; 2 green, 6 red, 3 blue; 5 red, 4 green, 8 blue; 4 green, 5 red, 6 blue; 7 green, 7 red, 6 blue +Game 95: 6 blue, 2 red, 3 green; 9 green, 2 blue; 2 red, 4 blue, 11 green; 4 blue, 4 red, 8 green; 13 green, 5 red, 7 blue; 4 red, 15 green, 2 blue +Game 96: 5 red, 8 blue, 5 green; 12 blue, 2 red; 15 blue, 2 red, 1 green; 4 red, 1 green +Game 97: 5 red, 5 blue; 1 red, 11 blue; 3 green, 10 blue; 3 green, 1 red, 7 blue; 2 blue, 6 red; 1 red, 1 green, 8 blue +Game 98: 4 red, 3 blue, 8 green; 5 blue, 2 red, 8 green; 7 green, 10 red, 2 blue; 11 blue, 9 red, 9 green; 6 blue, 10 green, 11 red +Game 99: 7 green, 3 red, 9 blue; 3 blue, 5 red, 4 green; 2 red, 6 green, 1 blue; 12 blue, 1 red, 1 green; 11 green, 1 red, 12 blue +Game 100: 2 blue, 1 red; 4 blue, 2 red, 1 green; 7 red, 5 blue; 2 red, 1 green, 5 blue diff --git a/day02a/package.yaml b/day02a/package.yaml new file mode 100644 index 0000000..27f3cd0 --- /dev/null +++ b/day02a/package.yaml @@ -0,0 +1,33 @@ +name: day02a +version: 0.1.0.0 +author: "Natty" +maintainer: "natty.sh.git@gmail.com" +copyright: "2023 Natty" + +description: Please see the README at + +dependencies: +- base >= 4.7 && < 5 +- megaparsec +- containers +- text + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-export-lists +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints + +executables: + day02a: + main: Main.hs + source-dirs: src + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N \ No newline at end of file diff --git a/day02a/src/Main.hs b/day02a/src/Main.hs new file mode 100644 index 0000000..47923ff --- /dev/null +++ b/day02a/src/Main.hs @@ -0,0 +1,36 @@ +{-# LANGUAGE ImportQualifiedPost #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE ViewPatterns #-} + +module Main (main) where + +import Data.Text qualified as T +import Data.Text.Read (decimal) + +parseVal :: [T.Text] -> (Integer, String) +parseVal [decimal @Integer -> Right cnt, name] = (fst cnt, T.unpack name) +parseVal _ = error "parseVal error" + +parseVals :: T.Text -> [(Integer, String)] +parseVals vals = parseVal . T.split (== ' ') <$> T.splitOn (T.pack ", ") vals + +parseParts :: [T.Text] -> (Integer, [[(Integer, String)]]) +parseParts [T.words -> [T.unpack -> "Game", decimal @Integer -> Right d], values] = (fst d, parseVals <$> T.splitOn (T.pack "; ") values) +parseParts _ = error "parseParts error" + +mapCubes :: T.Text -> (Integer, [[(Integer, String)]]) +mapCubes line = parseParts $ T.splitOn (T.pack ": ") line + +cubeAmtIsValid :: (Integer, String) -> Bool +cubeAmtIsValid (a, "red") = a <= 12 +cubeAmtIsValid (a, "green") = a <= 13 +cubeAmtIsValid (a, "blue") = a <= 14 +cubeAmtIsValid _ = error "cubeAmtIsValid error" + +main :: IO () +main = do + lns <- lines <$> getContents + let values = mapCubes . T.pack <$> lns + let filtered = filter (all (all cubeAmtIsValid) . snd) values + + print $ sum (fst <$> filtered) diff --git a/day02a/stack.yaml b/day02a/stack.yaml new file mode 100644 index 0000000..887f541 --- /dev/null +++ b/day02a/stack.yaml @@ -0,0 +1,6 @@ +resolver: lts-21.21 + +packages: +- . + +extra-deps: [] \ No newline at end of file diff --git a/day02a/stack.yaml.lock b/day02a/stack.yaml.lock new file mode 100644 index 0000000..433950c --- /dev/null +++ b/day02a/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186 + size: 640060 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml + original: lts-21.21 diff --git a/day02a/test.txt b/day02a/test.txt new file mode 100644 index 0000000..1cd7d33 --- /dev/null +++ b/day02a/test.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green \ No newline at end of file diff --git a/day02b/.gitignore b/day02b/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/day02b/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/day02b/day02b.cabal b/day02b/day02b.cabal new file mode 100644 index 0000000..b0848de --- /dev/null +++ b/day02b/day02b.cabal @@ -0,0 +1,27 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.2. +-- +-- see: https://github.com/sol/hpack + +name: day02b +version: 0.1.0.0 +description: Please see the README at +author: Natty +maintainer: natty.sh.git@gmail.com +copyright: 2023 Natty +build-type: Simple + +executable day02b + main-is: Main.hs + other-modules: + Paths_day02b + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , containers + , megaparsec + , text + default-language: Haskell2010 diff --git a/day02b/input.txt b/day02b/input.txt new file mode 100644 index 0000000..7afc5dd --- /dev/null +++ b/day02b/input.txt @@ -0,0 +1,100 @@ +Game 1: 1 blue, 1 red; 10 red; 8 red, 1 blue, 1 green; 1 green, 5 blue +Game 2: 9 green, 11 red; 1 green, 7 red, 1 blue; 1 red, 1 blue, 1 green; 11 green, 3 red, 1 blue; 5 green, 12 red; 8 green, 1 blue, 7 red +Game 3: 16 blue, 2 red, 4 green; 8 red, 4 green; 7 green, 16 blue +Game 4: 3 green, 4 blue, 6 red; 7 red, 12 green, 5 blue; 5 green, 16 red, 8 blue +Game 5: 4 green, 4 blue, 3 red; 4 green, 7 red, 1 blue; 2 blue, 2 red, 4 green; 3 green, 7 red, 4 blue; 2 blue, 3 green, 8 red +Game 6: 3 green, 4 blue, 10 red; 13 red, 3 green, 4 blue; 11 red; 14 red, 1 green; 6 red, 2 green, 1 blue; 10 red, 1 blue, 1 green +Game 7: 2 green, 9 red, 9 blue; 12 red, 14 blue; 8 red, 3 green +Game 8: 9 green, 1 red; 18 green, 2 red, 7 blue; 1 blue, 9 green, 3 red; 3 red, 15 blue, 18 green +Game 9: 2 red, 1 blue, 10 green; 4 red, 1 blue, 5 green; 6 green, 3 red; 1 green, 2 blue; 8 red +Game 10: 9 green; 6 red, 4 green, 4 blue; 9 red, 2 blue, 9 green; 8 blue, 9 red, 12 green; 4 red, 8 green, 2 blue; 6 green, 7 blue +Game 11: 1 blue, 15 red, 2 green; 3 green, 9 red; 1 blue, 3 red, 1 green; 10 red, 1 green; 3 red +Game 12: 6 blue, 9 red, 4 green; 1 green, 8 blue; 4 green, 8 blue, 1 red; 7 green, 7 blue, 2 red +Game 13: 11 green, 8 blue, 1 red; 5 blue, 2 red, 5 green; 2 red, 18 green; 5 blue, 5 green, 2 red +Game 14: 1 red, 4 blue, 3 green; 2 red, 1 green, 1 blue; 1 red, 5 green, 1 blue; 3 red, 4 green, 4 blue +Game 15: 2 blue, 1 green; 3 red, 2 blue, 7 green; 1 red, 4 green, 15 blue; 1 green, 14 blue, 2 red +Game 16: 3 red, 10 blue, 9 green; 20 blue, 13 green, 1 red; 9 green, 11 blue, 6 red +Game 17: 4 red, 1 green, 1 blue; 1 green, 1 red, 11 blue; 1 red, 7 blue +Game 18: 5 red, 9 green; 17 green, 2 blue, 16 red; 11 red, 2 blue, 1 green +Game 19: 3 red, 14 green; 4 blue, 1 red, 17 green; 14 green, 3 red, 9 blue; 9 blue, 8 green, 8 red; 4 green, 8 blue +Game 20: 8 green, 3 blue, 3 red; 5 green, 6 red, 1 blue; 1 blue, 6 red, 13 green; 1 blue, 4 red; 4 red, 4 green, 1 blue +Game 21: 10 green, 3 blue, 7 red; 4 green, 14 blue, 11 red; 2 red, 8 green, 15 blue; 13 green, 7 blue, 4 red +Game 22: 3 blue, 11 green, 3 red; 7 green, 2 blue, 2 red; 3 red, 3 blue, 15 green; 12 green, 2 blue, 5 red +Game 23: 4 red, 3 blue; 2 green, 7 red, 6 blue; 1 green, 2 red, 3 blue +Game 24: 7 red, 3 green, 2 blue; 3 red, 9 green, 9 blue; 8 green, 6 red, 7 blue +Game 25: 2 red, 2 green, 2 blue; 3 green, 1 red, 4 blue; 4 red, 3 blue; 4 green, 1 blue, 5 red; 2 green, 4 red, 2 blue; 5 green, 3 red +Game 26: 1 red, 7 blue, 12 green; 2 blue, 2 red, 7 green; 3 green, 5 red, 6 blue; 8 green, 3 blue, 5 red; 15 green, 7 blue +Game 27: 4 blue, 7 red, 15 green; 4 red, 1 blue, 15 green; 3 red; 8 red, 4 blue, 10 green; 4 red +Game 28: 2 blue, 5 red, 8 green; 13 red, 9 green; 2 blue, 4 red, 5 green; 1 blue, 14 green, 12 red; 13 green, 11 red, 3 blue +Game 29: 7 green, 10 red; 9 green, 2 red, 1 blue; 3 blue, 13 green, 5 red +Game 30: 3 green, 6 blue, 5 red; 3 green, 1 red, 13 blue; 2 red, 2 green, 7 blue; 13 red, 2 green, 9 blue; 2 blue, 7 red, 2 green +Game 31: 8 red, 16 green, 1 blue; 10 red, 3 blue, 11 green; 2 green, 4 blue, 3 red; 2 blue, 7 red, 12 green; 16 green, 9 red, 6 blue; 8 blue, 9 red, 11 green +Game 32: 6 red, 3 green, 7 blue; 3 green, 7 blue, 3 red; 4 green, 1 red, 7 blue; 7 blue, 6 green, 5 red; 9 green, 11 red +Game 33: 8 green, 6 blue; 2 green, 8 red, 1 blue; 12 green, 4 blue, 14 red; 11 blue, 1 green, 7 red; 1 blue, 5 green, 9 red; 7 green, 7 red, 5 blue +Game 34: 5 blue, 6 green, 7 red; 6 blue, 5 green, 10 red; 9 red, 3 blue, 7 green; 9 red, 4 green; 15 red, 6 green, 4 blue +Game 35: 6 blue, 4 green, 7 red; 2 blue, 6 red; 4 green, 11 red, 2 blue; 2 red, 1 blue, 4 green; 10 red, 10 blue, 1 green; 11 blue, 4 green +Game 36: 13 blue, 6 green, 7 red; 5 red, 9 blue, 9 green; 6 red, 2 blue, 3 green; 2 blue, 7 green; 7 red, 5 blue; 12 blue, 7 red, 3 green +Game 37: 2 red, 12 green, 3 blue; 6 red, 4 blue; 3 blue, 1 green; 4 red, 5 blue, 6 green +Game 38: 3 red, 16 blue, 10 green; 6 red, 5 green, 5 blue; 7 blue, 12 red, 5 green +Game 39: 17 red, 4 green, 6 blue; 6 blue, 11 red; 18 red, 3 green, 1 blue; 3 blue, 6 red, 2 green; 19 red, 5 blue +Game 40: 1 red, 2 green, 3 blue; 6 blue, 5 green, 1 red; 5 green, 1 blue +Game 41: 7 blue, 6 green; 8 blue, 2 red, 3 green; 2 blue, 3 green; 3 green, 7 red, 17 blue +Game 42: 6 blue; 10 red, 5 blue, 14 green; 1 blue, 11 green; 6 blue, 2 green, 5 red; 7 blue, 13 green, 11 red +Game 43: 15 green, 3 blue; 7 blue, 14 green; 11 blue, 3 green; 17 green, 1 red, 1 blue; 17 green, 2 blue, 1 red +Game 44: 11 red, 4 blue, 16 green; 11 blue, 16 green; 14 red, 10 blue, 17 green +Game 45: 6 blue; 6 green, 6 blue, 1 red; 5 blue, 11 green; 4 green, 6 blue +Game 46: 6 green, 3 blue, 2 red; 9 green, 4 red, 2 blue; 9 green, 1 red, 3 blue +Game 47: 6 red, 3 blue, 9 green; 3 green, 11 blue; 4 blue, 1 red; 11 green, 11 blue +Game 48: 13 blue, 15 red; 9 red, 9 blue; 15 green, 9 red, 1 blue +Game 49: 8 green, 4 red; 1 red, 6 blue; 5 blue, 8 green; 1 red, 2 green, 3 blue; 2 red, 5 blue, 5 green +Game 50: 1 red, 8 green, 3 blue; 1 blue, 9 green; 3 green, 6 blue +Game 51: 8 green, 8 red, 8 blue; 9 green, 2 red, 6 blue; 1 green, 1 red, 1 blue; 5 blue, 1 red, 7 green; 7 blue, 5 red, 5 green +Game 52: 2 green, 1 red; 4 blue, 7 red, 2 green; 3 red, 4 blue; 1 green, 1 blue, 7 red; 8 red, 1 green, 8 blue; 4 blue, 3 green, 4 red +Game 53: 9 blue, 9 red; 3 blue, 3 green, 5 red; 1 green, 6 blue, 2 red; 4 red, 3 green, 3 blue +Game 54: 1 blue, 2 red, 2 green; 6 green, 1 blue, 3 red; 1 green, 1 blue +Game 55: 14 red, 8 blue; 9 red, 2 green, 8 blue; 6 blue, 11 red, 1 green; 10 red, 3 blue, 4 green; 13 red, 2 green, 4 blue; 5 green, 5 blue, 3 red +Game 56: 12 green, 3 blue; 1 red, 13 green, 5 blue; 17 blue, 6 red, 2 green; 4 red, 12 green, 12 blue +Game 57: 9 green, 9 red, 10 blue; 10 blue, 10 red, 9 green; 4 blue, 14 red, 8 green; 14 red, 7 green; 10 green, 3 blue, 7 red; 1 red, 2 blue, 1 green +Game 58: 1 green, 9 red, 11 blue; 5 green, 11 red, 6 blue; 2 green, 3 red, 8 blue; 2 green, 14 blue, 4 red; 9 blue, 1 red +Game 59: 10 blue, 13 green, 3 red; 4 red, 12 green, 8 blue; 4 red, 9 blue, 10 green; 17 green, 5 red, 7 blue; 4 red, 3 blue, 10 green +Game 60: 4 green; 5 blue, 8 red; 19 red, 3 blue, 2 green; 4 green, 14 blue, 3 red +Game 61: 2 red, 6 blue; 15 blue, 5 red; 1 green, 2 red; 1 green, 2 blue, 8 red; 7 red, 10 blue; 11 blue +Game 62: 10 blue, 2 green, 7 red; 2 red, 2 green, 10 blue; 5 red +Game 63: 19 red; 1 green, 2 blue, 3 red; 1 blue, 11 red +Game 64: 11 red, 3 green; 3 red, 1 blue, 3 green; 2 green, 2 red, 4 blue; 9 red, 1 green; 3 green, 7 red, 4 blue; 9 red, 2 blue, 3 green +Game 65: 2 red, 12 green; 3 red, 3 green; 8 green, 1 blue, 4 red; 2 red, 3 green +Game 66: 4 blue, 11 red, 15 green; 1 blue, 2 green, 10 red; 4 red, 5 green; 16 red, 16 green, 4 blue +Game 67: 6 red, 1 green; 1 green, 1 blue, 12 red; 11 red, 2 blue +Game 68: 4 green, 5 red; 9 blue, 1 red; 1 green, 2 red, 7 blue; 6 red, 5 green, 8 blue; 2 green, 7 red, 2 blue +Game 69: 9 red, 16 green, 12 blue; 5 red, 4 green, 3 blue; 6 green, 11 blue, 6 red; 13 green, 16 blue, 7 red; 1 red, 8 blue, 14 green +Game 70: 7 red, 5 blue, 4 green; 6 red, 12 blue, 13 green; 5 green, 4 red, 8 blue; 9 blue, 5 green, 2 red +Game 71: 15 red, 3 green, 3 blue; 1 red, 5 green, 6 blue; 6 green, 7 blue, 16 red +Game 72: 4 blue, 4 green, 6 red; 9 green, 2 blue; 15 green, 5 red +Game 73: 1 green, 7 red; 6 red, 2 blue; 5 red, 9 blue; 12 blue, 9 red +Game 74: 6 blue, 4 red, 4 green; 17 green, 1 blue; 4 blue, 7 green, 4 red +Game 75: 3 green, 2 blue, 10 red; 17 red, 14 green, 2 blue; 2 green, 1 blue, 14 red; 16 red, 1 blue, 9 green; 14 red, 1 blue, 9 green +Game 76: 1 red, 10 green, 4 blue; 4 blue, 4 red, 7 green; 1 red, 2 blue, 15 green; 5 blue, 7 green, 1 red; 5 blue, 3 red, 17 green; 18 green, 4 blue, 1 red +Game 77: 4 green, 5 blue, 7 red; 3 blue, 4 red, 5 green; 1 red, 1 green; 6 green, 4 blue, 16 red +Game 78: 2 blue, 9 red; 7 red, 3 blue, 18 green; 2 blue, 12 green +Game 79: 3 red, 10 green, 10 blue; 10 blue, 12 green; 7 green, 10 blue, 3 red +Game 80: 10 green, 2 blue, 14 red; 3 blue, 4 red; 12 green; 3 red, 2 blue, 1 green; 10 red, 12 green, 2 blue +Game 81: 1 green, 3 red, 6 blue; 5 blue, 1 red, 1 green; 6 blue, 9 red +Game 82: 3 blue, 11 red, 20 green; 15 red, 5 green, 3 blue; 4 red, 7 green, 3 blue +Game 83: 5 red, 12 blue, 6 green; 11 green, 9 blue, 6 red; 6 green, 1 red, 7 blue; 16 blue, 4 red; 3 green, 6 blue +Game 84: 3 red, 3 blue, 2 green; 3 red, 9 green; 2 blue, 11 green, 1 red; 1 red, 11 green, 1 blue +Game 85: 11 green, 1 red, 2 blue; 6 red, 8 green, 14 blue; 3 red, 6 green, 15 blue; 9 blue, 7 red, 1 green; 19 blue, 2 green, 5 red; 6 blue, 7 red, 6 green +Game 86: 11 red, 4 green; 2 green, 1 blue, 10 red; 11 red, 3 blue, 9 green +Game 87: 2 green, 17 blue; 12 blue, 3 red, 2 green; 3 green, 2 blue, 13 red; 6 red, 3 green; 13 blue, 11 red, 3 green; 5 blue, 7 red, 1 green +Game 88: 1 red, 12 green; 4 green, 3 blue; 6 blue, 12 green; 1 red, 2 blue +Game 89: 9 green, 2 red, 12 blue; 19 green, 12 blue, 3 red; 2 red, 8 blue, 11 green; 13 blue, 8 red, 9 green +Game 90: 2 red, 2 blue, 7 green; 7 green, 2 red, 12 blue; 6 red, 9 green, 5 blue +Game 91: 14 blue, 2 red, 7 green; 7 blue, 7 green; 1 red, 5 green, 10 blue; 4 red, 7 blue, 4 green; 5 green, 1 red, 13 blue; 5 green, 1 red +Game 92: 6 green, 4 blue, 7 red; 18 blue, 3 red; 6 red, 2 blue; 16 blue, 5 red, 3 green +Game 93: 6 red, 17 blue, 5 green; 14 red, 16 blue, 10 green; 11 blue, 7 red, 11 green; 6 blue, 7 green, 3 red; 17 blue, 1 green +Game 94: 4 green, 9 blue; 2 green, 6 red, 3 blue; 5 red, 4 green, 8 blue; 4 green, 5 red, 6 blue; 7 green, 7 red, 6 blue +Game 95: 6 blue, 2 red, 3 green; 9 green, 2 blue; 2 red, 4 blue, 11 green; 4 blue, 4 red, 8 green; 13 green, 5 red, 7 blue; 4 red, 15 green, 2 blue +Game 96: 5 red, 8 blue, 5 green; 12 blue, 2 red; 15 blue, 2 red, 1 green; 4 red, 1 green +Game 97: 5 red, 5 blue; 1 red, 11 blue; 3 green, 10 blue; 3 green, 1 red, 7 blue; 2 blue, 6 red; 1 red, 1 green, 8 blue +Game 98: 4 red, 3 blue, 8 green; 5 blue, 2 red, 8 green; 7 green, 10 red, 2 blue; 11 blue, 9 red, 9 green; 6 blue, 10 green, 11 red +Game 99: 7 green, 3 red, 9 blue; 3 blue, 5 red, 4 green; 2 red, 6 green, 1 blue; 12 blue, 1 red, 1 green; 11 green, 1 red, 12 blue +Game 100: 2 blue, 1 red; 4 blue, 2 red, 1 green; 7 red, 5 blue; 2 red, 1 green, 5 blue diff --git a/day02b/package.yaml b/day02b/package.yaml new file mode 100644 index 0000000..c152f53 --- /dev/null +++ b/day02b/package.yaml @@ -0,0 +1,33 @@ +name: day02b +version: 0.1.0.0 +author: "Natty" +maintainer: "natty.sh.git@gmail.com" +copyright: "2023 Natty" + +description: Please see the README at + +dependencies: +- base >= 4.7 && < 5 +- megaparsec +- containers +- text + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-export-lists +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints + +executables: + day02b: + main: Main.hs + source-dirs: src + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N \ No newline at end of file diff --git a/day02b/src/Main.hs b/day02b/src/Main.hs new file mode 100644 index 0000000..92aa8fc --- /dev/null +++ b/day02b/src/Main.hs @@ -0,0 +1,31 @@ +{-# LANGUAGE ImportQualifiedPost #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE ViewPatterns #-} + +module Main (main) where + +import Data.Text qualified as T +import Data.Text.Read (decimal) + +parseVal :: [T.Text] -> (Integer, String) +parseVal [decimal @Integer -> Right cnt, name] = (fst cnt, T.unpack name) +parseVal _ = error "parseVal error" + +parseVals :: T.Text -> [(Integer, String)] +parseVals vals = parseVal . T.split (== ' ') <$> T.splitOn (T.pack ", ") vals + +parseParts :: [T.Text] -> (Integer, [[(Integer, String)]]) +parseParts [T.words -> [T.unpack -> "Game", decimal @Integer -> Right d], values] = (fst d, parseVals <$> T.splitOn (T.pack "; ") values) +parseParts _ = error "parseParts error" + +mapCubes :: T.Text -> (Integer, [[(Integer, String)]]) +mapCubes line = parseParts $ T.splitOn (T.pack ": ") line + +mergeMaxCube :: [(Integer, String)] -> (Integer, String) -> [(Integer, String)] +mergeMaxCube acc (aa, col) = (\(a, c) -> if c == col then (max a aa, c) else (a, c)) <$> acc + +main :: IO () +main = do + lns <- lines <$> getContents + let values = mapCubes . T.pack <$> lns + print $ sum $ product . (fst <$>) . foldl mergeMaxCube [(0, "red"), (0, "green"), (0, "blue")] . concat . snd <$> values diff --git a/day02b/stack.yaml b/day02b/stack.yaml new file mode 100644 index 0000000..887f541 --- /dev/null +++ b/day02b/stack.yaml @@ -0,0 +1,6 @@ +resolver: lts-21.21 + +packages: +- . + +extra-deps: [] \ No newline at end of file diff --git a/day02b/stack.yaml.lock b/day02b/stack.yaml.lock new file mode 100644 index 0000000..433950c --- /dev/null +++ b/day02b/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186 + size: 640060 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml + original: lts-21.21 diff --git a/day02b/test.txt b/day02b/test.txt new file mode 100644 index 0000000..1cd7d33 --- /dev/null +++ b/day02b/test.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green \ No newline at end of file