Created
December 7, 2022 18:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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