summaryrefslogtreecommitdiffstats
path: root/2019/day3/wires.pl
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:40:39 +0300
committerYigit Sever2021-12-13 10:40:39 +0300
commitbf16b19b1f6deffd1983efca059db576f3b60ee5 (patch)
tree1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day3/wires.pl
parent74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff)
downloadaoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip
2019, tracking
Diffstat (limited to '2019/day3/wires.pl')
-rw-r--r--2019/day3/wires.pl68
1 files changed, 68 insertions, 0 deletions
diff --git a/2019/day3/wires.pl b/2019/day3/wires.pl
new file mode 100644
index 0000000..fa4ddf1
--- /dev/null
+++ b/2019/day3/wires.pl
@@ -0,0 +1,68 @@
1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5use List::Util 'min';
6# use Smart::Comments;
7use Data::Dumper;
8
9sub manhattan_distance {
10 my $ref = shift;
11 my ($x, $y) = @{$ref};
12
13 return abs(0 - $x) + abs(0 - $y);
14}
15
16sub pedometer {
17 my $ref = shift;
18 my ($x, $y, $step_1, $step_2) = @{$ref};
19 return $step_1 + $step_2;
20}
21
22my %directions = (
23 'U' => [0, 1],
24 'D' => [0,-1],
25 'R' => [1, 0],
26 'L' => [-1,0]
27);
28
29my $turn = 0;
30my @intersections = ();
31my %map;
32
33while(<>) {
34 chomp;
35 my @path = split /,/;
36 my @coord = (0,0); # Starting point
37 my $num_steps = 0;
38
39 if (!$turn){
40 foreach my $way (@path) {
41 my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!";
42 for (1..$speed) {
43 $coord[0] += @{$directions{$dir}}[0];
44 $coord[1] += @{$directions{$dir}}[1];
45 $num_steps++;
46 $map{"$coord[0],$coord[1]"} = $num_steps;
47 }
48 }
49 $turn = !$turn;
50 } else {
51 foreach my $way (@path) {
52 my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!";
53 for (1..$speed) {
54 $coord[0] += @{$directions{$dir}}[0];
55 $coord[1] += @{$directions{$dir}}[1];
56 $num_steps++;
57 if (exists $map{"$coord[0],$coord[1]"}) {
58 push @intersections, [@coord,$num_steps,$map{"$coord[0],$coord[1]"}];
59 }
60 }
61 }
62 }
63}
64
65# print Dumper \@intersections;
66print(min(map {pedometer($_);} @intersections));
67
68