Created
December 1, 2022 16:31
-
-
Save jacobhyphenated/fc2d3131f2f39fbfad8c7b9fd35f9f95 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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