#!/usr/bin/perl -w

use v5.38;
use experimental 'multidimensional';
$; = ';';

my ($x, $y) = (1000, 1000);
my ($xmin, $xmax, $ymin, $ymax) = (1000) x 4;
my %dir = (
	R => [ 1,  0 ],
	D => [ 0,  1 ],
	L => [ -1, 0 ],
	U => [ 0, -1 ],
);

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

		$xmax = $x if $xmax < $x;
		$ymax = $y if $ymax < $y;
		$xmin = $x if $xmin > $x;
		$ymin = $y if $ymin > $y;

		$map{$x,$y} = $col;
	}
}

say "$xmin..$xmax x $ymin..$ymax";

my $sum;
for my $y ($ymin .. $ymax) {
	my $in = 0;
	my $from = 0;
	for my $x ($xmin .. $xmax) {
		if ($map{$x,$y}) {
			$sum++;
			if ($map{$x,$y-1} && $map{$x,$y+1}) {
				$in = !$in;
				$from = 0;
			} elsif ($map{$x,$y-1}) {
				if ($from == 1) {
					$in = !$in;
					$from = 0;
				} elsif ($from == 0) {
					$from = -1;
				} else {
					$from = 0;
				}
			} elsif ($map{$x,$y+1}) {
				if ($from == -1) {
					$in = !$in;
					$from = 0;
				} elsif ($from == 0) {
					$from = 1;
				} else {
					$from = 0;
				}
			}
			print '#';
		} else {
			$sum++ if $in;
			print $map{$x,$y} || $in ? 'O' : '.';
		}
	}
	print "\n";
}


asay $sum;

