Skip to content

Instantly share code, notes, and snippets.

@jacobhyphenated
Created December 1, 2022 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobhyphenated/fc2d3131f2f39fbfad8c7b9fd35f9f95 to your computer and use it in GitHub Desktop.
Save jacobhyphenated/fc2d3131f2f39fbfad8c7b9fd35f9f95 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 1
// Use this "Day" Interface to facilitate running the code. This will be re-used for each day's problem.
interface Day<T> {
fun getInput(): T
fun part1(input: T): Number
fun part2(input: T): Number
fun run() {
val input = getInput()
var start = System.currentTimeMillis()
println("Part 1: ${part1(input)} (${System.currentTimeMillis() - start}ms)")
start = System.currentTimeMillis()
println("Part 2: ${part2(input)} (${System.currentTimeMillis() - start}ms)")
}
}
class Day1: Day<List<List<Int>>> {
override fun getInput(): List<List<Int>> {
return this.javaClass.classLoader.getResource("day1/input.txt")!!
.readText()
.split("\n\n")
.map {
it.lines()
.map { num -> num.toInt() }
}
}
override fun part1(input: List<List<Int>>): Number {
return input.maxOf { it.sum() }
}
override fun part2(input: List<List<Int>>): Number {
return input.map { it.sum() }
.sortedDescending()
.subList(0,3)
.sum()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment