Rules: no spoilers.

The other rules are made up as we go along.

Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.

  • 200fifty@awful.systems
    link
    fedilink
    English
    arrow-up
    4
    ·
    11 months ago

    day 2

    perl
    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    use v5.010;
    use List::Util qw/ max /;
    
    # Parse the input
    
    my %games = ();
    
    for my $line (<>) {
        $line =~ /Game (\d+): (.+)/;
        my $game_id = $1;
        my $game_str = $2;
    
        my @segments = split '; ', $game_str;
        my @game = ();
        for my $segment (@segments) {
            my @counts = split ', ', $segment;
    
            my %colors = (red => 0, blue => 0, green => 0);
            for my $count (@counts) {
                $count =~ /(\d+) (\w+)/;
                $colors{$2} = $1;
            }
    
            push @game, { %colors };
        }
    
        $games{$game_id} = [ @game ];
    }
    
    # Part 1
    
    my $part1 = 0;
    
    game: for my $game_id (keys %games) {
        for my $segment (@{$games{$game_id}}) {
            next game if $segment->{red} > 12 || $segment->{green} > 13 || $segment->{blue} > 14;
        }
    
        $part1 += $game_id;
    }
    
    say "Part 1: $part1";
    
    # Part 2
    
    my $part2 = 0;
    
    for my $game (values %games) {
        my ($red, $green, $blue) = (0, 0, 0);
    
        for my $segment (@$game) {
            $red = max $segment->{red}, $red;
            $green = max $segment->{green}, $green;
            $blue = max $segment->{blue}, $blue;
        }
    
        $part2 += $red * $green * $blue;
    }
    
    say "Part 2: $part2";
    

    Found this much easier than day 1 honestly…