1+ class  Day15  : Application  {
2+  var  input:  MutableList <MutableList <Char >> =  mutableListOf ()
3+  val  directions =  listOf (Pair (- 1 , 0 ), Pair (0 , 1 ), Pair (1 , 0 ), Pair (0 , - 1 ))
4+ 5+  override  fun  run (fileName :  String ): Pair <Long , Long > {
6+  val  input =  readInput(fileName)
7+  val  emptyListIdx =  input.indexOf(" " 
8+  val  inputMap =  input.subList(0 , emptyListIdx)
9+  inputMap.println ()
10+  val  steps =  input.subList(emptyListIdx +  1 , input.size).fold(StringBuilder ()) { acc, s ->  acc.append(s) }
11+  .toString().toList().map {
12+  when  (it) {
13+  ' ^' ->  0 
14+  ' >' ->  1 
15+  ' v' ->  2 
16+  else  ->  3 
17+  }
18+  }
19+  val  initialCoord =  getInitialCoord(inputMap)
20+  val  res1 =  this .part1(inputMap, steps, initialCoord)
21+  val  res2 =  this .part2()
22+  return  res1 to res2
23+  }
24+ 25+  private  fun  part1 (inputMap :  List <String >, steps :  List <Int >, initialCoord :  Pair <Int , Int >): Long  {
26+  var  res =  0L 
27+  var  coord =  initialCoord
28+  input =  inputMap.map { it.toMutableList() }.toMutableList()
29+  steps.forEach { n -> 
30+  coord =  computeStep(directions[n], coord)
31+  }
32+  input.forEachIndexed {i, list -> 
33+  list.forEachIndexed { idx, c -> 
34+  if  (c ==  ' O' 
35+  res + =  100  *  i +  idx
36+  }
37+  }
38+  }
39+  return  res
40+  }
41+ 42+  private  fun  part2 (): Long  {
43+  return  0 
44+  }
45+ 46+  private  fun  getInitialCoord (inputMap :  List <String >): Pair <Int , Int > {
47+  inputMap.forEachIndexed { i, s -> 
48+  s.forEachIndexed { j, c -> 
49+  if  (c ==  ' @' return  Pair (i, j)
50+  }
51+  }
52+  return  Pair (0 , 0 )
53+  }
54+ 55+  private  fun  computeStep (dir :  Pair <Int , Int >, coord :  Pair <Int , Int >): Pair <Int , Int > {
56+  val  isNextCoordEmpty =  try  {
57+  input[coord.first +  dir.first][coord.second +  dir.second] ==  ' .' 
58+  } catch  (e:  IndexOutOfBoundsException ) {
59+  return  coord
60+  }
61+  if  (isNextCoordEmpty) {
62+  input[coord.first][coord.second] =  ' .' 
63+  input[coord.first +  dir.first][coord.second +  dir.second] =  ' @' 
64+  return  Pair (coord.first +  dir.first, coord.second +  dir.second)
65+  } else  {
66+  if  (input[coord.first +  dir.first][coord.second +  dir.second] ==  ' #' return  coord
67+  val  distToEmptyCoord =  distToEmptyCoord(dir, coord)
68+  if  (distToEmptyCoord ==  - 1 ) return  coord
69+  else  {
70+  input[coord.first +  dir.first *  distToEmptyCoord][coord.second +  dir.second *  distToEmptyCoord] =  ' O' 
71+  input[coord.plus(dir).first][coord.plus(dir).second] =  ' @' 
72+  input[coord.first][coord.second] =  ' .' 
73+  return  coord.plus(dir)
74+  }
75+  }
76+  }
77+ 78+  private  fun  distToEmptyCoord (dir :  Pair <Int , Int >, coord :  Pair <Int , Int >): Int  {
79+  var  res =  0 
80+  var  continueSearch =  true 
81+  var  coords =  coord
82+  while (continueSearch) {
83+  coords =  coords.plus(dir)
84+  try  {
85+  if  (input[coords.first][coords.second] ==  ' .' return  ++ res
86+  if  (input[coords.first][coords.second] ==  ' #' return  - 1 
87+  if  (input[coords.first][coords.second] ==  ' O' ++ 
88+  } catch  (e:  IndexOutOfBoundsException ) {
89+  return  - 1 
90+  }
91+  }
92+  return  - 1 
93+  }
94+ }
0 commit comments