#!/usr/bin/perl -w

use v5.16;
use strict;
use feature 'bitwise';

$/ = undef;
$_ = <> . "\n";

while (1) {
	my ($wire, $val);
	if (s/^(\d+) -> (\w+)\n//m) {
		($wire, $val) = ($2, $1);
	} elsif (s/^NOT (\d+) -> (\w+)\n//m) {
		($wire, $val) = ($2, $1 ^ 0xFFFF);
	} elsif (s/^(\d+) AND (\d+) -> (\w+)\n//m) {
		($wire, $val) = ($3, $1 & $2);
	} elsif (s/^(\d+) OR (\d+) -> (\w+)\n//m) {
		($wire, $val) = ($3, $1 | $2);
	} elsif (s/^(\d+) RSHIFT (\d+) -> (\w+)\n//m) {
		($wire, $val) = ($3, $1 >> $2);
	} elsif (s/^(\d+) LSHIFT (\d+) -> (\w+)\n//m) {
		($wire, $val) = ($3, ($1 << $2) & 0xFFFF);
	}
	last if !$wire;
	if ($wire eq 'a') {
		say "a=$val";
		last;
	}
	# say "$wire => $val";
	last if $val !~ /\A\d+\z/;
	$val &= 0xFFFF;
	s/\b$wire\b/$val/g;
	# say "$_\n\n";
}
