File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ * .txt 
Original file line number Diff line number Diff line change 1+ # Kotlin Solution  
2+ I recommend to use [ kscript] ( https://github.com/holgerbrandl/kscript )  by [ @holgerbrandl  ] ( https://github.com/holgerbrandl )  to execute the script file.
Original file line number Diff line number Diff line change 1+ import  java.nio.file.Files 
2+ import  java.nio.file.Paths 
3+ 
4+ val  instructions =  Files .readAllLines(Paths .get(" input.txt"  ))
5+  .map { it.split("  "  ) }
6+  .map { Instruction (it[0 ], it[1 ].toInt()) }
7+ 
8+ var  accumulator =  0 
9+ 
10+ var  currentInstruction =  0 
11+ 
12+ while  (currentInstruction <  instructions.size) {
13+  val  instruction =  instructions[currentInstruction]
14+  if  (instruction.executions >  0 ) {
15+  println (" accumulator is at $accumulator "  )
16+  break 
17+  }
18+  currentInstruction + =  instruction.execute()
19+ }
20+ 
21+ fun  Instruction.execute (): Int  {
22+  this .executions++ 
23+  return  when  (type) {
24+  " acc"   ->  {
25+  accumulator + =  this .value
26+  1 
27+  }
28+  " jmp"   ->  {
29+  this .value
30+  }
31+  " nop"   ->  {
32+  1 
33+  }
34+  else  ->  {
35+  error(" Invalid instruction type: $type "  )
36+  }
37+  }
38+ }
39+ 
40+ data class  Instruction (val  type :  String , val  value :  Int , var  executions :  Int  = 0 )
                                 You can’t perform that action at this time. 
               
                  
0 commit comments