summary refs log tree commit diff stats
path: root/2020/day-03/day-03.raku
blob: c8656ac565dd650f133a7e21aa6ea037ac8e2dbb (plain) (blame)
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
#!/usr/bin/env raku

sub MAIN (
    # Part to run.
    Int $part where * == 1|2 = 1
) {
    my @input = "input".IO.lines>>.comb;

    if $part == 1 {
        say "Part 1: ", count-trees(@input, 3, 1);
    } elsif $part == 2 {
        my @trees = [{ right => 1, down => 1 },
	 { right => 3, down => 1 },
	 { right => 5, down => 1 },
	 { right => 7, down => 1 },
	 { right => 1, down => 2 },].map(
            {count-trees(@input, $_<right>, $_<down>)}
        );
        say "Part 2: ", [*] @trees;
    }
}

sub count-trees (
    @input,
    Int $right,
    Int $down
) {
    # Start at top left position.
    my $x = 0;
    my $y = 0;
    my $trees = 0;

    my $x-max = @input[0].elems - 1;
    my $y-max = @input.elems - 1;

    loop {
        $trees++ if @input[$y][$x] eq '#';

        $x += $right;
        $y += $down;

        # Cannot let $x become greater than $x-max because that'll produce
        # error when we try to access @input with those positions. So, we
        # wrap it around because the same map is repeated.
        $x -= $x-max + 1 if $x > $x-max;

        last if $y-max < $y;
    }
    return $trees;
}