r/adventofcode • • Dec 05 '16

SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---

--- Day 5: How About a Nice Game of Chess? ---

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


STAYING ON TARGET IS MANDATORY [?]

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!

14 Upvotes

188 comments sorted by

View all comments

1

u/schlocke Dec 05 '16

PHP, JS, HTML... Initially I just wanted PHP but my local machine couldn't handle it and I'm not about to set up a dedicated server for my AoC solutions. I realized if I just loaded one character at a time via AJAX i wouldn't hit my execution limits in PHP. NOTE: this is on PHP 5.5.12, 8 GB ram, i5 4310M 2.7 GHz, 64 bit windows 7 while running chrome, WAMP, security scan, outlook, slack, etc. Takes a while (took me 5.7 min) to get the answers but both are done in parallel. NOTE2: I also know i could've probably done this all in JS but I had the PHP figured out already.

PHP (day5.php):

<?php
	$input = "ojvtpuvg";
	$p = '';
	$i= $_POST['i'];
	$index = 0;

	while(strlen($p) < 1) {
		$c = md5($input.$i);
		if( substr($c,0,5) === "00000") {
			if($_POST['part'] === '1') { 
				$p = substr($c, 5, 1);
			} else {
				$index = substr($c, 5, 1);
				$p = substr($c, 6, 1);
			}
		}
		$i++;
	}

	echo json_encode(array("p" => $p, "index" => $index, "i" => $i));

HTML+JS(day5.html):

<html>
	<head>
		<title>test</title>
	</head>
	<body>
		Password1<span id="load1">(loading...)</span> = <span id="password1"></span>
		<br>
		Password2<span id="load2">(loading...)</span> = <span id="password2"></span>
	
		<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
		<script>
			$(document).ready(function(){

				function getp1(i, j) {
					$.ajax({
						method: "POST",
						url: "day5.php",
						dataType: "json",
						data: { i:i , part:1}
						})
						.done(function( data ) {
							$("#password1").append(data.p);
							j++;
							if(j < 8) getp1(data.i, j);
							else $("#load1").text("DONE");
							return;
						}
					);
				}

				function getp2(i, j, arr) {
					$.ajax({
						method: "POST",
						url: "day5.php",
						dataType: "json",
						data: { i: i, part:2 }
						})
						.done(function( data ) {
							
							if(arr[data.index] === '') {
								arr[data.index] = data.p;
								$("#password2").text(arr.join("_"));
								j++;
							}
							if(j < 8) {
								getp2(data.i, j, arr);
							} else {
								$("#password2").text(arr.join(""));
								$("#load2").text("DONE");
							}
							return;
						}
					);
				}

				getp1(0, 0);
				getp2(0, 0, ['','','','','','','','']);
			});			
		</script>
	</body>
</html>

EDIT: added execution time 5.7min. EDIT2: if some web designer wants to steal this and do the cool hacking movie animation go a head im too lazy for that right now.