<?php $masses = explode(',', file_get_contents('input.txt')); function get_fuel_from_mass($mass) { return floor($mass / 3) - 2; } echo array_sum( array_map(function($mass) { $total_fuel = 0; while($mass > 0) { $mass = max(get_fuel_from_mass($mass), 0); $total_fuel += $mass; } return $total_fuel; }, $masses) );