-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday07.pl
More file actions
121 lines (103 loc) · 2.62 KB
/
day07.pl
File metadata and controls
121 lines (103 loc) · 2.62 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
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
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 %values;
my %children;
my %parent;
my $root;
while(my $input = <$fh>){
chomp $input;
#print "$input\n---\n";
my @input = split /,? /,$input;
#print arrayToString(@input),"\n";
my $element = $input[0];
my $value = substr($input[1], 1, length($input[1]) - 2);
$values{$element} = $value;
$children{$element} = [@input[3..$#input]];
foreach my $child (@input[3..$#input]) {
$parent{$child} = $element;
}
}
foreach my $element (keys %values) {
if(!$parent{$element}){
$root = $element;
}
}
print "The bottom program is named ", colored( $root, "black on_red" ), ". ( ", sprintf ("%.3f",time - $time) ," s )\n";
$time = time;
my @problemChildren;
my @problemValues;
createStack($root);
foundProblemSituation:
my $correctTotal = $problemValues[0]==$problemValues[1]?$problemValues[0]:$problemValues[2];
my $wrongTotal = (grep {!/$correctTotal/} @problemValues)[0];
my $wrongValue;
my $wrongProgram;
for (my $i = 0; $i < scalar @problemValues; $i++) {
if($problemValues[$i]==$wrongTotal){
$wrongProgram = $problemChildren[$i];
$wrongValue = $values{$problemChildren[$i]};
last;
}
}
print "The new value for the wrong program ($wrongProgram) is ",
colored( $wrongValue + ($correctTotal-$wrongTotal), "black on_red" ),
". ( ", sprintf ("%.3f",time - $time) ," s )\n";
sub arrayToString {
my $retval="";
foreach my $x (@_) {
$retval.= "$x,";
}
chop $retval;
return $retval;
}
sub childrenToString {
my $retval="";
for (my $i = 0; $i < scalar @_ /2; $i++) {
$retval .= $_[$i] . " (" . $_[$i + scalar @_ /2] . "),";
}
chop $retval;
return $retval;
}
sub createStack{
my $root = shift;
createLevel($root,0);
}
sub createLevel{
my $element = shift;
my $level = shift;
my @childWeights;
my $weight = $values{$element};
foreach my $child (@{$children{$element}}) {
push @childWeights, createLevel($child,$level+1);
}
if(!elementsEqual(@childWeights)){
@problemChildren = @{$children{$element}};
@problemValues = @childWeights;
goto foundProblemSituation;
foreach my $childWeight (@childWeights) {
$weight+=$childWeight;
}
$weight += scalar @childWeights * $childWeights[0];
} else {
$weight += scalar @childWeights * $childWeights[0];
}
#used for visual representation, remove goto for full tree
# for (my $i = 0; $i < $level; $i++) {
# print " ";
# }
# print "|> $element ($weight)\n";
return $weight;
}
sub elementsEqual {
for (my $i = 0; $i < scalar @_; $i++) {
if($_[0] != $_[$i]){
return 0;
}
}
return 1;
}