this post was submitted on 23 Dec 2024
14 points (88.9% liked)

Advent Of Code

920 readers
3 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
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

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 23: LAN Party

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] vole 2 points 1 week ago

Raku

My actual solution ran in about 2.5 seconds, but I optimized it to run in about 1 second.

sub MAIN($input) {
    my $file = open $input;
    my @connection-list := $file.slurp.trim.lines>>.split("-")>>.List.List;

    my %connections;
    my %all-computers is SetHash;
    for @connection-list -> @c {
        my ($first, $second) = @c.sort;
        %connections{$first} = [] if %connections{$first}:!exists;
        %connections{$second} = [] if %connections{$second}:!exists;
        %connections{$first}.push($second);
        %all-computers{@c.all}++;
    }
    for %connections.values -> $list is rw {
        $list = $list.sort.List;
    }

    my $part1-solution = 0;
    for %connections.keys -> $c1 {
        for %connections{$c1}.Seq -> $c2 {
            for (%connections{$c1} โˆฉ %connections{$c2}).keys -> $c3 {
                next unless ($c1, $c2, $c3).any.substr(0,1) eq "t";
                $part1-solution++;
            }
        }
    }
    say "part1 solution: $part1-solution";

    my $part2-solution = find-max-party((), %connections, %all-computers).join(",");
    say "part2 solution: $part2-solution";
}

sub find-max-party(@party, %connections, %available-members) {
    my @max-party = @party;
    for %available-members.keys.sort -> $c1 {
        my @new-party := (|@party, $c1);
        my %new-available-members := %available-members โˆฉ %connections{$c1};
        my @max-party-candidate = find-max-party(@new-party, %connections, %new-available-members);
        @max-party = @max-party-candidate if @max-party-candidate.elems > @max-party.elems;
        last if @max-party.elems == @party.elems + %available-members.elems;
    }
    return @max-party;
}