#!/usr/bin/perl -w

use v5.38;

my ($x, $y) = (20_000_000, 20_000_000);
my %dir = (
	0 => [ 1,  0 ],
	1 => [ 0,  1 ],
	2 => [ -1, 0 ],
	3 => [ 0, -1 ],
);

my %map;
my %dirs;
while (<>) {
	my ($len, $dir) = /\(\#(\w+)(\w)\)/;
	$len = hex $len;
	$map{$y}{$x} |= (1 << $dir);
	$x += $len * $dir{$dir}->[0];
	$y += $len * $dir{$dir}->[1];
	$map{$y}{$x} |= (1 << ($dir ^ 2));
	say "at $x,$y dir $dir len $len";
}

sub sum_int {
	my @pts = @_;
	my $sum = 0;
	my $in;
	my $prev;
	for my $pt (@pts) {
		if (defined $prev && $in) {
			$sum += $pt - $prev + 1;
		}
		$prev = $pt;
		$in = !$in;
	}
	return $sum;
}

my @pts;
my $y0;
my $sum;
for my $y1 (sort { $a <=> $b } keys %map) {
	say "y $y1";
	my $in;
	my $x0;
	my @nx;
	my @tx;
	my $rowsum;
	if (defined $y0) {
		my $psum = sum_int(@pts);
		$sum += ($y1-$y0-1) * $psum;
	}

	for my $x1 (sort { $a <=> $b } (@pts, keys %{ $map{$y1} })) {
		next if $x0 && $x1 == $x0;
		my $d = $map{$y1}{$x1};
		my $d0 = $map{$y1}{$x0} if defined $x0;
		say " at $x1,$y1 ", $d // "|";
		push @nx, $x1
			if !defined $d || ($d & 2);
		if (!defined $d) {
			push @tx, $x1;
		} elsif ($d & 4) {
			if (@tx & 1) {
				if (($d0 == 9 && $d == 6) || ($d0 == 3 && $d == 12)) {
					push @tx, $x1;
				}
			} else {
				push @tx, $x0;
				if (($d0 == 9 && $d == 12) || ($d0 == 3 && $d == 6)) {
					push @tx, $x1;
				}
			}
		}

		if (defined $x0) {
			say "   int $x0 .. $x1";
			
			$in = !$in;
			if ($in) {
				$rowsum += $x1 - $x0 + 1;
			}
		}
		$x0 = $x1;
	}
	$sum += sum_int(@tx);
	say " tx=@tx sum ", sum_int(@tx);
	say " nx=@nx sum ", sum_int(@nx);
	@pts = @nx;
	$y0 = $y1;
}

say $sum;

