-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day14.kt
51 lines (41 loc) · 1.51 KB
/
Day14.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2018 by Todd Ginsberg
*/
/**
* Advent of Code 2018, Day 14 - Chocolate Charts
*
* Problem Description: http://adventofcode.com/2018/day/14
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2018/day14/
*/
package com.ginsberg.advent2018
class Day14(stoppingPoint: String) {
private val stopInt = stoppingPoint.toInt()
private val stopList = stoppingPoint.map { it.toString().toInt() }.toList()
fun solvePart1(): String =
recipes { it.size == stopInt + 10 }.takeLast(10).joinToString("")
fun solvePart2(): Int =
recipes { it.endsWith(stopList) }.size - stopList.size
private fun recipes(stopCondition: (List<Int>) -> Boolean): List<Int> {
val history = mutableListOf(3, 7)
var elf1 = 0
var elf2 = 1
var stop = false
while (!stop) {
val nextValue = history[elf1] + history[elf2]
nextValue.asDigits().forEach {
if (!stop) {
history.add(it)
stop = stopCondition(history)
}
}
elf1 = (elf1 + history[elf1] + 1) % history.size
elf2 = (elf2 + history[elf2] + 1) % history.size
}
return history
}
private fun Int.asDigits(): List<Int> =
this.toString().map { it.toString().toInt() }
private fun List<Int>.endsWith(other: List<Int>): Boolean =
if (this.size < other.size) false
else this.slice(this.size - other.size until this.size) == other
}