#!/usr/bin/env ysh source --builtin list.ysh # sum() const INPUT = $1 const COLORS = {'red': 12, 'green': 13, 'blue': 14} proc main { var possible_ids = [] while read -r game { var possible = true var sets = game=>split(':')[1] setvar sets = sets=>split(';') for i in (0 .. len(sets)) { for color in (COLORS->keys()) { var pair = $(grep -o "[0-9]* ${color}" <<< $[sets[i]] || true) if (len(pair) !== 0) { var count = int(pair=>split()[0]) if (count > COLORS[color]) { setvar possible = false break 2 } } } } if (possible) { var id = game=>split(':')[0] setvar id = id=>split()[1] call possible_ids->append(id) } } < ${INPUT} echo $[sum(possible_ids)] } main