diff --git a/2017/README.md b/2017/README.md index 6e00cac..71ac997 100644 --- a/2017/README.md +++ b/2017/README.md @@ -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 diff --git a/2017/day-10/Makefile b/2017/day-10/Makefile new file mode 100644 index 0000000..ea3240b --- /dev/null +++ b/2017/day-10/Makefile @@ -0,0 +1,8 @@ +.PHONY: all + +all: solution.jar + java -jar $< + +solution.jar: solution.kt + kotlinc $< -include-runtime -d $@ + diff --git a/2017/day-10/input.txt b/2017/day-10/input.txt new file mode 100644 index 0000000..8fe1895 --- /dev/null +++ b/2017/day-10/input.txt @@ -0,0 +1 @@ +206,63,255,131,65,80,238,157,254,24,133,2,16,0,1,3 diff --git a/2017/day-10/solution.kt b/2017/day-10/solution.kt new file mode 100644 index 0000000..12a1244 --- /dev/null +++ b/2017/day-10/solution.kt @@ -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 { + 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) +{ + solve(readLine()!!) +} \ No newline at end of file