Skip to content

Instantly share code, notes, and snippets.

View windmaomao's full-sized avatar

Fang Jin windmaomao

  • Raleigh, NC
View GitHub Profile
@windmaomao
windmaomao / animationStrategy.js
Created May 23, 2020 13:44
In doing so, we guarantee that the animation time is independent of how often the timer callback is actually executed. If there are big delays (due to other blocking events), this method may result in dropped frames.
const animationRate = 30; // 30 ms
let initialTime, elapsedTime;
function animate(deltaT) {
// calculate object positions based on deltaT
}
function onFrame() {
const currentTime = new Date().getTime();
import java.io.File
fun parseFile(name: String): List<String> =
File("../res/$name.input").readLines()
fun String.extractNumbers(): List<Int> =
"""\d+""".toRegex()
.findAll(this)
.map { it.value.toInt() }
.toList()
@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)
}
@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-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-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()
// Map char into a int
private fun charValue(c: Char): Int = when (c) {
'(' -> 1
')' -> -1
else -> 0
}
// aoc2015-day0-model
fun extractModal(str: String): Model {
return ...
}
// aoc2015-day0-list.kt
fun extractModals(strs: List<String>) = strs
.map { Model(it) }
.toList()