r/adventofcode • • Dec 03 '15

SOLUTION MEGATHREAD --- Day 3 Solutions ---

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Post your solution as a comment. Structure your post like the Day One thread in /r/programming.

24 Upvotes

228 comments sorted by

View all comments

3

u/Aneurysm9 Dec 03 '15

Perl, once again sloppily done :)

my @moves = split //, $data;

my $x = 0;
my $y = 0;

my $houses = {};

$houses->{$x}{$y} = 1;
foreach my $move (@moves) {
	if ($move eq '^') {
		$y++;
	} elsif ($move eq 'v') {
		$y--;
	} elsif ($move eq '>') {
		$x++;
	} elsif ($move eq '<') {
		$x--;
	}
	$houses->{$x}{$y} += 1;
}

my $count = 0;
foreach my $a (keys %$houses) {
	foreach my $b (keys %{$houses->{$a}}) {
		$count++;
	}
}

Minor changes for part 2:

my @moves = split //, $data;

my $x = 0;
my $y = 0;

my $houses = {};

my $s = 0;
my @pos;
$pos[0]{x} = 0;
$pos[0]{y} = 0;
$pos[1]{x} = 0;
$pos[1]{y} = 0;

$houses->{$s}{$x}{$y} = 1;
foreach my $move (@moves) {
	if ($move eq '^') {
		$pos[$s]{y}++;
	} elsif ($move eq 'v') {
		$pos[$s]{y}--;
	} elsif ($move eq '>') {
		$pos[$s]{x}++;
	} elsif ($move eq '<') {
		$pos[$s]{x}--;
	}
	$houses->{$pos[$s]{x}}{$pos[$s]{y}} += 1;
	$s = 1-$s;
}

my $count = 0;
foreach my $a (keys %$houses) {
	foreach my $b (keys %{$houses->{$a}}) {
			$count++;
}
}

print "total: $count\n";

1

u/volatilebit Dec 04 '15

Have you thought about trying any of these in Perl6 as a learning excercise?

Though it's not actually "released" yet, supposed to be around Christmas.

1

u/Aneurysm9 Dec 04 '15

I haven't really thought about Perl6 since a talk I saw Larry Wall give at OSCON many years ago. That is a good idea though.

1

u/volatilebit Dec 04 '15

I think I may try it.

I was excited about Perl 6 a long long time ago and started to participate in on the internals mailing list before soon losing interest. Just checked, and that was over 10 years ago.