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
|
#!/usr/bin/env raku
use v6;
constant \EXAMPLE = q:to/END/;
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
END
sub solve($input, Int $step-right, Int $step-down --> Int) {
# Parse it
my @m = $input.split("\n").grep(*.chars > 0).map(*.comb.Array);
my $x = 0;
my $y = 0;
my $trees = 0;
my $width = @m[0].elems;
while $y < @m.elems {
$trees++ if @m[$y][$x] eq '#';
$y += $step-down;
$x = ($x + $step-right) % $width;
}
$trees
}
multi sub MAIN('part1', $file) {
say solve(slurp($file), 3, 1);
}
multi sub MAIN('part2', $file) {
my $input = slurp $file;
my &f = &solve.assuming($input);
say (
f(1, 1),
f(3, 1),
f(5, 1),
f(7, 1),
f(1, 2)
).reduce: &[*]
}
multi sub MAIN('test') {
use Test;
subtest 'Part 1', {
is solve(EXAMPLE, 3, 1), 7, 'Example';
}
}
|