diff options
| author | Archargelod <archargelod@gmail.com> | 2024-04-07 22:49:46 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-04-07 22:49:46 +0800 |
| commit | f0a59983d7ca27d1303de7ace94007491afc60a2 (patch) | |
| tree | ee7050d14b0af506b084c67490b2ae05cb921e20 /challenge-051 | |
| parent | 0495c7534995083a63370f8e93d3834bbe1f054d (diff) | |
| download | perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.tar.gz perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.tar.bz2 perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.zip | |
weeks 49-52, 263 in Nim
Diffstat (limited to 'challenge-051')
| -rw-r--r-- | challenge-051/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-051/archargelod/nim/ch_1.nim | 27 | ||||
| -rwxr-xr-x | challenge-051/archargelod/nim/ch_2.nim | 26 |
3 files changed, 54 insertions, 0 deletions
diff --git a/challenge-051/archargelod/README b/challenge-051/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-051/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-051/archargelod/nim/ch_1.nim b/challenge-051/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..eedc1e25d2 --- /dev/null +++ b/challenge-051/archargelod/nim/ch_1.nim @@ -0,0 +1,27 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[math, algorithm, bitops] + +proc findAll3sums*(numbers: openarray[int], target: int): seq[seq[int]] = + for i in 1..2^numbers.len: + if i.countSetBits != 3: continue + + var tmpResult: seq[int] + for j in 0..<numbers.len: + if i.testBit(j): + tmpResult.add numbers[j] + + if tmpResult.sum != target: continue + + tmpResult.sort() + result.add tmpResult + +when isMainModule: + import std/unittest + + const + Test = ([-25, -10, -7, -3, 2, 4, 8, 10], 0) + Expected = [@[-10, 2, 8], @[-7, -3, 10]] + + suite "3 Sum": + test "Example 1": + check findAll3sums(Test[0], Test[1]) == Expected diff --git a/challenge-051/archargelod/nim/ch_2.nim b/challenge-051/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..510ae532e3 --- /dev/null +++ b/challenge-051/archargelod/nim/ch_2.nim @@ -0,0 +1,26 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[sequtils, math] + +proc isColorFul(number: int): bool = + let digits = ($number).mapIt(it.ord - '0'.ord) + var uniq: set[int16] + + template testUniq(n: int16) = + if n in uniq: + return false + uniq.incl n + + for winLen in 1..digits.len: + for ind in 0 .. digits.len - winLen: + let product = digits.toOpenArray(ind, ind + winLen - 1).prod() + testUniq(int16 product) + + true + +proc allColorful3Digits*(): seq[int] = + for i in 100..999: + if i.isColorFul(): + result.add i + +when isMainModule: + echo "All colorful 3-digit numbers: ", allColorful3Digits() |
