[ art / civ / cult / cyb / diy / drg / feels / layer / lit / λ / q / r / sci / sec / tech / w / zzz ] archive provided by lainchan.jp

lainchan archive - /λ/ - 20599



File: 1480631549461.png (2.37 KB, 226x138, aoc.png)

No.20599

It's back, it's better than ever, and you can join the fun at http://adventofcode.com/. Are you in?

Feel free to share your answers here (or on the subreddit, like everyone else on the internet).

For the uninitiated, the Advent of Code is an annual programming challenge designed and Eric Wastl, the man behind The Synacor Challenge (among other things). It's fun, and if you suck (like me) and don't know your algorithms, you'll likely learn a lot.

  No.20600

File: 1480632010529.png (220.94 KB, 198x200, life-is-sour.png)

>To play, please identify yourself via one of these services:
> [GitHub] [Google] [Twitter] [Reddit]

  No.20603

I'm participating. Doing it in Haskell, as last year. I might also do it in Javascript, as I have an interview next week where they'd prefer me to use that or Ruby (which is basically gibberish to me).

>>20599
Did you attempt a cleverer solution for day 1 part 2 than just keeping a list of points visited? In principle you could do it by just keeping track of the line segment endpoints and checking for intersections, but that seems significantly tricker. I'm "cheating" and just using laziness to avoid the need for a clever approach.

  No.20605

>>20600
I believe the typical trick is to create a fresh Reddit account (you can make as many as you want) and just use it for AoC.

  No.20606

>>20603
I did mine in Scheme.

And yes, I did the lazy solution. Because if there's anything doing last year's AoC taught me, it's that laziness is most definitely a virtue (I spent many hours trying to optimize the travelling salesman problem: and the naive solution worked fine).

  No.20648

Day 3, Part 1, in AWK:

{if(($1+$2>$3)&&($1+$3>$2)&&($2+$3>$1)) count++;} END {print count}

  No.20723

Did mine using Agda. This is a super lazy implementation

Matrix : Set -> Nat * Nat -> Set
Matrix X (h , w) = Vec (Vec X w) h

tri : forall {h} -> Matrix Nat (h , 3) -> Nat
tri [] = zero
tri ((f :: s :: t :: x) :: xs) with ((((f +N s) > t) && ((f +N t) > s)) && ((t +N s) > f))
tri ((f :: s :: t :: x) :: xs) | tt = 1 +N tri xs
tri ((f :: s :: t :: x) :: xs) | ff = tri xs

triTest : Nat -> Nat -> Nat -> Nat
triTest f s t with ((((f +N s) > t) && ((f +N t) > s)) && ((t +N s) > f))
triTest f s t | tt = 1
triTest f s t | ff = zero

tri2 : forall {h} -> Matrix Nat (h , 3) -> Nat
tri2 [] = zero
tri2 (x :: []) = zero
tri2 (x :: s :: []) = zero
tri2 ((f :: s :: t :: fff) :: (f2 :: s2 :: t2 :: sss) :: (f3 :: s3 :: t3 :: ttt) :: xs) = triTest f f2 f3 +N triTest s s2 s3 +N triTest t t2 t3 +N tri2 xs

  No.20724

>>20723
Day 3 Part 1 and 2

  No.20725

>>20723
Wow, is this unreadable or is it just me?

  No.20732

>>20725
Never saw any agda before and it's perfectly readable and understandable to me.
Problem must be on your end.


Did the first five yesterday in Ruby after seeing this thread, other than the second part of the first one (doing it with vector intersections rather than storing every visited point), I was really underwhelmed by the difficulty, but oh well, still fun little puzzles.

  No.20737

>>20723
Looking at the explicit recursion I'm guessing Agda doesn't have map and fold? In Haskell I'd define tritest for 3-tuples, map that over the input list of tuples, and sum the result.

  No.20880

>>20732
Yeah, I'm about as far in as you and they are fairly easy. The second parts are sometimes a little interesting at least. If I'm bored I'll keep chugging along. Maybe switch to a language I'm less familiar with.

  No.20894

>>20732
>>20880

It gets harder. The difficulty jump happens around day 11, and it just gets more difficult from there.

  No.20921

I just started and it took a while to do day 1. i did the first part in a weird way and had to rewrite it to be able to do part 2. Have my poop code since i'm still a novice:
void one() {

struct node {
int x;
int y;
struct node *next;
};
FILE *file = fopen("input1.txt", "r");
char buffer[8];
char one, two;
int num, direction = 0, i,x,y,length=1;
struct node *head = malloc(sizeof (struct node));
struct node *current = head, *loop;
head->x = 0;
head->y = 0;
while (fscanf(file, "%c%d, ", &one, &num)) {
if (one != 'R' && one != 'L')
break;
direction = one == 'R' ? (direction + 1) % 4 : (direction + 3) % 4;
for (i = 0; i < num; i++) {
current->next = malloc(sizeof (struct node));
length++;
switch (direction){
case 0:
x=0;
y=1;
break;
case 1:
x=1;
y=0;
break;
case 2:
x=0;
y=-1;
break;
case 3:
x=-1;
y=0;
break;
}
current->next->x = current->x + x;
current->next->y = current->y + y;
current = current->next;
}
}
printf("(%d, %d)\n", current->x,current->y);
loop=head;
for(i=0; i<length; i++){
current=head;
for(x=0;x<i;x++){
if(current->x==loop->x && current->y==loop->y){
printf("(%d, %d)\n",current->x, current->y);
return;
}
current = current->next;
}
loop=loop->next;
}
}

  No.20922

>>20921
I have some untouched stuff from my first attempt, like the buffer. it adds every point i visit to a linkedlist then searches it for the first duplicate point

  No.20923

>>20921
That's actually *not* awful. it could be better, but, y'know, but terrible.

I'd start by cleaning up the "magic" direction numbers, and maybe even finding a cleaner, way to rotate than casing everything (there might be one: I found one, but my implementation was quite different).

And do *try* to free(2) you data...

  No.20925

>>20921
You could also use redirection for reading and writing files, so instead of fscanf(file... you could just simply read from stdin.

and execute your program like so:
./yourprogram < input1.txt

http://sc.tamu.edu/help/general/unix/redirection.html

  No.20934

>>20925
i did it that way because i'm suing netbeans and also because i'm doing each day in its own file, then calling it in main. I'm too lazy to open up cygwin, make, and run it myself on my craptop

  No.21263

>>20934
>i'm suing netbeans
On what grounds?