#!/usr/bin/perl use v5.35.0; sub try_ops { return undef if @_ < 3; my ($target, $tried, $left) = splice @_, 0, 3; if (@_) { my $right = shift; try_ops($target, "$tried*", $left * $right, @_) || $target > $left && ( try_ops($target, "$tried+", $left + $right, @_) || try_ops($target, "$tried.", $left . $right, @_) ); } else { $target == $left && "=$tried"; } } my $total = 0; while (<>) { my ($test_val, @terms) = split /:?\s+/; $total += $test_val if try_ops($test_val, '', @terms); } say $total;