-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.pl
141 lines (120 loc) · 3.3 KB
/
day11.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
135
136
137
138
139
140
141
use strict;
use warnings;
use feature 'say';
use lib './';
use assertions;
open(my $file, "<", "input/day11.txt") or die "Can't open input file";
my @lines = ();
while (my $line = <$file>) {
chomp($line);
push(@lines, $line);
}
close $file;
sub newMonkey {
my ($id) = @_;
return bless {
id => $id,
items => [],
operation => "",
divisor => "",
trueThrow => "",
falseThrow => "",
inspections => 0
}, "Monkey";
}
sub Monkey::addItem {
my ($self, $item) = @_;
my $items = $self->{items};
push(@$items, $item);
}
sub Monkey::removeItem {
my ($self, $itemToRemove) = @_;
my $items = $self->{items};
my $i = 0;
foreach my $item (@$items) {
if ($item == $itemToRemove) {
splice(@$items, $i, 1);
last;
}
$i++;
}
}
sub Monkey::evalItemOp {
my ($self, $old) = @_;
return eval $self->{operation};
}
sub Monkey::toString {
my ($self) = @_;
return sprintf(
"Monkey %d [%s], op: '%s', div: % 3d, tt: %d, ft: %d",
$self->{id},
join(', ', @{$self->{items}}),
$self->{operation},
$self->{divisor},
$self->{trueThrow},
$self->{falseThrow}
);
}
my @monkeys = ();
my $currentMonkey;
my $commonDivisor = 1;
foreach my $line (@lines) {
if ($line =~ /^Monkey (\d+):$/) {
$currentMonkey = newMonkey($1);
} elsif ($line =~ /^\s*Starting items: ([\d, ]+)$/) {
for my $item (split(/, /, $1)) {
$currentMonkey->addItem($item);
}
} elsif ($line =~ /^\s*Operation: new = (.+)$/) {
$currentMonkey->{operation} = $1 =~ s/old/\$old/rg;
} elsif ($line =~ /^\s*Test: divisible by (\d+)$/) {
$currentMonkey->{divisor} = $1;
$commonDivisor *= $1;
} elsif ($line =~ /^\s*If true: throw to monkey (\d+)$/) {
$currentMonkey->{trueThrow} = $1;
} elsif ($line =~ /^\s*If false: throw to monkey (\d+)$/) {
$currentMonkey->{falseThrow} = $1;
} elsif ($line =~ /^\s*$/) {
push(@monkeys, $currentMonkey);
} else {
die "failed to parse input.";
}
}
push(@monkeys, $currentMonkey); # 🐒
# foreach my $monkey (@monkeys) {
# say $monkey->toString;
# }
my $rounds = 10_000;
foreach my $round (1 .. $rounds) {
foreach my $monkey (@monkeys) {
while ($#{$monkey->{items}} >= 0) {
$monkey->{inspections}++;
my $item = $monkey->{items}[0];
$monkey->removeItem($item);
my $newItem = $monkey->evalItemOp($item);
# for part 1:
# $newItem = sprintf("%d", $newItem / 3);
# for part 2:
$newItem = $newItem % $commonDivisor;
my $throwTo = $newItem % $monkey->{divisor} == 0 ? $monkey->{trueThrow} : $monkey->{falseThrow};
$monkeys[$throwTo]->addItem($newItem);
}
}
if ($round == 20) {
# for part 1:
# last;
}
if ($round % 100 == 0) {
printf "% 3d%%\n", $round / 100;
}
# say "Round $round";
# foreach my $monkey (@monkeys) {
# say $monkey->toString;
# }
# say "";
}
# Part 1 / 2
# For switching between part 1 and 2, see comments above.
my @insp = sort({$b <=> $a} map({ $_->{inspections} } @monkeys));
say join(', ', @insp);
say $insp[0] * $insp[1];