aboutsummaryrefslogtreecommitdiff
path: root/challenge-039/archargelod/nim
diff options
context:
space:
mode:
authorArchargelod <archargelod@gmail.com>2024-03-08 19:02:07 +0800
committerArchargelod <archargelod@gmail.com>2024-03-08 19:02:07 +0800
commitc45f2d8a7ae3836921747c832d2bfe9db7cc9aa9 (patch)
treecea6fb0f51440f00210ab68d018e334c92bc7ef5 /challenge-039/archargelod/nim
parent510484bb98b0d2413f8c09c0a827e91b992d2533 (diff)
downloadperlweeklychallenge-club-c45f2d8a7ae3836921747c832d2bfe9db7cc9aa9.tar.gz
perlweeklychallenge-club-c45f2d8a7ae3836921747c832d2bfe9db7cc9aa9.tar.bz2
perlweeklychallenge-club-c45f2d8a7ae3836921747c832d2bfe9db7cc9aa9.zip
weeks 27-40, 259 in Nim
Diffstat (limited to 'challenge-039/archargelod/nim')
-rwxr-xr-xchallenge-039/archargelod/nim/ch_1.nim47
-rwxr-xr-xchallenge-039/archargelod/nim/ch_2.nim54
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-039/archargelod/nim/ch_1.nim b/challenge-039/archargelod/nim/ch_1.nim
new file mode 100755
index 0000000000..5b52e3268f
--- /dev/null
+++ b/challenge-039/archargelod/nim/ch_1.nim
@@ -0,0 +1,47 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/[strutils, times, algorithm]
+type
+ TimeRange = HSlice[Time, Time]
+
+proc parseGuestBook(input: string): seq[TimeRange] =
+ for line in input.splitLines():
+ let fields = line.splitWhitespace()
+ let (inTime, outTime) = (fields[3], fields[5])
+
+ result.add parseTime(inTime, "HH:mm", utc())..parseTime(outTime, "HH:mm", utc())
+
+proc mergeOverlaps[T](input: openarray[HSlice[T,T]]): seq[HSlice[T,T]] =
+ let input = input.sortedByIt(it.a)
+ result.add input[0]
+ for range in input[1..^1]:
+ if range.a <= result[^1].b:
+ result[^1].b = range.b
+ else:
+ result.add range
+
+proc guestHouseLightDuration*(guestBook: string): Duration =
+ var timeIntervals = guestBook.parseGuestBook().mergeOverlaps()
+
+ for interval in timeIntervals:
+ result += interval.b - interval.a
+
+when isMainModule:
+ import std/unittest
+
+ const
+ Test = """
+1) Alex IN: 09:10 OUT: 09:45
+2) Arnold IN: 09:15 OUT: 09:33
+3) Bob IN: 09:22 OUT: 09:55
+4) Charlie IN: 09:25 OUT: 10:05
+5) Steve IN: 09:33 OUT: 10:01
+6) Roger IN: 09:44 OUT: 10:12
+7) David IN: 09:57 OUT: 10:23
+8) Neil IN: 10:01 OUT: 10:19
+9) Chris IN: 10:10 OUT: 11:00"""
+ Expected = initDuration(minutes = 110)
+
+ suite "Guest house lights":
+ test "Example 1":
+ check guestHouseLightDuration(Test) == Expected
+
diff --git a/challenge-039/archargelod/nim/ch_2.nim b/challenge-039/archargelod/nim/ch_2.nim
new file mode 100755
index 0000000000..60aa0d2ba7
--- /dev/null
+++ b/challenge-039/archargelod/nim/ch_2.nim
@@ -0,0 +1,54 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/strutils
+
+proc popExcept[T](stack: var seq[T]): T =
+ if stack.len < 1:
+ raise newException(ValueError, "Stack is Empty")
+ stack.pop()
+
+proc evaluateRPN*(expression: string): float =
+ var stack: seq[float]
+ for token in expression.splitWhitespace():
+ if token in ["+", "-", "*", "/"]:
+ let val2 = stack.popExcept()
+ let val1 = stack.popExcept()
+ case token
+ of "+": stack.add val1 + val2
+ of "-": stack.add val1 - val2
+ of "*": stack.add val1 * val2
+ of "/": stack.add val1 / val2
+ else:
+ stack.add parseFloat(token)
+
+ if stack.len == 1:
+ stack[0]
+ else:
+ raise newException(ValueError, "Invalid expression")
+
+when isMainModule:
+ import std/unittest
+
+ suite "Reverse Polish Notation":
+ test "Basic Subtraction":
+ check evaluateRPN("3 2 -") == 1.0
+
+ test "Multiple Operations":
+ check evaluateRPN("3 4 + 2 *") == 14.0
+
+ test "Division":
+ check evaluateRPN("10 2 /") == 5.0
+
+ test "Float Operations":
+ check evaluateRPN("3.5 2.5 +") == 6.0
+
+ test "Empty Stack Error":
+ expect ValueError:
+ discard evaluateRPN("2 +")
+
+ test "Invalid Expression Error":
+ expect ValueError:
+ discard evaluateRPN("2 3 + 4")
+
+ test "Invalid Float Literal Error":
+ expect ValueError:
+ discard evaluateRPN("10.5.3 2 +")