From: Jan "Yenya" Kasprzak Date: Thu, 24 Dec 2020 05:32:30 +0000 (+0100) Subject: Day 24 - nice! X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=e3567af39d5b9459f3050d1c6b64ffc1f976b633;p=aoc2020.git Day 24 - nice! --- 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"; +} +