1+ import de.ronny_h.extensions.isSymmetrical
2+
13fun main () {
24 val day = " Day14"
35
@@ -10,7 +12,7 @@ fun main() {
1012 Robot (px.toInt(), py.toInt(), vx.toInt(), vy.toInt())
1113 }
1214
13- fun List<Robot>.move (width : Int , height : Int , seconds : Int ) = map {
15+ fun List<Robot>.move (width : Int , height : Int , seconds : Long ) = map {
1416 it.copy(
1517 px = (it.px + seconds * it.vx).mod(width),
1618 py = (it.py + seconds * it.vy).mod(height),
@@ -97,6 +99,55 @@ fun main() {
9799
98100 val input = readInput(day)
99101 printAndCheck(input, ::part1, 218433348 )
102+
103+
104+ println (" $day part 2" )
105+
106+ fun List<Robot>.isNotSymmetrical (width : Int , height : Int ): Boolean {
107+ val grid = toGrid(width, height)
108+ val symmetrical = grid.all { row ->
109+ row.isSymmetrical()
110+ }
111+ if (symmetrical) {
112+ grid.print ()
113+ }
114+ return ! symmetrical
115+ }
116+
117+ fun iterateUntilSymmetrical (robots : List <Robot >, width : Int , height : Int ): Long {
118+ var seconds = 0L
119+ do {
120+ seconds++
121+ if (seconds % 10000 == 0L ) print (' .' )
122+ if (seconds % 1000000 == 0L ) println ()
123+ val result = robots.move(width, height, seconds)
124+ } while (result.isNotSymmetrical(width, height))
125+ return seconds
126+ }
127+
128+ fun part2Small (input : List <String >) = iterateUntilSymmetrical(input.parseRobots(), 11 , 7 )
129+
130+ fun part2 (input : List <String >) = iterateUntilSymmetrical(input.parseRobots(), 101 , 103 )
131+
132+ printAndCheck(
133+ """
134+ p=0,4 v=3,-3
135+ p=6,3 v=-1,-3
136+ p=10,3 v=-1,2
137+ p=2,0 v=2,-1
138+ p=0,0 v=1,3
139+ p=3,0 v=-2,-2
140+ p=7,6 v=-1,-3
141+ p=3,0 v=-1,-2
142+ p=9,3 v=2,3
143+ p=7,3 v=-1,2
144+ p=2,4 v=2,-3
145+ p=9,5 v=-3,-3
146+ """ .trimIndent().lines(),
147+ ::part2Small, 2
148+ )
149+
150+ printAndCheck(input, ::part2, 91649162 )
100151}
101152
102153data class Robot (val px : Int , val py : Int , val vx : Int , val vy : Int )
0 commit comments