r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


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 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


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:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/lucbloom Dec 05 '18 edited Dec 05 '18

C implementation. Executes Part 2 in 0.13 msec.

static char* readFile(const char* fileName)
{
	char* buffer = nullptr;
	FILE* file = fopen(fileName, "rb");
	if (file)
	{
		fseek(file, 0, SEEK_END);
		size_t siz = ftell(file);
		buffer = (char*)malloc(siz+1);
		if (buffer)
		{
			fseek(file, 0, SEEK_SET);
			fread(buffer, 1, siz, file);
			buffer[siz] = '\0';
		}
		fclose(file);
	}
	return buffer;
}

static uint64_t getTime()
{
#define WINDOWS_TICK (100*100) // ns to ms
#define MSEC_TO_UNIX_EPOCH (1000*60*60*24*134774ULL) // 134774 days
	FILETIME fileTime;
	::GetSystemTimeAsFileTime(&fileTime);
	uint64_t windowsTicks = static_cast<__int64>(fileTime.dwHighDateTime) << 32 | fileTime.dwLowDateTime;
	windowsTicks = windowsTicks / WINDOWS_TICK - MSEC_TO_UNIX_EPOCH;
	return windowsTicks;
}

int main()
{
	uint64_t t1 = getTime();

	char* inputStart = readFile("C:/development/zooi/bgtools/input.txt");
	char* output = (char*)malloc(50000+1);
	int remains = MAX_INT;
	for (int i = 0; i < 1000; ++i)
	{
		for (int c = 'a'; c <= 'z'; ++c)
		{
			char* input = inputStart;
			char* cur = output;
			char e;
			while ((e = *input))
			{
				if (e != c && e != c - 'a' + 'A')
				{
					if (cur <= output || abs(*(cur-1) - e) != 0x20)
					{
						*cur = e;
						++cur;
					}
					else
					{
						--cur;
					}
				}
				++input;
			}
			if (cur - output < remains)
			{
				remains = cur - output;
			}
		}
	}
	uint64_t t2 = getTime();
	uint64_t t = t2 - t1;
	free(output);
	free(inputStart);

	printf(str, "Time: %I64u/1000 ms. Remains: %d", t, remains);

	return 0;
}

1

u/CryZe92 Dec 05 '18 edited Dec 05 '18

We measured this, and it takes about 4.37ms. Anything below that wouldn't make sense anyway as with minimal asm we can't get below that number either (we are hitting 1ms by parallelizing the outer A - Z loop though).

1

u/hpzr24w Dec 06 '18

Yeah, my C solution with minimal copying has a floor of about 4 msec for both parts, so getting much below that is going to take some doing.

Day 05 in C