This commit is contained in:
Samae 2023-12-23 23:49:47 +02:00
commit 955e33330f
9 changed files with 1175 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
result*
*.cabal

9
Main.hs Normal file
View file

@ -0,0 +1,9 @@
module Main where
import Day1
main :: IO ()
main = do
putStrLn "AoC 2023"
putStrLn "Day 1"
Day1.main

25
flake.lock Normal file
View file

@ -0,0 +1,25 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1703134684,
"narHash": "sha256-SQmng1EnBFLzS7WSRyPM9HgmZP2kLJcPAz+Ug/nug6o=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d6863cbcbbb80e71cecfc03356db1cda38919523",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

37
flake.nix Normal file
View file

@ -0,0 +1,37 @@
{
description = "A Hello World for AoC";
inputs.nixpkgs.url = "nixpkgs";
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
nixpkgsFor = forAllSystems (system: import nixpkgs {
inherit system;
overlays = [ self.overlay ];
});
in
{
overlay = (final: prev: {
aoc23 = final.haskellPackages.callCabal2nix "aoc23" ./. {};
});
packages = forAllSystems (system: {
aoc23 = nixpkgsFor.${system}.aoc23;
});
defaultPackage = forAllSystems (system: self.packages.${system}.aoc23);
checks = self.packages;
devShell = forAllSystems (system: let haskellPackages = nixpkgsFor.${system}.haskellPackages;
in haskellPackages.shellFor {
packages = p: [self.packages.${system}.aoc23];
withHoogle = true;
buildInputs = with haskellPackages; [
haskell-language-server
ghcid
cabal-install
];
# Change the prompt to show that you are in a devShell
shellHook = ''
hpack
'';
});
};
}

4
inputs/day1-test.input Normal file
View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
inputs/day1-test2.input Normal file
View file

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

1000
inputs/day1.input Normal file

File diff suppressed because it is too large Load diff

23
package.yaml Normal file
View file

@ -0,0 +1,23 @@
name: aoc23
ghc-options: -Wall -threaded
default-extensions:
- OverloadedStrings
dependencies:
- base == 4.*
- attoparsec
- bytestring
executables:
aoc23:
main: Main.hs
dependencies:
- aoc23
library:
source-dirs: src
exposed-modules:
- Day1

68
src/Day1.hs Normal file
View file

@ -0,0 +1,68 @@
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unused-do-bind #-}
module Day1 where
import Data.Semigroup
import Data.Char
import Data.String
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as Char8
import Data.Attoparsec.Lazy
import Data.Attoparsec.ByteString.Char8 (letter_ascii)
import Data.Either (fromRight)
-- import Debug.Trace
main :: IO ()
main = do
day1input <- lines <$> readFile "inputs/day1.input"
putStrLn "Part 1 result:"
print $ part1 day1input
putStrLn "Part 2 result:"
print $ part2 day1input
part1 :: [String] -> Sum Int
part1 = foldMap go
where
go :: String -> Sum Int
go = glueFirstLast . map (\c -> read [c]) . filter isNumber
part2 :: [String] -> Sum Int
part2 = foldMap (glueFirstLast . getNumbers)
glueFirstLast :: [Int] -> Sum Int
glueFirstLast xs = Sum $ 10 * head xs + last xs
getNumbers :: String -> [Int]
getNumbers = fromRight [] . parseOnly numberParser . fromString
numberParser :: Parser [Int]
numberParser = do
s <- concat <$> (parseN `sepBy` letter_ascii)
pure $ concatMap toInt s
where
parseN = many' . choice $ map string
["oneight","threeight","fiveight","sevenine","nineight","eightwo","eighthree","twone"
,"one","two","three","four","five","six","seven","eight","nine"
, "1","2","3","4","5","6","7","8","9"
]
toInt :: B.ByteString -> [Int]
toInt "one" = [1]
toInt "two" = [2]
toInt "three" = [3]
toInt "four" = [4]
toInt "five" = [5]
toInt "six" = [6]
toInt "seven" = [7]
toInt "eight" = [8]
toInt "nine" = [9]
toInt "oneight" = [1,8]
toInt "threeight" = [3,8]
toInt "fiveight" = [5,8]
toInt "sevenine" = [7,9]
toInt "nineight" = [9,8]
toInt "eighthree" = [8,3]
toInt "eightwo" = [8,2]
toInt "twone" = [2,1]
toInt ss = [read $ Char8.unpack ss]