[ 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)


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.


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]


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).

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.


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.


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).


Day 3, Part 1, in AWK:

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


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


Day 3 Part 1 and 2


Wow, is this unreadable or is it just me?


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.


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.


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.



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


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')
direction = one == 'R' ? (direction + 1) % 4 : (direction + 3) % 4;
for (i = 0; i < num; i++) {
current->next = malloc(sizeof (struct node));
switch (direction){
case 0:
case 1:
case 2:
case 3:
current->next->x = current->x + x;
current->next->y = current->y + y;
current = current->next;
printf("(%d, %d)\n", current->x,current->y);
for(i=0; i<length; i++){
if(current->x==loop->x && current->y==loop->y){
printf("(%d, %d)\n",current->x, current->y);
current = current->next;


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


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...


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



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


>i'm suing netbeans
On what grounds?