Skip to content

Instantly share code, notes, and snippets.

@freszu
Created December 7, 2022 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freszu/cd6e563b5f8d267e11537f892381adfb to your computer and use it in GitHub Desktop.
Save freszu/cd6e563b5f8d267e11537f892381adfb to your computer and use it in GitHub Desktop.
Generates list of other participants each person have to give a gift to every day.
import java.io.File
/**
* Advent calendar gift generator
*
* Generates list of other participants each person have to give a gift to every day.
*/
fun main() {
println("Pass a list of participants separated by \",\"")
val santas: List<String> = readln().split(',')
val santaToPersonAndDayList = santas.associateWith {
(1..24).toSet().shuffled()
}
.map { (santa, bags) ->
val bag = bags.toMutableSet()
val santasWithoutSanta = santas.toMutableList().apply { remove(santa) }
var currentSanta = 0
val map = mutableMapOf<Int, String>()
while (bag.isNotEmpty()) {
val santa = santasWithoutSanta[currentSanta % santasWithoutSanta.size]
val day = bag.first()
map[day] = santa
bag.remove(day)
currentSanta++
}
santa to map.toSortedMap()
}.toMap()
santaToPersonAndDayList.forEach { (santa, giftsMap) ->
File("src", "$santa.txt")
.printWriter()
.use { out ->
out.println("HoHoHo $santa! This is your gift list:")
giftsMap.forEach { (day, person) ->
out.println("$day $person")
}
out.println("-----------------------------------")
out.println("Generated with giftmachine v2.137")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment