Imagine an arsonist walking around the town and picking its victims according to a very specific pattern (Or, alternatively, Imagine a bee flying around the garden and picking its flowers to pollenize according to a very specific pattern). Let's say the town is an N × N matrix, where N is an integer higher than or equal to 2. The arsonist starts from the upper-left corner and successively sets the house M spots in front of them (where M is the number of the house they're currently in), while changing the direction it's moving in after each fire, in the order East ⟶ South ⟶ West ⟶ North ⟶ East ⟶ South... and so on. The lullaby of the arsonist is the value of M that makes them exit the town (i.e. the last house they visit before stopping the abomination). This is way easier to understand with an example. Take the following matrix for instance:
3 2 3 2 7 3 1 4 1 6 2 5 3 1 1 4 4 3 2 4 1 1 1 1 1
- We start in the upper-left corner, so M = 3 (
X
marks the current and previous positions of the arsonist):X 2 3 2 7 3 1 4 1 6 2 5 3 1 1 4 4 3 2 4 1 1 1 1 1
- According to the known order, it first goes east M (3) spots and lands on a 2 so M changes accordingly:
X 2 3 X 7 3 1 4 1 6 2 5 3 1 1 4 4 3 2 4 1 1 1 1 1
- Then it goes south 2 spots and M is now 1:
X 2 3 X 7 3 1 4 1 6 2 5 3 X 1 4 4 3 2 4 1 1 1 1 1
- Now it moves 1 spot to the West and M becomes 3:
X 2 3 X 7 3 1 4 1 6 2 5 X X 1 4 4 3 2 4 1 1 1 1 1
- After it moves 3 spots north, it exits the town! Therefore, 3 is this arsonist's lullaby:
X X 2 3 X 7 3 1 4 1 6 2 5 X X 1 4 4 3 2 4 1 1 1 1 1
Given an N × N matrix (you can optionally take N as input too), find the lullaby of the arsonist. I've written a program with which you can generate more test cases and visualise the path of the arsonist: Try it online!
- You may assume that the arsonist does have a lullaby (that is, it can actually get out of the matrix).
- The matrix will only contain positive integers less than or equal to 9 (digits), for simplicity. Solutions that handle any positive integer are completely welcome.
- Note that the arsonist can land on a spot they've already burned, in case the sense they're moving in is different from the first time. In such a scenario, just take the value of that element and move again as usual.
- You can compete in any programming language and can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest submission (in bytes) for every language wins.
Test cases
------------- 9 2 3 1 7 2 8 7 6 Lullaby: 9 ------------- 2 1 2 1 3 1 1 2 1 2 2 1 1 1 1 3 Lullaby: 2 ------------- 3 2 3 2 7 3 1 4 1 6 2 5 3 1 1 4 4 3 2 4 1 1 1 1 1 Lullaby: 3 ------------- 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 Lullaby: 2 ------------- 3 2 1 2 1 1 1 2 3 2 3 2 1 1 2 1 1 1 3 1 2 3 1 1 1 1 1 1 4 5 2 3 1 1 1 1 2 1 2 1 2 2 1 2 2 3 2 1 2 Lullaby: 3 -------------
The matrices in a different format:
[[9, 2, 3], [1, 7, 2], [8, 7, 6]] [[2, 1, 2, 1], [3, 1, 1, 2], [1, 2, 2, 1], [1, 1, 1, 3]] [[3, 2, 3, 2, 7], [3, 1, 4, 1, 6], [2, 5, 3, 1, 1], [4, 4, 3, 2, 4], [1, 1, 1, 1, 1]] [[1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2]] [[3, 2, 1, 2, 1, 1, 1], [2, 3, 2, 3, 2, 1, 1], [2, 1, 1, 1, 3, 1, 2], [3, 1, 1, 1, 1, 1, 1], [4, 5, 2, 3, 1, 1, 1], [1, 2, 1, 2, 1, 2, 2], [1, 2, 2, 3, 2, 1, 2]]
The fifth test case is very interesting to visualise.