-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_9.pl
More file actions
86 lines (65 loc) · 1.97 KB
/
task_9.pl
File metadata and controls
86 lines (65 loc) · 1.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
#===============================================================================
#
# FILE: task_9.pl
#
# USAGE: ./task_9.pl
#
# DESCRIPTION: https://adventofcode.com/2018/day/9
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Lubos Kolouch
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 12/09/2018 10:36:39 AM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use 5.026;
use Data::Dumper;
my $players = shift;
my $last_marble = shift;
$last_marble = $last_marble;
my %scores;
my $current_player = 0;
my $position = 0;
my $marbles->{$position} = {next=>0, prev=>1};
for my $current_marble ( 1 .. $last_marble ) {
$current_player++;
$current_player = $current_player % $players;
if ( $current_marble % 23 == 0 ) {
$scores{$current_player} += $current_marble;
for (1..7) {
$position = $marbles->{$position}->{prev};
}
$scores{$current_player} += $position;
my $prev = $marbles->{$position}->{prev};
my $next = $marbles->{$position}->{next};
$marbles->{$prev}->{next} = $next;
$marbles->{$next}->{prev} = $prev;
$position = $next;
}
else {
# current position, move to next one
# 0 - (1)
$position = $marbles->{$position}->{next};
my $old_next = $marbles->{$position}->{next};
my $old_prev = $position;
$marbles->{$position}->{next} = $current_marble;
$position = $marbles->{$position}->{next};
$marbles->{$position}->{next} = $old_next;
$marbles->{$position}->{prev} = $old_prev;
$position = $marbles->{$position}->{next};
$marbles->{$position}->{prev} = $current_marble;
$position = $current_marble;
}
}
my $max = 0;
for ( keys %scores ) {
$max = $scores{$_} if $scores{$_} > $max;
}
say $max;