Found some Go code on r/AdventOfCode for 2023, Day 1 Part 2. The OP shared their [code](https://topaz.github.io/paste/#XQAAAQAGBgAAAAAAAAA4GEiZzRd1JAgz+whYRQxSFI7XvmlfhtGDinguAj8sFyd/fs4fcP3B6Imi/tlRkpJxoxv/JfWeshkESBsqCweXJ14ZzjRg52O41RvGQIOhxLkp/BCu2M2adZj+bPkAo8IGWqjk1Y8lc7AqvTHS58+vpdcw4IeF35zwLzk7aZGY+xqTLCq1+XBwtLF/J4qswpdaaTEBaUiH9WjlzsgHcBN5YN1iUPpjFP3DKV+HPP+/r21vlrOsg7HLJ5hb+JUpK6h5PmhD6BOFwHAmpCtewGSNq+AFlGgjPFvlWwh/DTD6/vLnmY6NNWBwnnNIII+BF4n2e8Kz1BvEJQqTJIpiYlXYCHpBXn4LsWPbyoKcuxRGY7Sd+Q7Sj1mgEycunk+X+wITzC4K3Syyx8b2IYxgxv+k7ctOcYfzKgQK7yrtfhcdxtnJPeFDLKCXK6Ui/oXqZr1flOMkmr4ixUlQtViB3iAHLSpnR2+DCzxtcysb6WsOPdyiPHFBZGFsYhrdDX41Dkfz7KdHev01O/oJglxlFkmrmrkqXm4B16F8/Yy99F5tYlF02glg16U9JZfR9WUZzeYrB+VhEPfYsml6RpOr52R5EMlDCMfslcCEIhhuQLvTb+GrwRRKK2kQHucOPmzanqetF2czvw9WnEVpF/alFzOp8Ty82tWJeUH3Jq1F2sdQPfCbUWx7OU2nMOd3TR+T3Fq9QY26qFBeNwc7GCxQU4oVQVy3IEYHSt+EIzkbNDnerYy
/* | |
MIT License | |
Copyright (c) 2024 Junilu Lacar | |
Full license text: https://spdx.org/licenses/MIT.html | |
*/ | |
private fun plural(value: String, singular: String, plural: String) = | |
when { |
/** | |
* Calculate permutations of elements in a List | |
*/ | |
// Extension properties | |
val <T> List<T>.head: T | |
get() = first() | |
val <T> List<T>.tail: List<T> |
This is just one way Kotlin improves on Java.
In Java, you can use String.format()
, like so:
System.out.println("".format("Hello, %s!", "world"));
This works because the static format()
method can be called using a String reference which is why we start the expression with an empty string, "". The format string needs to be passed in as the first argument and there needs to be enough subsequent arguments to match all the format placeholders.
In Kotlin, it's much simpler and more object-oriented. You can call format on a String and the string itself will be treated as the format string. You only need to pass in enough arguments to match all the format placeholders.
This is about a little pitfall I stumbled upon recently while trying to use regular expressions in Kotlin to split strings. It is relevant to interoperability between Pattern.split()
Kotlin's CharSequence.split()
.
Pattern
is part of the Standard Java Library and is documented in the Standard Java API Specifications.- Kotlin's
CharSequence
,String
, andRegex
classes have asplit()
method that is documented in the Kotlin Standard Library documentation. Everything that applies toCharSequence.split()
also applies toRegex.split()
andString.split()
. Pattern.split()
uses the limit parameter differently from howCharSequence.split()
uses it. Despite having the same default limit of0
, their results can be different when trailing empty strings are involved.- `Pattern.
import java.util.*; | |
import java.util.function.*; | |
/** | |
* Sample program to show use of Predicates and composing Predicates, | |
* capturing variables in lambdas expressions, and using enums to | |
* implement the Strategy pattern. | |
* | |
* Inspired by this discussion on CodeRanch.com: | |
* https://coderanch.com/t/729390/engineering/Design-pattern-good-command-pattern |
// Example of Map.associateBy() in Kotlin | |
// Map vowels to their positions in the alphabet | |
val vowels = "aeiou".toCharArray() // ['a', 'e', 'i', 'o', 'u'] | |
// Map<Char, Int> = {a=1, e=5, i=9, o=15, u=21} | |
val positions = vowels.associateBy({ it }, { it.toInt() - 'a'.toInt() + 1 }) | |
val letter = readLine()!! | |
println(positions[letter.first().toLowerCase()] ?: 0) // default == 0 if not a vowel |
This is about the Google foo.bar Gearing Up for Destruction challenge - 2nd of 2 challenges on Level 2. It was pretty interesting and I got through most of it without looking for answers. I did get stuck on 2 failing tests so I finally caved. I didn't copy my solution though, just got an insight from the solution that @CBarraford posted here: https://gist.github.com/1lann/be45311db1bd8cbbe6650b0a3e9d1977, which was a more brute-force trial-and-error solution.
Here's the math I worked out for this problem:
Given:
n pegs, numbered from 1 to n
Let:
d(n) - the distance between peg[n] and peg[n+1]
Spock and Geb (pronounced /jeb/), are relatively new Groovy-based frameworks for writing Behavior-Driven Development (BDD) style test specifications. They allow you to write very concise and expressive test code.
The example code below is from gebish.org:
class LoginSpec extends GebSpec {
def "login to admin section"() {
given: