aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2023/01/solve.effekt46
1 files changed, 46 insertions, 0 deletions
diff --git a/2023/01/solve.effekt b/2023/01/solve.effekt
new file mode 100644
index 0000000..616ec8f
--- /dev/null
+++ b/2023/01/solve.effekt
@@ -0,0 +1,46 @@
+// I tried to use many effects :)
+
+import io/error
+import exception
+import char
+import stream
+
+def collectLine(): Unit / {read[Char], emit[Int], stop} = {
+ val c = do read[Char]()
+ if (c == '\n') do stop()
+
+ with default[WrongFormat, Unit] { collectLine() }
+ val number = digitValue(c)
+ do emit(number)
+ collectLine()
+}
+
+def solve(): Unit / {read[Char], emit[Int], stop} = {
+ val line: List[Int] = collectList {
+ with exhaustively
+ collectLine()
+ }
+
+ val sum = line match {
+ case Cons(head, Nil()) => head * 10 + head
+ case Cons(head, ls) => {
+ with default[MissingValue, Int] { 0 }
+ head * 10 + ls.last
+ }
+ case Nil() => do stop()
+ }
+
+ do emit(sum)
+}
+
+def main() = {
+ with on[IOError].panic
+ with readFile("input")
+ with decodeUTF8
+
+ // part 1
+ println(sum {
+ with exhaustively
+ solve()
+ })
+} \ No newline at end of file