#!/bin/env perl use strict; use warnings; use List::Util qw(sum0); use List::MoreUtils qw(uniq); { my %buttons = ( '0' => 16, '1' => 8, '2' => 4, '3' => 2, '4' => 1, ); sub buttonsPressed { my ($cab, @pressed) = @_; my @values = map { 0 + $buttons{$_} } uniq @pressed; return sprintf "", $cab, 128 + sum0 @values; # Yes, these two lines could be combined } } my $cab = 1108; my @tests = ( [ 0 ], [ 1 ], [ 2 ], [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], [ 0, 1, 2 ], ); for my $test ( @tests ) { print "@{$test} : ", buttonsPressed( $cab, @{$test} ), "\n"; } __END__ 0 : 1 : 2 : 0 1 : 0 2 : 1 2 : 0 1 2 :