Skip to content

Instantly share code, notes, and snippets.

View windmaomao's full-sized avatar

Fang Jin windmaomao

  • Raleigh, NC
View GitHub Profile
// aoc2015-day0-test
@Test fun day01Part1Example() {
val part1 = { s: String ->
d.part1(d.extractModels(s))
}
assertEquals(0, part1("(())"))
assertEquals(0, part1("()()"))
}
// aoc2015-d0-part1
fun part1(list: List<Model>) = list.
.map {}
...
.sum()
}
// aoc2015-day0-list.kt
fun extractModals(strs: List<String>) = strs
.map { Model(it) }
.toList()
// aoc2015-day0-model
fun extractModal(str: String): Model {
return ...
}
// Map char into a int
private fun charValue(c: Char): Int = when (c) {
'(' -> 1
')' -> -1
else -> 0
}
@windmaomao
windmaomao / aoc2015-d1-ops.kt
Last active September 18, 2020 17:38
Extract operations from a string
// Extract operation list from a string
fun extractOps(s: String): List<Int> = s
.map { charValue(it) }
.toList()
@windmaomao
windmaomao / aoc2015-d1-part2.kt
Last active September 18, 2020 17:36
Day 1: Not Quite Lisp, Part 2
// Day 1: Not Quite Lisp, Part 2
fun part2(ops: List<Int>): Int = ops.asSequence()
.scan(0) { acc, v -> acc + v }
.indexOf(-1)
}
@windmaomao
windmaomao / aoc2015-d1-part1.kt
Last active September 18, 2020 17:35
Day 1: Not Quite Lisp, Part 1
// Aoc 2015 Day 1, Not Quite Lisp, Part 1
fun part1(ops: List<Int>) = ops.sum()
@windmaomao
windmaomao / aoc2015-d1-generator.kt
Last active September 5, 2020 14:24
Generator to the rescue
fun part2s(s: String): Int {
val all = s.asSequence()
scan(0) { acc, c -> acc + charValue(c) }
return all.indexOf(-1)
}
fun String.extractNumbers(): List<Int> =
"""\d+""".toRegex()
.findAll(this)
.map { it.value.toInt() }
.toList()