[Question]
  [
This challenge is from a game called [Keep Talking And Nobody Explodes.](http://www.keeptalkinggame.com/)
*Memory is a fragile thing but so is everything else when a bomb goes off, so pay attention!* – from the [manual](https://ktane.timwi.de/HTML/Memory.html)
# The Objective
A number from 1–4 is given as the "display", along with scrambled numbers 1–4 as the "buttons."
The output must be the label of the correct "button." Repeat this process 5 times.
# The Format
The input format must be:
```
<display #> <buttons #>
```
Repeated 5 times. As long as the numbers are distinguishable and the `<display #>` is dedicated separately, the input format and type don't matter. For any input that doesn't fit this format, the entire challenge falls in *don't care* situation.
The output format and type don't matter as long as the numbers are distinguishable.
# Which button to press
Pressing a button means to output the label of the button.
The buttons are indexed from left to right, and are 1-indexed.
**For the 1st line:**
If the display is 1, press the button in the second position.  
If the display is 2, press the button in the second position.  
If the display is 3, press the button in the third position.  
If the display is 4, press the button in the fourth position.
**For the 2nd line:**
If the display is 1, press the button labeled “4”.  
If the display is 2, press the button in the same position as you pressed in line #1.  
If the display is 3, press the button in the first position.  
If the display is 4, press the button in the same position as you pressed in line #1.
**For the 3rd line:**
If the display is 1, press the button with the same label you pressed in line #2.  
If the display is 2, press the button with the same label you pressed in line #1.  
If the display is 3, press the button in the third position.  
If the display is 4, press the button labeled “4”.
**For the 4th line:**
If the display is 1, press the button in the same position as you pressed in line #1.  
If the display is 2, press the button in the first position.  
If the display is 3, press the button in the same position as you pressed in line #2.  
If the display is 4, press the button in the same position as you pressed in line #2.
**For the last line:**
If the display is 1, press the button with the same label you pressed in line #1.  
If the display is 2, press the button with the same label you pressed in line #2.  
If the display is 3, press the button with the same label you pressed in line #4.  
If the display is 4, press the button with the same label you pressed in line #3.
# An Example
Given the following input:
```
2 1432
4 3214
2 4213
1 4231
1 3214
```
For the 1st line, press the button in the second position, so output `4`.  
For the 2nd line, press the button in the same position as you pressed in line #1, so output `2`.  
For the 3rd line, press the button with the same label you pressed in line #1, so output `4`.  
For the 4th line, press the button in the same position as you pressed in line #1, so output `2`.  
For the last line, press the button with the same label you pressed in line #1, so output `4`.
So the entire output is `42424`, in any format and type.
  
[Answer]
# JavaScript (ES6), ~~109 106 105~~ 104 bytes
Takes input as a list of pairs `[display, "buttons"]`. Returns a list of pressed buttons.
```
a=>a.map(([d,b],i)=>a[i]=b[a[~i]=(n='_5567.848106.80990132'[i*4+d])>3?n>7?a[7-n]:n-4:b.search(a[n]||4)])
```
[Try it online!](https://tio.run/##VcvdCoIwGMbx865idNJWOtxHacL0Ql5GvH6VUVtodCTdupmE0NEDP57/FV/Yl137eIbOV/XYmBFNhvyOD0qhCgobtGwCaK0pAOE9LXVmc9rvDzFPdCKiA0@i4zESSm6g3epdZVmmcpfFOUIcOpu6UKcF72vsygtFcHYYNLNsLL3r/a3mN3@mDYUVIUBkQNZCK7kmNphBT6Ck0At8H1oKtYCYQYk/@CUry9j4AQ "JavaScript (Node.js) – Try It Online")
### How?
The action for each line and each displayed value is encoded as a single character as follows (with 0-indexed lines and positions):
* \$n=0\$ to \$3\$: press the button with the same label that we pressed in line \$n\$
* \$n=4\$ to \$7\$: press the button in position \$n-4\$
* \$n=8\$ or \$9\$: press the button in the same position as we pressed in line \$n-8\$
* `"."`: press the button labeled "\$4\$"
Translating the rules described in the challenge into these codes leads to the groups `5567`, `.848`, `106.`, `8099` and `0132`, which are concatenated into a single lookup string. The purpose of the leading `"_"` is just to deal with the 1-indexed input.
The input array \$a[\:]\$ is re-used to store both the labels that are pressed and their positions. For each line \$i\$, we store the label in \$a[i]\$ and its position in \$a[-i-1]\$.
### Commented
```
a =>                              // a[] = input
  a.map(([d, b],                  // for each pair of displayed value d and buttons b
                 i) =>            // at position i in a[]:
    a[i] = b[                     //   save the label in a[i]
      a[~i] =                     //     save its position in a[-i-1]
        ( n =                     //       n = action code taken from ...
          '_5567.848106.80990132' //           ... our lookup string ...
          [i * 4 + d]             //           ... at position i * 4 + d
        ) > 3 ?                   //       if n is greater than 3:
          n > 7 ?                 //         if n is greater than 7:
            a[7 - n]              //           re-use the position used at line n - 8,
                                  //           stored in a[-(n-8)-1], i.e. a[7-n]
          :                       //         else:
            n - 4                 //           use the position n - 4
        :                         //       else:
          b.search(a[n]           //         look for the label used at line n
                        || 4)     //         or look for '4' if a[n] is undefined,
                                  //         which happens only when n is '.'
    ]                             //   end of button lookup
  )                               // end of map()
```
[Answer]
# [Python 2](https://docs.python.org/2/), 415 375 351 317 299 273 265 bytes
```
a=[0]*5;x=input()
for i,z in enumerate(x):j,q=z;exec'a[i]'+['=[q[j-1],j]',';k=a[0][1];a[i]=[4,q.index(4)+1]if j<2else[q[k-1],k]if j-3else[q[0],1]','=[4if j>3else a[j<2][0]if j<3else q[0]]','=[q[a[j>1][1]-1]]if j-2else[q[0]]','=a[6>>4-j&3];print[l[0]for l in a]'][i]
```
[Try it online!](https://tio.run/##PU9LboMwEN33FF7V0AxVDKSLUHOR0Sys1lEN1AFCJJrL0xlA2fl9/ab/m36uMV8WZ/FIb6dqtiH29ylJXy7XUQV4qBCVj/dfP7rJJ3N6bmCwj8rP/ks7DKQPqC0O2GSGoCENumqt4zI0VInBYgnDe4jffk7K9GAoXFTzmfvu5jnWSqxduazYuSOBkSJOCl@vvHLIKWJxzW@ceDfngKzXRn7lxq0vf/atHocfdV1mzWtBVT@GOGHHkpzZyZGONPHcZUHMQaGBEgrIiYD3K@QnMCVQ1HKFhUCzw4JH7/Bppn8 "Python 2 – Try It Online")
-40 bytes by making everything ternary  
-24, -18, -10 by removing unnecessary code  
-16 by using (label,position) rather than (position,label)  
-4, -4 thanks to @Arnauld  
-26, -8 thanks to @Chas Brown
Not quite as competitive as @Arnauld's solution... probably a bit naive as its basically just logic. 
Very happy with how this progressed, thanks everyone for the suggestions!
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 74 bytes
```
Ｓθ≔∨⊖Ｓ¹η⊞υ§θηＳθ≔§⟦⌕θ4η⁰η⟧⊖Ｓζ⊞υ§θζ⊞υ§⁺⮌υ⟦§Ｓ²4⟧⊖Ｓ⊞υ§Ｓ§⟦η⁰ζζ⟧⊖ＳＳθ⊞υ§υ⌕1243Ｓ↑υ
```
[Try it online!](https://tio.run/##fVBNi8IwEL37K4aeJhBh83FaT4IseFIUT8WDtMEWulnNR5H@@ThVXHYlSj6YvHl5b2aq5uCqn0OX0tKeYtgG19ojntlsMve@PVpcOVyYyplvY4Op8S@LMQ6CTkPsdfQNRg7zsLS1ueB5hAl/ofqglV@trUdyoYtRicMH3XsOrz2JNuQNB5bB1130uDG9cd5gpM/lI/NPloNktyr27K15xuFJ57e1ezMD7ff95Mb07EHhbVKFkFoVHDJVURzwc3fiENksJaGVBAlKCg20pFD00lIJEHdQpGnfXQE "Charcoal – Try It Online") Link is to verbose version of code. Takes input as `buttons#1 display#1 buttons#2 ... display#5`. Explanation:
```
Ｓθ
```
Input the first buttons.
```
≔∨⊖Ｓ¹η
```
Decrement the first display to make it 0-indexed, but if that results in zero, increment it again. Save the result for steps 2 and 4.
```
⊞υ§θη
```
Save the appropriately indexed button in the predefined empty list.
```
Ｓθ
```
Input the second buttons.
```
≔§⟦⌕θ4η⁰η⟧⊖Ｓζ
```
Make a list of four elements: the index of "4" in the buttons; the index of the first button; `0` (representing the button in first position); the index of the first button. Get the element corresponding to the (decremented) second display. Save the result for step 4.
```
⊞υ§θζ
```
Add the appropriately indexed button to the list.
```
⊞υ§⁺⮌υ⟦§Ｓ²4⟧⊖Ｓ
```
Input the third buttons and make a list of four elements: the two buttons so far in reverse order, the button at index 2 (i.e. third position), and `4`. Index this using the (decremented) third display. Add the result to the list.
```
⊞υ§Ｓ§⟦η⁰ζζ⟧⊖Ｓ
```
Input the fourth buttons, and index them from a list of four elements: the position used for the first button, `0` (representing the first button), and the position used for the second button (twice), first indexed using the (decremented) fourth display. Add the result to the list.
```
Ｓθ
```
Input the fifth buttons.
```
⊞υ§υ⌕1243Ｓ
```
Input the fifth display, find its index into the string `1243`, and index that into the list so far, adding the result to the list.
```
↑υ
```
Output the result.
]