-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.pl
134 lines (110 loc) · 2.59 KB
/
day20.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use strict;
use warnings;
use feature 'say';
use lib './';
use assertions;
open(my $file, "<", "input/day20.txt") or die "Can't open input file";
my @lines = ();
while (my $line = <$file>) {
chomp($line);
push(@lines, $line);
}
close $file;
sub newNumber {
my ($value, $index, $prev, $next) = @_;
return bless {
value => $value,
index => $index,
prev => $prev,
next => $next
}, "Number";
}
sub Number::str {
my $self = shift @_;
return $self->{value};
}
sub Number::next {
my $self = shift @_;
my $count = shift @_;
if ($count < 0) {
# actually prev
return $self->prev(-$count);
}
my $next = $self;
for (my $i = 0; $i < $count; $i++) {
$next = $next->{next};
}
return $next;
}
sub Number::prev {
my $self = shift @_;
my $count = shift @_;
my $prev = $self;
for (my $i = 0; $i <= $count; $i++) {
$prev = $prev->{prev};
}
return $prev;
}
# For part 1, set to 1.
my $multiplier = 811589153;
# For part 1, set to 1.
my $iters = 10;
my $start = undef;
my $zero = undef;
my $prev = undef;
my $count = 0;
foreach my $line (@lines) {
my $curr = newNumber($line * $multiplier, $count++);
if (!defined $start) {
$start = $curr;
}
if (defined $prev) {
$prev->{next} = $curr;
$curr->{prev} = $prev;
}
if ($curr->{value} == 0) {
$zero = $curr;
}
$prev = $curr;
}
$prev->{next} = $start;
$start->{prev} = $prev;
sub printNumbers {
my $start = shift @_;
my $curr = $start;
do {
printf "%d ", $curr->{value};
$curr = $curr->{next};
} while ($curr->{value} != $start->{value});
printf "\n";
}
sub moveNumbers {
my $start = shift @_;
for (my $i = 0; $i < $count; $i++) {
my $curr = $start;
while ($curr->{next}->{index} != $i) {
$curr = $curr->{next};
}
my $S1 = $curr;
my $X = $S1->{next};
# say "Moving " . $X->{value};
if ($X->{value} != 0) {
my $E1 = $X->{next};
my $S2 = $X->next($X->{value} % ($count - 1));
my $E2 = $S2->{next};
$S1->{next} = $E1;
$E1->{prev} = $S1;
$S2->{next} = $X;
$X->{prev} = $S2;
$X->{next} = $E2;
$E2->{prev} = $X;
}
# printNumbers $zero;
}
}
for (my $i = 0; $i < $iters; $i++) {
moveNumbers $zero;
# say "Iter " . $i;
}
# See $multiplier and $iters for Part 1/2.
say $zero->next(1000)->{value} + $zero->next(2000)->{value} + $zero->next(3000)->{value};