Skip to content

Instantly share code, notes, and snippets.

@simondegheselle
Created June 8, 2018 13:07
Show Gist options
  • Save simondegheselle/56edad5d7df3f8398f6a59c70576bf6f to your computer and use it in GitHub Desktop.
Save simondegheselle/56edad5d7df3f8398f6a59c70576bf6f to your computer and use it in GitHub Desktop.
16.pl
@ARGV_COPY = @ARGV;
$/ = undef;
$file = <>;
# filtering lines
$file =~ s/.* w$//ms;
$file =~ s/endstream.*//ms;
$_ = $file;
while (/(\d+)(?:\.\d+)? (\d+)(?:\.\d+)? m.(\d+)(?:\.\d+)? (\d+)(?:\.\d+)? l/gsm) {
$x1 = $1;
$y1 = $2;
$x2 = $3;
$y2 = $4;
$X{$x1} = undef;
$X{$x2} = undef;
$Y{$y1} = undef;
$Y{$y2} = undef;
}
$z = 1;
foreach (sort { $a <=> $b } keys %X) {
$X{$_} = $z;
$maxX = $_;
$z++;
}
$z = 1;
foreach (sort { $a <=> $b } keys %Y) {
$Y{$_} = $z;
$maxY = $_;
$z++;
}
foreach (keys %X) {
printf "%s: %s\n", $_, $X{$_};
}
foreach (keys %Y) {
printf "%s: %s\n", $_, $Y{$_};
}
@ARGV = @ARGV_COPY;
$/ = undef;
$file = <>;
# filtering lines
$file =~ s/.* w$//ms;
$file =~ s/endstream.*//ms;
$_ = $file;
@matrix = ();
while (/(\d+)(?:\.\d+)? (\d+)(?:\.\d+)? m.(\d+)(?:\.\d+)? (\d+)(?:\.\d+)? l/gsm) {
$x1 = $1;
$y1 = $2;
$x2 = $3;
$y2 = $4;
# verticale lijn
if ($x1 == $x2) {
if ($y2 > $y1) {
$min = $Y{$y1};
$max = $Y{$y2};
} else {
$min = $Y{$y2};
$max = $Y{$y1};
}
for ($i = $min; $i<= $max; $i++) {
$matrix[$i][$X{$x1}] = "|";
}
}
if ($y1 == $y2) {
if ($x2 > $x1) {
$min = $X{$x1};
$max = $X{$x2};
} else {
$min = $X{$x2};
$max = $X{$x1};
}
for ($i = $min; $i<= $max; $i++) {
$matrix[$Y{$y1}][$i] = "-";
}
}
}
print "\n\n";
foreach $row (reverse @matrix) {
foreach $element (@$row) {
if ($element eq "|" || $element eq "-" || $element eq "+") {
print $element;
} else {
print " ";
}
}
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment