-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_12_2.pl
86 lines (69 loc) · 1.71 KB
/
task_12_2.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
#!/usr/bin/perl
#===============================================================================
#
# FILE: task_12.pl
#
# USAGE: ./task_12.pl
#
# DESCRIPTION: www.adventofcode.com task 12 part 1
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Lubos Kolouch
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 12/12/2018 06:05:42 PM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use autodie;
use 5.026;
use Data::Dumper;
my $input = shift;
my $generations = shift;
open my $file, '<', $input;
my $gen = 0;
my $pad = $generations * 1.5;
my %field;
my %rules;
my %counts;
my %diffs;
while (<$file>) {
chomp;
next if /^$/msx;
if (/initial/msx) {
($field{$gen}) = $_ =~ /\:\h+(.*)/msx;
$field{$gen} = '.'x$pad . $field{$gen} .'.'x$pad;
} else {
my ($rule, $result) = $_ =~ /(.*?)\h+.*\h+(.)/msx;
$rules{$rule} = $result if $result eq '#';
}
}
for (1 .. $generations) {
$gen = $_;
my $str = $field{$gen-1};
$str = '..'.$str;
my $count;
for (2..length($str)-1) {
my $test_pattern = substr( $str, $_-2, 5);
if ($rules{$test_pattern}) {
$field{$gen} .= '#';
$count += $_ - $pad -2;
} else {
$field{$gen} .= '.';
}
}
$counts{$gen} = $count;
$diffs{$gen} = $counts{$gen} - $counts{$gen-1};
say "$gen $count ".$diffs{$gen};
if ($diffs{$gen} == $diffs{$gen-1}) {
say 'stable';
print 'Total: ';
my $result = $counts{$gen} + (50_000_000_000 - $gen) * $diffs{$gen};
say $result;
last;
}
}