r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


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 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


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!

38 Upvotes

340 comments sorted by

View all comments

1

u/itsjeremeh Dec 05 '18

Javascript (Node) - Not the most efficient but it worked :)

const $input = myInput.split('\n');
const logRegex = /\[([\d|\:|\-\s]+)\] (.+)/
let currentGuardId = false;

const logs = $input.map(input => {
	let matches = logRegex.exec(input);
	
	if (!matches) {
	  return null;
	}
	
	return {
		date: new Date(matches[1]),
		event: matches[2],
	}
})
.filter(log => log !== null) // clear empties
.sort((a, b) => {
	if (a.date > b.date) {
	  return 1;
	}
	
	if (a.date < b.date) {
		return -1;
	}
	
	return 0;
})
.reduce((carry, log) => {
	if (log.event.indexOf('Guard') !== -1) {
		let [ match, guardId ] = /.+?\#(\d+).+?/.exec(log.event);
		currentGuardId = guardId;
	 
		if (!carry[guardId]) {
			carry[guardId] = {
				id: guardId,
				events: [],
			}
		}    
	  
		carry[guardId].events[carry[guardId].events.length] = [];
		return carry;
	}
	
	carry[currentGuardId].events[carry[currentGuardId].events.length - 1].push(log);
	
	return carry;
}, {});

const minsTotal = Object
.values(logs)
.map(log => {
	let timeline = {};
	let totalAsleep = 0;
	for (let i = 0; i < 60; i++) {
		timeline[i] = 0;
	}

	log.events.forEach((events) => {
		let fellAsleepAt = false;
		events.forEach(event => {
			if (event.event === 'falls asleep') {
				fellAsleepAt = event.date;
				return;
			}
			
			totalAsleep += event.date.getMinutes() - fellAsleepAt.getMinutes();
			for (let i = fellAsleepAt.getMinutes(); i < event.date.getMinutes(); i++) {
				timeline[i] += 1;
			}

		});
	})
	
	let popularMins = Object.keys(timeline).sort((a, b) => {
		if (timeline[a] < timeline[b]) {
			return 1;
		}
		
		if (timeline[b] < timeline[a]) {
	    	return -1;
		}
		
		return 0;
	}).shift();
	
	return {
	  id: log.id,
		timeline,
		totalAsleep,
		popularMins,
	}
})


const partOne = minsTotal.sort((a, b) => {
	if (a.totalAsleep < b.totalAsleep) {
		return 1;
	}
	
	if (a.totalAsleep > b.totalAsleep) {
		return -1;
	}
	
	return 0;
}).shift();

console.log('p1', partOne.id * partOne.popularMins);

const partTwo = minsTotal.map(guard => {
	return {
		id: guard.id,
		total: guard.timeline[guard.popularMins],
		minute: guard.popularMins
	}
}).sort((a, b) => {
	if (a.total < b.total) {
		return 1;
	}
	
	if (a.total > b.total) {
		return -1;
	}
	
	return 0;
}).shift();

console.log(partTwo.id * partTwo.minute)