r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

38 Upvotes

340 comments sorted by

View all comments

3

u/donaldihunter Dec 04 '18 edited Dec 04 '18

Perl 6

#!/usr/bin/env perl6
use v6;

class Guard {
    has $.id;
    has int @.sleep[60];
    has $.fall;

    method fall(Int $t) { $!fall = $t; }
    method wake(Int $t) {  @!sleep[$_] += 1 for $!fall..^$t; }
    method totes { [+] @!sleep; }
    method max { @!sleep.max; }
    method gist { "Guard #{$!id} – { [+] @!sleep } – {@!sleep}"; }

    method show {
        my $max = @!sleep.maxpairs[0];
        say '';
        say "Guard #{$!id} had {$max.value} sleeps in minute {$max.key}";
        say "Giving {$!id * $max.key}";
    }
}

my %guards;

for '4a-input.txt'.IO.lines.sort -> $event {
    my ($mm, $act) = $event.match(/ ':' (\d+) ']' \s (.*) /).map: ~*;

    state $current;
    given $act {
        when /'Guard #' (\d+)/ {
            my $id = ~$/[0];
            $current = %guards{$id} //= Guard.new(:$id);
        }
        when /'falls asleep'/ {
            $current.fall(+$mm);
        }
        when /'wakes up'/ {
            $current.wake(+$mm);
        }
    }
}

# Part One
%guards.values.max(*.totes).show;

# Part Two
%guards.values.max(*.max).show;