-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16.pl
More file actions
71 lines (58 loc) · 1.55 KB
/
day16.pl
File metadata and controls
71 lines (58 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use strict;
use Term::ANSIColor;
use Win32::Console::ANSI;
use Time::HiRes qw/time/;
my $time = time;
my $filename = "${0}_input";
open(my $fh, $filename);
my @line = ("a".."p");
my @input = split ",",<$fh>;
my %outputs = {join("",@line) => 0};
my $output;
my $count = 1_000_000_000;
my $i=1;
while (1) {
foreach (@input) {
chomp;
if($_ =~ "s"){
my ($count) = $_ =~ /(\d+)/;
# print "spin : $_ => $count\n";
for (my $i = 0; $i < $count; $i++) {
unshift @line, pop(@line);
}
} elsif ($_ =~ "x"){
my @pos = $_ =~ /(\d+)/g;
# print "exchange: $_ => ($pos[0], $pos[1])\n";
my $temp = $line[$pos[0]];
$line[$pos[0]] = $line[$pos[1]];
$line[$pos[1]] = $temp;
} elsif ($_ =~ "p"){
my ($partner1, $partner2) = (split (""))[1,3];
# print "partner : $_ => ($partner1, $partner2)\n";
foreach (@line){
if( $_ eq $partner1 ){
$_ = $partner2;
} elsif( $_ eq $partner2 ){
$_ = $partner1;
}
}
}
}
$output = join "",@line;
if($i==1){
print "The order after 1 dance is ", colored( $output, "black on_red" ), ". ( ", sprintf ("%.3f",time - $time) ," s )\n";
}
if(exists $outputs{$output}){
#print "element $i is a repeated output of element $outputs{$output}.\n";
last;
}
$outputs{$output}=$i++;
}
my $period = $i - $outputs{$output};
$count -= {$outputs{$output}-1};
$count %= $period;
while (my ($order,$index) = each %outputs){
if($index == $count){
print "The order after 1.000.000.000 dances is ", colored( $order, "black on_red" ), ". ( ", sprintf ("%.3f",time - $time) ," s )\n";
}
}