#Perl 5, 22 bytes
Perl 5, 22 bytes
21 plus one for -p
:
$_=(-$_,$_+2)[$_%3]/3
Uses 1-based indexing.
Explanation:
-p
sets the variable $_
equal to the input. The code then sets it equal to the $_%3
th element, divided by 3, of the 0-based list (-$_,$_+2)
(where %
is modulo). Note that if $_%3
is two, then there is no such element, and the subsequent division by 3 numifies the undefined to 0. -p
then prints $_
.