From 94f3e04fbd4cfaecf9a3a05527076978c868d44c Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Sun, 4 Dec 2022 09:11:45 +0300 Subject: [PATCH] init d1-4 --- .gitignore | 26 ++++++++++++++++++++++++++ 1-a.hs | 9 +++++++++ 1-b.hs | 12 ++++++++++++ 2-a.hs | 29 +++++++++++++++++++++++++++++ 2-b.hs | 39 +++++++++++++++++++++++++++++++++++++++ 3-a-v2.hs | 20 ++++++++++++++++++++ 3-a.hs | 20 ++++++++++++++++++++ 3-b-v2.hs | 25 +++++++++++++++++++++++++ 3-b.hs | 34 ++++++++++++++++++++++++++++++++++ 4-a.hs | 18 ++++++++++++++++++ 4-b.hs | 18 ++++++++++++++++++ README.md | 13 +++++++++++++ clean.sh | 8 ++++++++ 13 files changed, 271 insertions(+) create mode 100644 .gitignore create mode 100644 1-a.hs create mode 100644 1-b.hs create mode 100644 2-a.hs create mode 100644 2-b.hs create mode 100644 3-a-v2.hs create mode 100644 3-a.hs create mode 100644 3-b-v2.hs create mode 100644 3-b.hs create mode 100644 4-a.hs create mode 100644 4-b.hs create mode 100644 README.md create mode 100755 clean.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7065d50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +dist +dist-* +cabal-dev +*.o +*.hi +*.hie +*.chi +*.chs.h +*.dyn_o +*.dyn_hi +.hpc +.hsenv +.cabal-sandbox/ +cabal.sandbox.config +*.prof +*.aux +*.hp +*.eventlog +.stack-work/ +cabal.project.local +cabal.project.local~ +.HTF/ +.ghc.environment.* + +# aoc +*_input \ No newline at end of file diff --git a/1-a.hs b/1-a.hs new file mode 100644 index 0000000..1bb39d6 --- /dev/null +++ b/1-a.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE OverloadedStrings #-} + +import qualified Data.Text as T + +main = interact $ + show + . maximum + . map (sum . map read . lines . T.unpack) + . T.splitOn "\n\n" . T.pack \ No newline at end of file diff --git a/1-b.hs b/1-b.hs new file mode 100644 index 0000000..152bb21 --- /dev/null +++ b/1-b.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE OverloadedStrings #-} + +import qualified Data.Text as T +import Data.List (sort) + +main = interact $ + show + . sum + . (\l -> [l!!0, l!!1, l!!2]) -- this is stupid :) + . reverse . sort + . map (sum . map read . lines . T.unpack) + . T.splitOn "\n\n" . T.pack \ No newline at end of file diff --git a/2-a.hs b/2-a.hs new file mode 100644 index 0000000..b0528ac --- /dev/null +++ b/2-a.hs @@ -0,0 +1,29 @@ +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + +scoreLose = 0 +scoreDraw = 3 +scoreWin = 6 + +resolveMatchScore 'A' 'Z' = scoreLose +resolveMatchScore 'B' 'X' = scoreLose +resolveMatchScore 'C' 'Y' = scoreLose + +resolveMatchScore 'A' 'Y' = scoreWin +resolveMatchScore 'B' 'Z' = scoreWin +resolveMatchScore 'C' 'X' = scoreWin + +resolveMatchScore 'A' 'X' = scoreDraw +resolveMatchScore 'B' 'Y' = scoreDraw +resolveMatchScore 'C' 'Z' = scoreDraw + +resolveOptionScore 'X' = 1 +resolveOptionScore 'Y' = 2 +resolveOptionScore 'Z' = 3 + +getScore a b = resolveMatchScore a b + resolveOptionScore b + +main = interact $ + show + . sum + . map ((\l -> getScore (l !! 0 !! 0) (l !! 1 !! 0)) . words) + . lines \ No newline at end of file diff --git a/2-b.hs b/2-b.hs new file mode 100644 index 0000000..7a4d888 --- /dev/null +++ b/2-b.hs @@ -0,0 +1,39 @@ +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + +scoreLose = 0 +scoreDraw = 3 +scoreWin = 6 + +getMatchScore 'X' = scoreLose +getMatchScore 'Y' = scoreDraw +getMatchScore 'Z' = scoreWin + +resolveOptionScore 'X' = 1 +resolveOptionScore 'Y' = 2 +resolveOptionScore 'Z' = 3 + +getWinningChoice 'A' = 'Y' +getWinningChoice 'B' = 'Z' +getWinningChoice 'C' = 'X' + +getLosingChoice 'A' = 'Z' +getLosingChoice 'B' = 'X' +getLosingChoice 'C' = 'Y' + +getDrawChoice 'A' = 'X' +getDrawChoice 'B' = 'Y' +getDrawChoice 'C' = 'Z' + +getCorrectChoice 'X' = getLosingChoice +getCorrectChoice 'Y' = getDrawChoice +getCorrectChoice 'Z' = getWinningChoice + +getScore a b = + resolveOptionScore correctChoice + getMatchScore b + where correctChoice = getCorrectChoice b a + +main = interact $ + show + . sum + . map ((\l -> getScore (l !! 0 !! 0) (l !! 1 !! 0)) . words) + . lines \ No newline at end of file diff --git a/3-a-v2.hs b/3-a-v2.hs new file mode 100644 index 0000000..ddbf101 --- /dev/null +++ b/3-a-v2.hs @@ -0,0 +1,20 @@ +-- i found out that intersect exists so i redid day 3 + +import Data.Char +import Data.List +import GHC.Utils.Misc + +getCharPriority c + | isLower c = fromEnum c - 96 + | isUpper c = fromEnum c - 64 + 26 + | otherwise = undefined + +findDuplicate s = only $ take half s `intersect` drop half s + where + halfFloat = fromIntegral (length s) / 2 + half = round halfFloat + +main = interact $ + show . sum + . map (getCharPriority . findDuplicate) + . lines \ No newline at end of file diff --git a/3-a.hs b/3-a.hs new file mode 100644 index 0000000..e5f15c3 --- /dev/null +++ b/3-a.hs @@ -0,0 +1,20 @@ +import Data.Char + +getCharPriority c + | isLower c = fromEnum c - 96 + | isUpper c = fromEnum c - 64 + 26 + | otherwise = undefined + +findDuplicate s = go s 0 0 + where + halfFloat = fromIntegral (length s) / 2 + half = round halfFloat + go s i1 i2 + | i1 >= half = go s 0 (i2+1) + | s!!i1 == s!!(i2 + half) = s!!i1 + | otherwise = go s (i1+1) i2 + +main = interact $ + show . sum + . map (getCharPriority . findDuplicate) + . lines \ No newline at end of file diff --git a/3-b-v2.hs b/3-b-v2.hs new file mode 100644 index 0000000..41e8be0 --- /dev/null +++ b/3-b-v2.hs @@ -0,0 +1,25 @@ +-- i found out that intersect exists so i redid day 3 + +import Data.Char +import Data.List +import GHC.Utils.Misc + +getCharPriority c + | isLower c = fromEnum c - 96 + | isUpper c = fromEnum c - 64 + 26 + | otherwise = undefined + +findDuplicate :: [String] -> Char +findDuplicate [l1, l2, l3] = only $ l1 `intersect` l2 `intersect` l3 +findDuplicate _ = undefined + +extractIntoGroups :: Int -> [String] -> [[String]] +extractIntoGroups s l + | s > length l = [] + | otherwise = take s l : extractIntoGroups s (drop s l) + +main = interact $ + show . sum + . map (getCharPriority . findDuplicate) + . extractIntoGroups 3 + . lines \ No newline at end of file diff --git a/3-b.hs b/3-b.hs new file mode 100644 index 0000000..30b2ce0 --- /dev/null +++ b/3-b.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE DataKinds #-} +import Data.Char +import Data.Maybe + +getCharPriority c + | isLower c = fromEnum c - 96 + | isUpper c = fromEnum c - 64 + 26 + | otherwise = undefined + +allChars :: [Char] +allChars = map toEnum $ [65..90] ++ [96..122] + +-- this is STUPID +unwrapJust (Just a) = a +unwrapJust Nothing = undefined + +findFirstJust :: [Maybe a] -> a +findFirstJust a = unwrapJust $ head $ filter isJust a + +findDuplicate :: [String] -> Char +findDuplicate [l1, l2, l3] = findFirstJust $ map (\c -> + if c`elem`l1 && c`elem`l2 && c`elem`l3 then Just c else Nothing) allChars +findDuplicate _ = undefined + +extractIntoGroups :: Int -> [String] -> [[String]] +extractIntoGroups s l + | s > length l = [] + | otherwise = take s l : extractIntoGroups s (drop s l) + +main = interact $ + show . sum + . map (getCharPriority . findDuplicate) + . extractIntoGroups 3 + . lines \ No newline at end of file diff --git a/4-a.hs b/4-a.hs new file mode 100644 index 0000000..3b48c4b --- /dev/null +++ b/4-a.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + +import qualified Data.Text as T +import Data.List + +isContained :: [[Int]] -> Bool +isContained [a, b] = intersection == a || intersection == b + where intersection = a `intersect` b + +toRange :: [Int] -> [Int] +toRange [a, b] = [a .. b] + +main = interact $ + show + . length . filter id + . map (isContained . map (toRange . map (read . T.unpack) . T.splitOn "-") . T.splitOn "," . T.pack) + . lines \ No newline at end of file diff --git a/4-b.hs b/4-b.hs new file mode 100644 index 0000000..778b3a6 --- /dev/null +++ b/4-b.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + +import qualified Data.Text as T +import Data.List +import GHC.Utils.Misc (count) + +listHasOverlap :: (Eq a) => [a] -> [a] -> Bool +listHasOverlap a = not . null . intersect a + +toRange :: [Int] -> [Int] +toRange [a, b] = [a .. b] + +main = interact $ + show + . count ((\[a,b] -> listHasOverlap a b) + . map (toRange . map (read . T.unpack) . T.splitOn "-") . T.splitOn "," . T.pack) + . lines \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a1c5f1 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# aoc2022 + +entirely in haskell :) + +## how do i run this + +you shouldn't; but for documentation purposes it's + +```bash +$ ghc 1-a.hs && cat 1_input | ./1-a +``` + +you'll need to retrieve `1_input` from your aoc \ No newline at end of file diff --git a/clean.sh b/clean.sh new file mode 100755 index 0000000..6d81284 --- /dev/null +++ b/clean.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +rm ./*-a +rm ./*-b +rm ./*-a-v* +rm ./*-b-v* +rm ./*.hi +rm ./*.o