-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09b.pl
executable file
·53 lines (42 loc) · 1.31 KB
/
day09b.pl
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
#!/usr/bin/perl
#
# It took me a few days, but I finally realized... "It's a linked list!!!"
#
use strict;
use warnings;
my %scores;
my $curr_marble = { num => 0 };
$curr_marble->{ prev } = $curr_marble;
$curr_marble->{ next } = $curr_marble;
sub place_marble {
my ($num) = @_;
$curr_marble = $curr_marble->{ next };
my $next = $curr_marble->{ next };
my $marble = { num => $num, prev => $curr_marble, next => $next };
$curr_marble->{ next } = $marble;
$next->{ prev } = $marble;
$curr_marble = $marble;
return;
}
my $num_players = $ARGV[0] || die "Enter number of players\n";
my $last_marble = $ARGV[1] || die "Enter score of last marble\n";
my $player = 0;
for (my $i = 1; $i <= $last_marble; $i++) {
if ($i % 23) {
place_marble( $i );
}
else {
# Back up 7
for (my $p = 0; $p < 7; $p++) {
$curr_marble = $curr_marble->{ prev };
}
$scores{ $player } += $i + $curr_marble->{ num };
#print "$curr_marble->{ prev }{ num }: $player scores $i + $curr_marble->{ num }\n";
$curr_marble->{ prev }{ next } = $curr_marble->{ next };
$curr_marble->{ next }{ prev } = $curr_marble->{ prev };
$curr_marble = $curr_marble->{ next };
}
$player = ($player + 1) % $num_players;
}
my $highest = (sort { $b <=> $a } (values %scores))[0];
print "The highest score is $highest\n";