day 1
This commit is contained in:
commit
79d25cb028
3 changed files with 1049 additions and 0 deletions
1000
day1/input1.txt
Normal file
1000
day1/input1.txt
Normal file
File diff suppressed because it is too large
Load diff
43
day1/p1.pl
Normal file
43
day1/p1.pl
Normal file
|
@ -0,0 +1,43 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
open(my $in, "<", "input1.txt");
|
||||
|
||||
my @nums1;
|
||||
my @nums2;
|
||||
|
||||
# assign numbers correctly
|
||||
while (<$in>) {
|
||||
my $trimmed = $_;
|
||||
$trimmed =~ s/\n//g;
|
||||
my @parts = split(/\s{3}/, $trimmed);
|
||||
push(@nums1, $parts[0]);
|
||||
push(@nums2, $parts[1]);
|
||||
}
|
||||
|
||||
my @sorted1 = sort(@nums1);
|
||||
my @sorted2 = sort(@nums2);
|
||||
my $total_length = scalar @sorted1;
|
||||
#foreach my $i (0..$total_length - 1) {
|
||||
# print "$sorted1[$i] $sorted2[$i]\n"
|
||||
#}
|
||||
|
||||
my $result = 0;
|
||||
my $similarity = 0;
|
||||
|
||||
foreach my $i (0..$total_length - 1) {
|
||||
if ($sorted1[$i] > $sorted2[$i]) {
|
||||
my $distance = $sorted1[$i] - $sorted2[$i];
|
||||
$result = $result + $distance;
|
||||
} elsif ($sorted1[$i] < $sorted2[$i]) {
|
||||
my $distance = $sorted2[$i] - $sorted1[$i];
|
||||
$result = $result + $distance;
|
||||
}
|
||||
|
||||
my @filtered = grep(/$sorted1[$i]/, @sorted2);
|
||||
my $numitems = scalar @filtered;
|
||||
$similarity += $sorted1[$i] * $numitems;
|
||||
}
|
||||
|
||||
print "Total distance: $result\n";
|
||||
print "Total similarity: $similarity\n";
|
6
day1/test.txt
Normal file
6
day1/test.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
Loading…
Add table
Reference in a new issue