summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas <andi@stillalive.games>2022-12-15 18:31:10 +0100
committerAndreas <andi@stillalive.games>2022-12-15 18:31:10 +0100
commita6eb2133c974c6f9880ed78c3a2b8521249081e6 (patch)
tree43a85dda4c82325c00bdc59a3e8429571834d3f4
parent952c486fe2bf7011ac38601b1c3eccdda23cc9d0 (diff)
Day 7: Unfinished
-rw-r--r--Day7/CHANGELOG.md5
-rw-r--r--Day7/Day7.cabal34
-rw-r--r--Day7/app/Main.hs60
3 files changed, 99 insertions, 0 deletions
diff --git a/Day7/CHANGELOG.md b/Day7/CHANGELOG.md
new file mode 100644
index 0000000..0c04a7d
--- /dev/null
+++ b/Day7/CHANGELOG.md
@@ -0,0 +1,5 @@
+# Revision history for Day7
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/Day7/Day7.cabal b/Day7/Day7.cabal
new file mode 100644
index 0000000..bd15eb3
--- /dev/null
+++ b/Day7/Day7.cabal
@@ -0,0 +1,34 @@
+cabal-version: 2.4
+name: Day7
+version: 0.1.0.0
+
+-- A short (one-line) description of the package.
+-- synopsis:
+
+-- A longer description of the package.
+-- description:
+
+-- A URL where users can report bugs.
+-- bug-reports:
+
+-- The license under which the package is released.
+-- license:
+author: Andreas
+maintainer: andi@stillalive.games
+
+-- A copyright notice.
+-- copyright:
+-- category:
+extra-source-files: CHANGELOG.md
+
+executable Day7
+ main-is: Main.hs
+
+ -- Modules included in this executable, other than Main.
+ -- other-modules:
+
+ -- LANGUAGE extensions used by modules in this package.
+ -- other-extensions:
+ build-depends: base ^>=4.14.3.0
+ hs-source-dirs: app
+ default-language: Haskell2010
diff --git a/Day7/app/Main.hs b/Day7/app/Main.hs
new file mode 100644
index 0000000..8956791
--- /dev/null
+++ b/Day7/app/Main.hs
@@ -0,0 +1,60 @@
+module Main (main) where
+
+import System.Environment ( getArgs )
+
+main :: IO ()
+main = getArgs >>= readFile . head >>= print . solveDay7
+
+solveDay7 :: String -> String
+solveDay7 = formatSolutions . solveDay7Parts . parseDay7Input
+ where formatSolutions (x,y) = show x ++ ", " ++ show y
+
+data File = File String Int
+data Directory = Directory String [Directory] [File]
+
+data Command = CdRoot | CdUp | Cd String | Ls String
+
+newtype Path = Path [String] -- Just a list of directory names. Root is LAST element.
+
+pushDirectory :: Path -> String -> Path
+pushDirectory (Path p) = Path . (:p)
+
+popDirectory :: Path -> (Path, String)
+popDirectory (Path (p:ps)) = (Path ps, p)
+popDirectory _ = error "Cannot pop a directory from an empty path."
+
+parseDay7Input :: String -> Directory
+parseDay7Input = buildDirectoryFromCommands . parseCommands
+
+parseCommands :: String -> [Command]
+parseCommands = map parseCommand . splitAtDollarSign
+
+splitAtDollarSign :: String -> [String]
+splitAtDollarSign [] = []
+splitAtDollarSign ('$':' ':bs) = []:splitAtDollarSign bs
+splitAtDollarSign (b:bs) = prependToFirst (splitAtDollarSign bs) b
+ where prependToFirst (l:ls) b = (b:l):ls
+ prependToFirst [] b = [[b]]
+
+parseCommand :: String -> Command
+parseCommand ('c':'d':' ':'/':_) = CdRoot
+parseCommand ('c':'d':' ':'.':'.':_) = CdUp
+parseCommand ('c':'d':' ':path) = Cd $ stripTrailingNewline path
+parseCommand ('l':'s':'\n':files) = Ls $ stripTrailingNewline files
+parseCommand e = error ("Invalid command: " ++ e)
+
+stripTrailingNewline :: String -> String
+stripTrailingNewline ['\n'] = []
+stripTrailingNewline [] = []
+stripTrailingNewline (a:as) = a:stripTrailingNewline as
+
+buildDirectoryFromCommands :: [Command] -> Directory
+buildDirectoryFromCommands = error "unimplemented"
+
+applyCommandToDirectory :: (Path, Directory) -> Command -> (Path, Directory)
+applyCommandToDirectory (oldPath, oldDir) CdRoot = (Path [], oldDir)
+applyCommandToDirectory (oldPath, oldDir) CdUp = (fst . popDirectory $ oldPath, oldDir)
+-- todo: other arms
+
+solveDay7Parts :: Directory -> (Int, Int)
+solveDay7Parts = error "unimplemented" \ No newline at end of file