-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_14.pl
More file actions
59 lines (50 loc) · 1.37 KB
/
task_14.pl
File metadata and controls
59 lines (50 loc) · 1.37 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
#!/usr/bin/perl
#===============================================================================
#
# FILE: task_14_3.pl
#
# USAGE: ./task_14_3.pl
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: YOUR NAME (),
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 12/15/2018 06:01:16 PM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use 5.026;
use autodie;
my $goal = shift;
#part 1
my $recipes = '37';
my ($elf1, $elf2) = (0, 1);
while (length $recipes < $goal + 10) {
my ($value1, $value2) = (substr($recipes, $elf1, 1), substr($recipes, $elf2, 1));
$recipes .= $value1 + $value2;
$elf1 = ($elf1 + 1 + $value1) % length $recipes;
$elf2 = ($elf2 + 1 + $value2) % length $recipes;
}
say 'Part1 :'.substr($recipes, $goal, 10);
#part 2
while (1) {
if (length($recipes) - length($goal) > 1) {
pos($recipes) = length($recipes) - length($goal) - 2;
} else {
pos($recipes) = 0;
}
if ($recipes =~ /$goal/g) {
say 'Part2: '.$-[0];
last;
}
my ($value1, $value2) = (substr($recipes, $elf1, 1), substr($recipes, $elf2, 1));
$recipes .= $value1 + $value2;
$elf1 = ($elf1 + 1 + $value1) % length $recipes;
$elf2 = ($elf2 + 1 + $value2) % length $recipes;
}