-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_8_2.pl
More file actions
74 lines (57 loc) · 1.4 KB
/
task_8_2.pl
File metadata and controls
74 lines (57 loc) · 1.4 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
#!/usr/bin/perl
#===============================================================================
#
# FILE: task_8_2.pl
#
# USAGE: ./task_8_2.pl
#
# DESCRIPTION: https://adventofcode.com/2018/day/8
# second part
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Lubos Kolouch
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 12/08/2018 12:36:27 PM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use autodie;
use 5.026;
use Data::Dumper;
my @all_data;
sub process_node {
my $child_count = shift @all_data;
my $metadata = shift @all_data;
my @child_total;
my $child_value = 0;
for (1 .. $child_count) {
push @child_total, &process_node;
}
# no children nodes
if ($child_count == 0) {
$child_value += shift @all_data for (1 .. $metadata);
} else {
# children nodes
for (1..$metadata) {
my $child = shift @all_data;
# skip if does not exist
next if $child > scalar @child_total;
$child_value += $child_total[ $child - 1 ];
}
}
return $child_value;
}
sub main {
open my $file, '<', 'input8.txt';
my $str = <$file>;
close $file;
@all_data = split / /, $str;
say &process_node();
return 1;
}
main;