From 03929010d2e6003bcc0d8224af773752f3fbffd4 Mon Sep 17 00:00:00 2001 From: 2colours Date: Sun, 12 May 2024 21:09:39 +0200 Subject: Week 262 solutions in Groovy --- challenge-262/2colours/groovy/ch-1.groovy | 6 ++++++ challenge-262/2colours/groovy/ch-2.groovy | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 challenge-262/2colours/groovy/ch-1.groovy create mode 100644 challenge-262/2colours/groovy/ch-2.groovy diff --git a/challenge-262/2colours/groovy/ch-1.groovy b/challenge-262/2colours/groovy/ch-1.groovy new file mode 100644 index 0000000000..bd1b79407a --- /dev/null +++ b/challenge-262/2colours/groovy/ch-1.groovy @@ -0,0 +1,6 @@ +import groovy.json.JsonSlurper + +final REPLACEMENTS = ['()', '[]'] +def jsonSlurper = new JsonSlurper() +def ints = jsonSlurper.parseText(System.console().readLine('@ints = ').tr(*REPLACEMENTS)) +println ints.groupBy{ Integer.signum(it) }.findAll{ it.key.abs() > 0 }.values()*.size().max() \ No newline at end of file diff --git a/challenge-262/2colours/groovy/ch-2.groovy b/challenge-262/2colours/groovy/ch-2.groovy new file mode 100644 index 0000000000..2aa51bd176 --- /dev/null +++ b/challenge-262/2colours/groovy/ch-2.groovy @@ -0,0 +1,12 @@ +import groovy.json.JsonSlurper + +final REPLACEMENTS = ['()', '[]'] +def jsonSlurper = new JsonSlurper() +def ints = jsonSlurper.parseText(System.console().readLine('@ints = ').tr(*REPLACEMENTS)) +def k = System.console().readLine('$k = ').toInteger() +println ints.withIndex().collect { it, index -> + Integer neededFactor = k / (k as BigInteger).gcd(index as BigInteger) + Integer nextValid = Math.ceil((index + 1) / neededFactor) * neededFactor; + def possible_values = ints.drop(nextValid).collate(neededFactor)*.get(0) + possible_values.count(it) +}.sum() \ No newline at end of file -- cgit