diff options
Diffstat (limited to 'src/Day02.kt')
-rw-r--r-- | src/Day02.kt | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/Day02.kt b/src/Day02.kt index 401c508..2eb085a 100644 --- a/src/Day02.kt +++ b/src/Day02.kt @@ -1,19 +1,14 @@ -/** - * Given a list of correct command strings, dispatch [action] taking command name and argument on each of them. - * @param commands list of valid command strings - * @param action function taking a string (command name) and integer (argument) that gets called for each command - */ -fun dispatchCommands(commands: List<String>, action: (command: String, argument: Int) -> Unit) { - for (line in commands) { - val parts = line.split(" ") - val command = parts[0] - val argument = parts[1].toInt() - - action(command, argument) +object Day02 { + private fun dispatchCommands(commands: List<String>, action: (command: String, argument: Int) -> Unit) { + for (line in commands) { + val parts = line.split(" ") + val command = parts[0] + val argument = parts[1].toInt() + + action(command, argument) + } } -} -fun main() { fun part1(input: List<String>): Int { var horizontal = 0 var depth = 0 @@ -40,6 +35,7 @@ fun main() { horizontal += argument depth += aim * argument } + "down" -> aim += argument "up" -> aim -= argument } @@ -47,12 +43,14 @@ fun main() { return horizontal * depth } +} +fun main() { val testInput = readInputAsLines("Day02_test") - check(part1(testInput) == 150) - check(part2(testInput) == 900) + check(Day02.part1(testInput) == 150) + check(Day02.part2(testInput) == 900) val input = readInputAsLines("Day02") - println(part1(input)) - println(part2(input)) + println(Day02.part1(input)) + println(Day02.part2(input)) } |