-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_12_1.pl
More file actions
73 lines (58 loc) · 1.38 KB
/
task_12_1.pl
File metadata and controls
73 lines (58 loc) · 1.38 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
#!/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;
open my $file, '<', $input;
my $gen = 0;
my %field;
my %rules;
while (<$file>) {
chomp;
next if /^$/msx;
if (/initial/msx) {
($field{$gen}) = $_ =~ /\:\h+(.*)/msx;
$field{$gen} = '.'x30 . $field{$gen} .'.'x30;
} else {
my ($rule, $result) = $_ =~ /(.*?)\h+.*\h+(.)/msx;
$rules{$rule} = $result if $result eq '#';
}
}
#say "0 ".$field{0};
for (1 .. 20) {
$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 += $_ - 32;
} else {
$field{$gen} .= '.';
}
}
say "$gen $count";
# say "$gen $field{$gen} $count";
}