#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my %seen = ("0,0" => 1);
my ($x, $y, $tx, $ty) = (0, 0, 0, 0);

while (<>) {
	my ($dir, $len) = /^(.) (\d+)/;

	while ($len--) {
		if ($dir eq 'U') {
			$y--;
		} elsif ($dir eq 'D') {
			$y++;
		} elsif ($dir eq 'L') {
			$x--;
		} elsif ($dir eq 'R') {
			$x++;
		}
		
		if ($x == $tx) {
			if ($y == $ty - 2) {
				$ty--;
			} elsif ($y == $ty + 2) {
				$ty++;
			}
		} elsif ($y == $ty) {
			if ($x == $tx - 2) {
				$tx--;
			} elsif ($x == $tx + 2) {
				$tx++;
			}
		} elsif ($x == $tx - 2) {
			$tx--;
			if ($y > $ty) {
				$ty++;
			} else {
				$ty--
			}
		} elsif ($x == $tx + 2) {
			$tx++;
			if ($y > $ty) {
				$ty++;
			} else {
				$ty--;
			}
		} elsif ($y == $ty - 2) {
			$ty--;
			if ($x > $tx) {
				$tx++;
			} else {
				$tx--;
			}
		} elsif ($y == $ty + 2) {
			$ty++;
			if ($x > $tx) {
				$tx++;
			} else {
				$tx--;
			}
		}
		$seen{"$tx,$ty"}++;
		say "$x,$y $tx,$ty";
	}
}

say scalar keys %seen;

