aoc2023

advent of code 2023
Log | Files | Refs | Feed | README

Commit: cb2fb93130d8e170a1432877aaa849f0564f2c1a
Parent: 25202c5465a20a2c9f80404ccfb505f9a69040dc
Author: Randy Palamar
Date:   Fri,  1 Dec 2023 21:33:54 -0700

day 1

Most of the time was spent reminding myself how to do anything at
all in Haskell. Being a purely functional language makes it very
different from anything I'm normally writing. My prior knowledge
is coming back though and I remembered one of my favourite
features of Haskell: Curried Functions.

The second task almost tripped me up since I didn't notice at
first that there were terms like `oneight`. My new haskell brain
solution was to replace the `one` as `o1e` and the `eight` as
`e8t` so that further replacements would still apply. This applies
to all digits that start or end in letters that end or start [sic]
other digits. I'm sure there is a better way but I'm notyet
experienced enough with Haskell to do it.

No performance numbers so far. This was too fast to get repeatable
results. Execution time was on the order of 650us.

Diffstat:
A.gitignore | 4++++
MREADME.md | 14+++++++-------
Aday1/day1.hs | 49+++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,4 @@ +**/day[0-9] +**/day[0-9][0-9] +*.hi +*.o diff --git a/README.md b/README.md @@ -1,17 +1,17 @@ # Advent of Code 2023 -The repo where I will dump all my solutions to AOC 2023. +Here is where I will dump all my solutions to AOC 2023. ## Language(s) This year I plan on completing all the tasks in Haskell. Going -into to I would say I'm only slightly beyond "Hello World!" level -though I did write an ultra slow JSON like parser at one point. +into it I would say I'm only slightly beyond "Hello World!". But I +did write an ultra slow JSON like parser at one point. -I also plan on completing some tasks in C as well. I want to test -if I can write something in Haskell that will outperform a trivial -solution in C. My prior experience has taught me otherwise but I'm -hoping to be surprised. +I also plan on completing some tasks in both Haskell and C. I want +to find out if I can write something in Haskell that will +outperform a trivial solution in C. My prior experience has taught +me otherwise but I'm hoping to be surprised. ## Documentation diff --git a/day1/day1.hs b/day1/day1.hs @@ -0,0 +1,49 @@ +-- AOC 2023: Day 1 Part 1 +-- Given a list of newline separated strings containing ASCII digits +-- interspersed among other characters take the first and last ASCII +-- digit from each line as a 2 digit number then sum all of them together +-- +-- Example: +-- treb7uchet -> 77 +-- 1abc2 -> 12 +-- (Output) 89 +-- +import Data.Char +import qualified Data.Text as T +import qualified Data.Text.IO as TI + +stripalpha :: T.Text -> T.Text +stripalpha = T.filter (`elem` ['0' .. '9']) + +tocode :: T.Text -> Int +tocode st = + let h = T.head st + t = T.last st + z = ord '0' + in 10 * (ord h - z) + (ord t - z) + +replmap :: (String, String) -> (T.Text -> T.Text) +replmap (f, s) = T.replace (T.pack f) (T.pack s) + +repstrnums :: T.Text -> T.Text +repstrnums = + let pairs = + [ ("one", "o1e") + , ("two", "t2o") + , ("three", "t3e") + , ("four", "4") + , ("five", "5e") + , ("six", "6") + , ("seven", "7n") + , ("eight", "e8t") + , ("nine", "n9e") + ] + in foldr1 (.) $ map replmap pairs + +getcal :: [T.Text] -> [Int] +getcal = map $ tocode . stripalpha + +main = do + contents <- TI.getContents + print $ sum $ getcal $ T.lines contents + print $ sum $ getcal $ T.lines $ repstrnums contents