init d1-4

This commit is contained in:
Jill 2022-12-04 09:11:45 +03:00
commit 94f3e04fbd
13 changed files with 271 additions and 0 deletions

26
.gitignore vendored Normal file
View File

@ -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

9
1-a.hs Normal file
View File

@ -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

12
1-b.hs Normal file
View File

@ -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

29
2-a.hs Normal file
View File

@ -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

39
2-b.hs Normal file
View File

@ -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

20
3-a-v2.hs Normal file
View File

@ -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

20
3-a.hs Normal file
View File

@ -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

25
3-b-v2.hs Normal file
View File

@ -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

34
3-b.hs Normal file
View File

@ -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

18
4-a.hs Normal file
View File

@ -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

18
4-b.hs Normal file
View File

@ -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

13
README.md Normal file
View File

@ -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

8
clean.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
rm ./*-a
rm ./*-b
rm ./*-a-v*
rm ./*-b-v*
rm ./*.hi
rm ./*.o