#!/usr/bin/perl -w

use v5.36;
use strict;

undef $SIG{__WARN__};
my %names;
my %vals;
while (<>) {
	chomp;
	my ($name, $rest) = split /:/;
	$names{$name} = $rest;
	$vals{$name} = 0+$rest if $rest =~ /\A\s*\d+\z/;
}

delete $vals{humn};
$names{humn} = 'xx';

while (!defined $vals{root}) {
	my $added;
	for my $n (keys %names) {
		no warnings;
		next if defined $vals{$n};
		my $e = $names{$n};
		my $d = 1;
		for my $v (keys %vals) {
			$e =~ s/$v/$vals{$v}/g;
			$names{$n} = $e;
		}
		next if $e =~ /[a-z]/;
		
		eval "\$vals{$n} = $e";
		# say "set \$vals{$n} to $vals{$n}" unless $@;
		$added = 1;
	}
	last if !$added;
}

for my $n (sort keys %names) {
	say "$n = ", defined $vals{$n} ? $vals{$n} : $names{$n};
}

my ($val) = $names{root} =~ /(\d+)/;
my ($var) = $names{root} =~ /([a-z]+)/;
while (defined $var) {
	say "$var = $val, var = $names{$var}";
	my ($arg1, $op, $arg2) = $names{$var} =~ /(\w+)\s+(\S)\s+(\w+)/;
	say "-> $arg1 $op $arg2";
	if ($arg2 =~ /\d/) {
		$var = $arg1;
		if ($op eq '/') {
			$val = $val * $arg2;
		} elsif ($op eq '*') {
			$val = $val / $arg2;
		} elsif ($op eq '+') {
			$val = $val - $arg2;
		} elsif ($op eq '-') {
			$val = $val + $arg2;
		}
		say "var $var = $val";
	} elsif ($arg1 =~ /\d/) {
		$var = $arg2;
		if ($op eq '/') {
			$val = $arg1 / $val;
		} elsif ($op eq '*') {
			$val = $val / $arg1;
		} elsif ($op eq '+') {
			$val = $val - $arg1;
		} elsif ($op eq '-') {
			$val = $arg1 - $val;
		}
		say "var $var = $val";
		if ($var eq 'humn') {
			$vals{humn} = $val;
			last;
		}
	} else {
		last;
	}
}
say $vals{humn};
