From e3567af39d5b9459f3050d1c6b64ffc1f976b633 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 24 Dec 2020 06:32:30 +0100 Subject: [PATCH] Day 24 - nice! --- 47.pl | 29 +++++++++++++++++++++++++++++ 48.pl | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100755 47.pl create mode 100755 48.pl diff --git a/47.pl b/47.pl new file mode 100755 index 0000000..fca227f --- /dev/null +++ b/47.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w + +use strict; + +my %tiles; + +while (<>) { + chomp; + my ($x, $y) = (0, 0); + while (length) { + s/\A(se|ne|sw|nw|e|w)//; + if ($1 eq 'ne') { + $y++; + } elsif ($1 eq 'e') { + $x++; + } elsif ($1 eq 'se') { + $x++; $y--; + } elsif ($1 eq 'sw') { + $y--; + } elsif ($1 eq 'w') { + $x--; + } elsif ($1 eq 'nw') { + $x--; $y++; + } + } + $tiles{"$x|$y"}++; +} + +print "Tiles=" . (grep { $tiles{$_} % 2 == 1 } keys %tiles)."\n"; diff --git a/48.pl b/48.pl new file mode 100755 index 0000000..f76f993 --- /dev/null +++ b/48.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl -w + +use strict; + +my %tiles; + +my ($min_x, $max_x, $min_y, $max_y); + +while (<>) { + chomp; + my ($x, $y) = (0, 0); + while (length) { + s/\A(se|ne|sw|nw|e|w)//; + if ($1 eq 'ne') { + $y++; + } elsif ($1 eq 'e') { + $x++; + } elsif ($1 eq 'se') { + $x++; $y--; + } elsif ($1 eq 'sw') { + $y--; + } elsif ($1 eq 'w') { + $x--; + } elsif ($1 eq 'nw') { + $x--; $y++; + } + } + $tiles{"$x|$y"} ^= 1; + $min_x = $x if !defined $min_x || $min_x > $x; + $max_x = $x if !defined $max_x || $max_x < $x; + $min_y = $y if !defined $min_y || $min_y > $y; + $max_y = $y if !defined $max_y || $max_y < $y; +} + + +for (1 .. 100) { + $min_x--; $min_y--; $max_x++, $max_y++; + my %newtiles; + for my $x ($min_x .. $max_x) { + for my $y ($min_y .. $max_y) { + my $count = 0; + $count++ if $tiles{($x+1).'|'.($y)}; # E + $count++ if $tiles{($x).'|'.($y+1)}; # NE + $count++ if $tiles{($x+1).'|'.($y-1)}; # SE + $count++ if $tiles{($x).'|'.($y-1)}; # SW + $count++ if $tiles{($x-1).'|'.($y)}; # W + $count++ if $tiles{($x-1).'|'.($y+1)}; # NW + if ($tiles{"$x|$y"} && ($count == 0 || $count > 2)) { + $newtiles{"$x|$y"} = 0; + } elsif (!$tiles{"$x|$y"} && $count == 2) { + $newtiles{"$x|$y"} = 1; + } else { + $newtiles{"$x|$y"} = $tiles{"$x|$y"}; + } + } + } + %tiles = %newtiles; + print "Tiles=" . (grep { $tiles{$_} } keys %tiles)."\n"; +} + -- 2.43.5