Implement day 10, in Kotlin

This commit is contained in:
2017-12-10 17:43:43 +01:00
parent 836274331c
commit 51d1e5aed0
4 changed files with 78 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ The current plan, in no particular order:
- [ ] Coffeescript
- [ ] Haskell
- [ ] Java
- [ ] Kotlin
- [x] Kotlin - [Day 10](./day-10/solution.kt)
- [x] Lex - [Day 09](./day-09/solution.l)
- [x] Matlab - [Day 06](./day-06)
- [ ] Node.js

8
2017/day-10/Makefile Normal file
View File

@@ -0,0 +1,8 @@
.PHONY: all
all: solution.jar
java -jar $<
solution.jar: solution.kt
kotlinc $< -include-runtime -d $@

1
2017/day-10/input.txt Normal file
View File

@@ -0,0 +1 @@
206,63,255,131,65,80,238,157,254,24,133,2,16,0,1,3

68
2017/day-10/solution.kt Normal file
View File

@@ -0,0 +1,68 @@
fun valArray(): IntArray
{
val arr = IntArray(256)
for (i in arr.indices) {
arr[i] = i
}
return arr
}
fun hash(input: IntArray, values: IntArray, index: Int, skip: Int): Pair<Int, Int> {
var curIndex = index
var curSkip = skip
for (range in input) {
val halfRange = range / 2
for (i in 0..(halfRange - 1)) {
val i1 = (curIndex + i) % values.size
val i2 = (curIndex + range - i - 1) % values.size
values[i1] = values[i2].also { values[i2] = values[i1] }
}
curIndex = (curIndex + range + curSkip) % values.size
curSkip++
}
return Pair(curIndex, curSkip)
}
fun solve(input: String)
{
// Part 1
val part1input = input.split(",").map { it.toInt() }.toIntArray()
val values = valArray()
hash(part1input, values, 0, 0)
println(values[0] * values[1])
// Part 2
val part2input = ArrayList(List(input.length) { input[it].toInt()})
part2input.addAll(arrayOf(17, 31, 73, 47, 23))
val values2 = valArray()
var skip = 0
var index = 0
// Repeat hash 64 times
for (i in 1..64) {
val (newIndex, newSkip) = hash(part2input.toIntArray(), values2, index, skip)
index = newIndex
skip = newSkip
}
for (i in 0..15) {
val startIndex = 16 * i
val endIndex = startIndex + 15
val currentNum = values2.slice(startIndex..endIndex).reduce{a, b -> a.xor(b)}
print("%02x".format(currentNum))
}
println()
}
fun main(args: Array<String>)
{
solve(readLine()!!)
}