Skip to content

Instantly share code, notes, and snippets.

View windmaomao's full-sized avatar

Fang Jin windmaomao

  • Raleigh, NC
View GitHub Profile
const move = (a, b) => [a[0]+b[0], a[1]+b[1]]
// given data build a maze matrix
const buildMaze = (data) => {
const maze = data.slice(1).split('\n')
.map(r => r.split(''))
let startPos = [0, 0], numKeys = 0
for (let i = 0; i < maze.length; i++) {
const row = maze[i]
@windmaomao
windmaomao / world_interpretation.rs
Created January 12, 2022 15:31
World interpretation rust
use std::collections::HashMap;
#[derive(Debug,Hash,PartialEq,Eq,Copy,Clone)]
struct Pos(i8, i8);
type CharMat = Vec<Vec<char>>;
type MemoHash = HashMap<String, usize>;
struct Maze {
mat: CharMat,
// aoc2015-day3-part2.kt
fun part2(s: String) = evenOddGroups(s)
.flatMap { it.scan(0, nextPos) }
.distinct()
.count()
// aoc2015-day3-evenOddGroups.kt
fun evenOddGroups(s: String) = s
.withIndex()
.groupBy { it.index % 2 }.values
.map { it.map { v -> v.value }
// aoc2015-day3-part1.kt
fun part1(s: String) = s
.scan(0, nextPos)
.distinct()
.count()
}
// aoc2015-day3-nextPos
private val len = 10000
fun nextPos(acc: Int, c: Char) = when(c) {
'^' -> acc + len
'v' -> acc - len
'>' -> acc + 1
'<' -> acc - 1
else -> acc
// aoc2015-d2-part1.kt
val paper = { l: Int, w: Int, h: Int ->
(l * w + w * h + h * l) * 2 +
l * w * h / maxOf(l, w, h)
}
fun part1(list: List<Box>) = list
.map { (l, w, h) -> paper(l, w, h) }
.sum()
// aoc2015-d2-part2.kt
val paper2 = { l: Int, w: Int, h: Int ->
val m = maxOf(l, w, h)
(l + w + h - m) * 2 + l * w * h
}
fun part2(list: List<Box>) = list
.map { (l, w, h) -> paper2(l, w, h) }
.sum()
// aoc2015-day2-part.kt
fun part(
list: List<Box>,
fn: (Int, Int, Int) -> Int
) = list
.map { (l, w, h) -> fn(l, w, h) }
.sum()
fun part1(list: List<Box>) = part(list, paper)
// aoc-2015-day2.kt
typealias Box = List<Int>
fun extractBox(s: String): Box = "\\d+".toRegex()
.findAll(s)
.map { it.value.toInt() }
.toList()
}