vbe_elvis

2021 Day2 - rewrite

Dec 2nd, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. @Test
  2. fun `Part 1 and 2`() {
  3. val submarine = Submarine()
  4. data.split("\n")
  5. .forEach { submarine.dive(it) }
  6. println("Part 1: " + submarine.part1())
  7. println("Part 2: " + submarine.part2())
  8. }
  9.  
  10. private class Submarine {
  11. private var aim = 0
  12. private var position = 0
  13. private var depth = 0
  14.  
  15. fun dive(instruction: String) {
  16. val (command, value) = instruction.split(" ")
  17. when(command) {
  18. "forward" -> {
  19. position += value.toInt()
  20. depth += aim * value.toInt()
  21. }
  22. "up" -> aim -= value.toInt()
  23. "down" -> aim += value.toInt()
  24. }
  25. }
  26.  
  27. fun part1() = aim * position
  28. fun part2() = depth * position
  29. }
Advertisement
Add Comment
Please, Sign In to add comment