Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Test
- fun `Part 1 and 2`() {
- val submarine = Submarine()
- data.split("\n")
- .forEach { submarine.dive(it) }
- println("Part 1: " + submarine.part1())
- println("Part 2: " + submarine.part2())
- }
- private class Submarine {
- private var aim = 0
- private var position = 0
- private var depth = 0
- fun dive(instruction: String) {
- val (command, value) = instruction.split(" ")
- when(command) {
- "forward" -> {
- position += value.toInt()
- depth += aim * value.toInt()
- }
- "up" -> aim -= value.toInt()
- "down" -> aim += value.toInt()
- }
- }
- fun part1() = aim * position
- fun part2() = depth * position
- }
Advertisement
Add Comment
Please, Sign In to add comment