diff options
| author | Archargelod <archargelod@gmail.com> | 2024-03-31 21:26:06 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-03-31 21:26:06 +0800 |
| commit | 7e07268564c8b39d4908e8123a662cd13355bb7f (patch) | |
| tree | 8ae6473e7e3c7cf0e0c17d48af123b66eced9d3f /challenge-048 | |
| parent | fdeee3349654c610d6ed2f4aca69ef8985e3c094 (diff) | |
| download | perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.tar.gz perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.tar.bz2 perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.zip | |
weeks 48, 262 in Nim
Diffstat (limited to 'challenge-048')
| -rw-r--r-- | challenge-048/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-048/archargelod/nim/ch_1.nim | 19 | ||||
| -rwxr-xr-x | challenge-048/archargelod/nim/ch_2.nim | 33 |
3 files changed, 53 insertions, 0 deletions
diff --git a/challenge-048/archargelod/README b/challenge-048/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-048/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-048/archargelod/nim/ch_1.nim b/challenge-048/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..f04fe841ab --- /dev/null +++ b/challenge-048/archargelod/nim/ch_1.nim @@ -0,0 +1,19 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/bitops + +proc highestOneBit(n: int): int = + if n == 0: return 0 + 1 shl (sizeof(n) * 8 - countLeadingZeroBits(n) - 1) + +proc josephusSurvivor(n: int): int = + not(highestOneBit(n*2)) and ((n shl 1) or 1) +when isMainModule: + import std/unittest + + const + Test = 50 + Expected = 37 + + suite "Survivor": + test "50 people, k = 2": + check josephusSurvivor(Test) == Expected diff --git a/challenge-048/archargelod/nim/ch_2.nim b/challenge-048/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..f7eddd4baf --- /dev/null +++ b/challenge-048/archargelod/nim/ch_2.nim @@ -0,0 +1,33 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[times, strformat] + +proc asInt(c: char): int = + assert c in '0'..'9' + c.ord - '0'.ord + +proc palindromeDates(yearRange: Slice[int]) = + assert yearRange.a <= yearRange.b + assert yearRange.b < 10000 + + for year in yearRange: + let yearString = &"{year:04}" + let day = yearString[^3].asInt * 10 + yearString[^4].asInt + if day notin 1..31: continue + + let month = yearString[^1].asInt * 10 + yearString[^2].asInt + if month notin 1..12: continue + + let formatted = $month & $day & yearString + + try: + let date = formatted.parse("MMddyyyy") + echo "There is a palindrome date in year ", $year, ": ", formatted, + " or ", date.format("MMM-dd-yyyy") + except TimeParseError: + discard + +proc main = + palindromeDates(2000 .. 2999) + +when isMainModule: + main() |
