-
-
Save alexgreenbank/7a9123f8bd1aec310d75836f8946596b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| my $y=0; | |
| my %map=(); | |
| while( my $line = <STDIN> ) { | |
| chomp( $line ); | |
| my $x=0; | |
| foreach my $o ( split( //, $line ) ) { | |
| $map{$x.":".$y}=$o; | |
| $x++; | |
| } | |
| $y++; | |
| } | |
| my $part1=0; | |
| my $step=0; | |
| while( 1 ) { | |
| $step++; | |
| foreach my $k ( keys %map ) { incr($k); } | |
| my $nosflash=0; | |
| foreach my $k ( keys %map ) { | |
| if( $map{$k} eq "*" ) { | |
| $nosflash++; | |
| $map{$k}=0; | |
| } | |
| } | |
| if( $step == 100 ) { print "part1: $part1\n"; } | |
| if( $nosflash == 100 ) { print "part2: $step\n"; last; } | |
| } | |
| exit; | |
| sub incr { | |
| my ( $k ) = @_; | |
| if( $map{$k} ne "*" ) { | |
| $map{$k}++; | |
| if( $map{$k} > 9 ) { | |
| $map{$k}="*"; | |
| flash($k); | |
| } | |
| } | |
| } | |
| sub flash { | |
| my ( $k ) = @_; | |
| my ( $fx, $fy ) = split( /:/, $k ); | |
| $part1++; | |
| for my $dx ( -1..1 ) { | |
| for my $dy ( -1..1 ) { | |
| next if( $dx == 0 && $dy == 0 ); | |
| my $dk=($fx+$dx).":".($fy+$dy); | |
| incr( $dk ) if( exists( $map{$dk} ) ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment