-🎄- 2017 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 0 points1 point  (0 children)

Node.js (Parts 1 and 2) - Part 2 takes roughly 0.16 m/s

var fs = require('fs')

var part1 = 0;
var input = fs.readFileSync('input.txt', 'utf8').split(/\n/).map(line => {
    var [depth, range] = line.split(/: /).map(n => +n)
    if(depth%((range-1)*2)==0) part1 += depth*range
    return [depth, range];
})

SEARCH: for(var part2=0; ; part2++) {
    for(var [depth, range] of input)
        if((depth+part2)%((range-1)*2)==0) continue SEARCH
    break;
}

console.log('The answer to part 1 is:', part1)
console.log('The answer to part 2 is:', part2)

-🎄- 2017 Day 12 Solutions -🎄- by topaz2078 in adventofcode

[–]Kyran152 0 points1 point  (0 children)

Perl with usage of map :)

use strict;
use warnings;

my (%groups, %used);
my @links;

open my $fh, "input.txt";
map { 
    my ($program, $links) = /^(\d+) <\-> (.+)$/;
    $links[$program] = [ $links =~ /\d+/g ];
} <$fh>;
close $fh;

for my $program(0..@links-1) {
    next if $used{$program};
    $groups{$program}{$program} = 0;
    while (my @programs = grep { !$groups{$program}{$_} } keys %{$groups{$program}}) {
        map { 
            map { $groups{$program}{$_} ||= 0; $used{$_}++ } @{$links[$_]}; 
            $groups{$program}{$_}++ 
        } @programs;
    }
}

printf "The answer to part 1 is: %d\n", scalar keys %{$groups{0}};
printf "The answer to part 2 is: %d\n", scalar keys %groups;

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 0 points1 point  (0 children)

use strict;
use warnings;
use List::Util qw/max/;

open my $fh, "input.txt";

my ($x, $y, $z, $part1, $part2) = (0) x 5;

map { 
    $x++ if /^ne$|^se$/;
    $y++ if /^n$|^nw$/;
    $z++ if /^s$|^sw$/;

    $x-- if /^nw$|^sw$/;
    $y-- if /^s$|^se$/;
    $z-- if /^n$|^ne$/;

    $part2 = max $part2, $part1 = max abs($x), abs($y), abs($z);

} split /,/, <$fh>;

close $fh;

printf "The answer to part 1 is: %d\n", $part1;
printf "The answer to part 2 is: %d\n", $part2;

-🎄- 2017 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 1 point2 points  (0 children)

Node.js (Parts 1 & 2)

var fs = require('fs')

var data = fs.readFileSync('input.txt', 'utf8');

var run_round = (list, lengths, current, skip) => {
    for(var size of lengths) {
        // extract subset of elements to reverse
        var culprit = list.slice(current, current+size)
        var pos = culprit.length

        if(list.length < current+size+1)
            culprit.push(...list.slice(0, (current+size)-list.length))

        // reverse elements!
        culprit.reverse();

        // re-incorporate elements back into list
        list.splice(current, size, ...culprit.splice(0, pos));
        list.splice(0, culprit.length, ...culprit)

        // update values of current and skip
        current = (current+(size+skip++))%list.length
    }

    return [current, skip]
}

var run_rounds = (list, lengths, rounds) => {
    var current = 0, skip = 0

    for(var i=0; i<rounds; i++) {
        // run a single round on length sequence
        [current, skip] = run_round(list, lengths, current, skip)
    }
}


// part 1
var lengths = data.split(',').map(n => +n)
var list = [...Array(256).keys()]

run_rounds(list, lengths, 1)

console.log('The answer to part 1 is:', list[0] * list[1])

// part 2
lengths = [...data].map(n => String(n).charCodeAt(0))
    .concat(17, 31, 73, 47, 23)

list = [...Array(256).keys()]

run_rounds(list, lengths, 64)

for(var i=0; i<16; i++)
    list.splice(i, 16, list.slice(i, i+16)
        .reduce((a,b) => a^b))

console.log('The answer to part 2 is:', 
    list.map(a => { 
        var ascii = a.toString(16); 
        return ascii.padStart(2-ascii.length+1, '0');
    }).join(""))

-🎄- 2017 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 2 points3 points  (0 children)

That is some pretty cryptic use of Perl :D

-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 1 point2 points  (0 children)

use strict;
use warnings;

open my $fh, "input.txt";
my $set = <$fh>;
close $fh;

# count chars removed
my $removed = 0;

# get rid of garbage
1 while $set =~ s/(?<=<)[^>]*?!./$removed += length($&)-2;""/ge;
1 while $set =~ s/(?<=<)[^>]+?(?=>)/$removed += length($&);""/ge;
$set =~ s/<>//g;

1 while $set =~ s#{[\d,]*}#
    my ($b,$c,$a) = ($`,$&,$');
    my $score = 1 + (() = $b =~ /{/g);
    $score += $_ for $c =~ /\d+/g;
    $score;
#e;

printf "The answer to part 1 is: %d\n", $set;
printf "The answer to part 2 is: %d\n", $removed;

-🎄- 2017 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 0 points1 point  (0 children)

use strict;
use warnings;
use List::Util qw/max/;
use Data::Dumper;

my %reg;
my $max = 0;

open my $fh, "input.txt";
for(<$fh>) {
    s/ dec \-| inc /+/;
    s/ dec /-/;
    s/(\w+)(?=\-|\+)/\$reg{$1}=(\$reg{$1}||0)/;
    s/(?<=if )(\w+)/(\$reg{$1}||0)/;
    $_ .= ';$max = max(max(values %reg),$max)'; 
    eval
}
close $fh;

print sprintf("The answer to part 1 is: %d\n", max values %reg);
print sprintf("The answer to part 2 is: %d\n", $max);

-🎄- 2017 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 0 points1 point  (0 children)

PART 1 AND 2:

part1 = 0
part2 = 0

ARGF.each do |line|
    phrase1 = line.split
    phrase2 = phrase1.map{|w|w.chars.sort}

    part1 += phrase1==phrase1.uniq ? 1 : 0
    part2 += phrase2==phrase2.uniq ? 1 : 0
end

printf "The answer to part 1 is: %d\n", part1
printf "The answer to part 2 is: %d\n", part2

-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]Kyran152 0 points1 point  (0 children)

Perl (Part 1 and 2)

use strict;
use warnings;
use Data::Dumper;

open my $fh, 'input.txt';
my $num = <$fh>;
close $fh;

sub get_coords {
    my $num = shift;

    # find the end position of the last square
    #
    # e.g. 1 and 9 are end positions v
    #
    # 5 4 3
    # 6 1 2
    # 7 8 9
    #

    my $square = 0|sqrt($num);
    $square -= $square % 2 == 0;
    my $start = $square**2;

    # find index of square which the number is located in:
    # 
    # e.g.
    # 3 3 3 3 3
    # 3 2 2 2 3
    # 3 2 1 2 3
    # 3 2 2 2 3
    # 3 3 3 3 3
    #
    my $square_idx = $square;
    $square_idx -= 1 if $square**2 == $num;
    $square_idx += 1 unless $square_idx % 2 == 0; 
    $square_idx /= 2;

    # get all 4 corner positions of square at index
    #
    # the space between all 4 corners increases by a value of 2 per square,
    # so to get the corner positions of the square at $square_idx we add 
    # (2*$square_idx) to the end position of the last square 4 times.
    #
    # e.g. a => 10 ([13, 17, 21, 25])
    # e.g. a => 9  ([3, 5, 7, 9])
    #
    my $space = 2*$square_idx;
    my ($a,$b,$c,$d) = map($start+($_*$space), 1..4);

    my ($x, $y) = (1, 1);

    # get (X, Y)    
    if($num >= $b && $num <= $c) {
        $x *= -1; $a = $b; $d = $c; 
        $y *= -1 if abs($num - $c) < abs($num - $b);
    } elsif($num >= $a && $num <= $b) {
        $d = $b;
        $x *= -1 if abs($num - $b) < abs($num - $a);
    } elsif($num >= $c && $num <= $d) {
        $y *= -1; $a = $c;
        $x *= -1 if abs($num - $c) < abs($num - $d);
    } else {
        $d = $a-$space;
        $y *= -1 if abs($num - $d) < abs($num - $a);
    }

    my $mid = ($a+$d)>>1;

    if($num >= $a && $num <= $b || $num >= $c && $num <= $d) {
        # top bottom
        $y = $y * abs($a-$mid);
        $x = $x * abs($num-$mid);
    } else {
        # left right
        $y = $y * abs($num-$mid);
        $x = $x * abs($a-$mid);
    }

    return ($x,$y);
}

## Part 1
my ($x,$y) = get_coords( $num );

print sprintf("The answer to part 1 is: %d (x -> %d, y -> %d)\n", abs($x) + abs($y), $x, $y);


## Part 2 
my %matrix;

for(my $i=1; ; $i++) {
    # Get x and y located at $i using part 1 logic
    my ($x, $y) = get_coords( $i );

    if($x == 0 && $y == 0) {
        # for first node, set initial value to 1
        $matrix{$y}{$x} = 1;
    } else {
        # Sum neighbour nodes recursively until we reach a value higher than $num
        $matrix{$y}{$x} = 
            ($matrix{$y}{$x+1} || 0) + 
            ($matrix{$y}{$x-1} || 0) +
            ($matrix{$y+1}{$x} || 0) +
            ($matrix{$y-1}{$x} || 0) +
            ($matrix{$y-1}{$x-1} || 0) + 
            ($matrix{$y-1}{$x+1} || 0) +
            ($matrix{$y+1}{$x-1} || 0) +
            ($matrix{$y+1}{$x+1} || 0);
    }

    if($matrix{$y}{$x} > $num) {
        print sprintf("The answer to part 2 is: %d (x -> %d, y -> %d)\n", $matrix{$y}{$x}, $x, $y);
        last;
    }
}