This challenge was inspired by programming an Arduino microcontroller. I have 6 LEDs and 6 buttons connected to various pins on the board. In the code, each button and LED is assigned an ID number (1-6). Pin numbers (ranging from 0-13) corresponding to the ID numbers are looked up using a switch
statement. Purely for amusement, I was wondering if these switch
es could be circumvented with an arithmetic/other function just to horrify future code maintainers.
The challenge
Provide the function/functions that take the ID number (integer) as a parameter and return the pin number (integer) for the 6 LEDs and/or the 6 buttons, without using conditional statements (no if
, no switch
and no ternary).
Return values for LEDs:
ID Pin
1 3
2 5
3 6
4 9
5 10
6 11
Return values for buttons:
ID Pin
1 2
2 4
3 7
4 8
5 12
6 13
Bonus challenge
Provide a single function that takes an ID number (integer) and second parameter (any type) indicating whether LED or button pins are requested, and returns the corresponding pin (integer).
Rules
This is not an Arduino-specific challenge. Use any language, do whatever you want.
Edit: at the suggestion of steveverril, this is now a code golf challenge.
Good luck!
(If you're still reading: although patently absurd and arbitrary by programming standards, the mappings are based on the Arduino Micro's pinout. Pins 0 and 1 are reserved for serial communication, LEDs are assigned to the 6 lowest-numbered PWM-capable pins, buttons are assigned to remaining pins)
1+(1==1)
? \$\endgroup\$if
,switch
and ternary) are off limits. \$\endgroup\$