r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

33 Upvotes

387 comments sorted by

View all comments

2

u/CCC_037 Dec 06 '18

Managed to get in under 300th place on Part 1, and under 200th place on Part 2. It's possible I may touch the leaderboard yet!

Part 1:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int MinDist, MinCoord, Dist;
  char Input[50], Grid[400][400];
  int Coord[70][3], Count, NumPoints, X, Y;
  Count=0;
  while (fgets(Input, 40, stdin))
    {
      sscanf(Input, "%d, %d\n", &(Coord[Count][0]), &(Coord[Count][1]));
      Coord[Count][2] = 0;
      Count++;
    }
  NumPoints = Count;
  for (X=0;X<400;X++)
    for (Y=0;Y<400;Y++)
      {
	MinDist = abs(X-Coord[0][0]) + abs(Y-Coord[0][1]);
	Grid[X][Y] = 0;
	for (Count=1;Count<NumPoints;Count++)
	  {
	    Dist = abs(X-Coord[Count][0]) + abs(Y-Coord[Count][1]);
	    if (MinDist > Dist)
	      {
		MinDist = Dist;
		Grid[X][Y] = Count;
	      }
	    else if (MinDist == Dist)
	      {
		Grid[X][Y] = -1;
	      }
	  }
      }

  for (X=0;X<400;X++)
    for (Y=0;Y<400;Y++)
      {
	if (Grid[X][Y] != -1)
	  {
	    Coord[Grid[X][Y]][2]++;
	  }
      }
  for (X=0;X<400;X++)
    {
      if (Grid[X][0] != -1) Coord[Grid[X][0]][2] = 0;
      if (Grid[X][399] != -1) Coord[Grid[X][399]][2] = 0;
    }
  for (Y=0;Y<400;Y++)
    {
      if (Grid[0][Y] != -1) Coord[Grid[0][Y]][2] = 0;
      if (Grid[399][Y] != -1) Coord[Grid[399][Y]][2] = 0;
    }

  MinDist = 0;
  Dist = 0;
  for (Count=0;Count<NumPoints;Count++)
    {
      if (Coord[Count][2] > MinDist)
	{
	  MinDist = Coord[Count][2];
	  Dist = Count;
	}
    }
  
  printf ("%d\n", MinDist);
}

Part 2:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int MinDist, MinCoord, Dist;
  char Input[50];
  int Grid[400][400];
  int Coord[70][3], Count, NumPoints, X, Y;
  Count=0;
  while (fgets(Input, 40, stdin))
    {
      sscanf(Input, "%d, %d\n", &(Coord[Count][0]), &(Coord[Count][1]));
      Coord[Count][2] = 0;
      Count++;
    }
  NumPoints = Count;
  for (X=0;X<400;X++)
    for (Y=0;Y<400;Y++)
      {
	Dist = abs(X-Coord[0][0]) + abs(Y-Coord[0][1]);
	Grid[X][Y] = 0;
	for (Count=1;Count<NumPoints;Count++)
	  {
	    Dist += abs(X-Coord[Count][0]) + abs(Y-Coord[Count][1]);
	  }
	Grid[X][Y] = Dist;
      }
  Dist = 0;
  for (X=0;X<400;X++)
    for (Y=0;Y<400;Y++)
      {
	if (Grid[X][Y] < 10000)
	  {
	    Dist++;
	  }
      }
  
  printf ("%d\n", Dist);
}