summary refs log tree commit diff stats
path: root/2020/day-10/day-10.raku
blob: 803de0a76195b2201704e0d4fbda4b2bffadfb50 (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
#!/usr/bin/env raku

sub MAIN (
    Int $part where * == 1|2 = 1 #= part to run (1 or 2)
) {
    my $solution;

    # Initialize @adapters with charging outlet joltage. @adapters
    # will hold joltages of each adapter.
    my @adapters = 0;
    append @adapters, "input".IO.lines>>.Int.sort;
    push @adapters, @adapters[*-1] + 3; # push the built in joltage.

    if $part == 1 {
        my Int ($diff_1, $diff_3);

        for 0 .. @adapters.end - 1 -> $idx {
            # joltage difference.
            my $diff = @adapters[$idx + 1] - @adapters[$idx];
            $diff_1++ if $diff == 1;
            $diff_3++ if $diff == 3;
        }
        $solution = $diff_1 * $diff_3;
    } elsif $part == 2 {
        my @memoize;
        # complete-chain returns the number of ways to complete the
        # chain given that you're currently at @adapters[$idx]. This
        # is taken from Jonathan Paulson's solution.
        sub complete-chain (
            Int $idx
        ) {
            return 1 if $idx == @adapters.end;
            return @memoize[$idx] if @memoize[$idx];

            my Int $ways;
            for $idx + 1 .. @adapters.end {
                if @adapters[$_] - @adapters[$idx] <= 3 {
                    $ways += complete-chain($_);
                }
            }
            @memoize[$idx] = $ways;
            return $ways;
        }
        $solution = complete-chain(0);
    }

    say "Part $part: ", $solution;
}