26
2
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.
1
This is like a generalisation of Skip like a Rabbit in two dimensions, with a slightly different goal. The thematic of this challenge and its title were inspired by a song by Hozier
– Mr. Xcoder – 2018-05-09T18:23:26.407what happens when the arsonist lands on a square that is already burnt? – Level River St – 2018-05-09T19:11:05.770
@LevelRiverSt It just burns it again and takes the value of M as the value of that spot. This scenario is only possible if the arsonist is going in a different direction (sense, actually), because otherwise it would be impossible to exit the matrix (which you are guaranteed to happen). This is covered by the fourth test case, hence the The fourth test case is very interesting to visualise. I've addressed your concerns in the challenge body. Thanks for spotting that.
– Mr. Xcoder – 2018-05-09T19:13:16.2532Can we assume that the arsonist is not actually an arsonist and is instead doing something nice at each location rather than burning it down? +1 for a very nice idea :) – ElPedro – 2018-05-09T19:56:55.940
2@ElPedro Sure, an alternate version for you: Imagine a bee flying around the garden and picking its flowers to pollenize according to a very specific pattern. :D Happy friendly golfing! – Mr. Xcoder – 2018-05-09T20:05:20.703
1That's a much nicer thought. If I could upvote again I would for that. – ElPedro – 2018-05-09T20:06:10.097