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

Show parent comment

2

u/[deleted] Dec 04 '18

Nice! I think i'm going to start using Sscanf.

Here is mine for both parts:

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"
)

func main() {
	start := time.Now()
	file, err := os.Open("input.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	// see: https://regex101.com/r/VRbjzk/1
	var re = regexp.MustCompile(`.(.*)(]\s)(.[a-zA-Z]*)(..)(.*)`)
	lines := []string{}
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	sort.Strings(lines)
	currentGuard := 0
	guardSleepTimes := make([][]int, 4000) //should calculate highest guard id instead of hardcoding 4000
	for i := 0; i < 4000; i++ {
		guardSleepTimes[i] = make([]int, 61) // minute counters, 61 is summ
	}

	sleepTimer := 0 // time guard fell asleep
	for _, line := range lines {
		eventLog := re.FindAllStringSubmatch(line, -1)
		minuteSplit := strings.Split(eventLog[0][1], ":")
		minutes, _ := strconv.Atoi(minuteSplit[1])
		logType := eventLog[0][3] // falls,wakes,Guard
		if logType == "Guard" {
			s := strings.Split(eventLog[0][5], " ")
			currentGuard, err = strconv.Atoi(s[0])
			if err != nil {
				fmt.Println(err)
			}
		}
		if logType == "falls" {
			sleepTimer = minutes
		}
		// when guard wakes up calculate time sleep
		if logType == "wakes" {
			for i := sleepTimer; i < minutes; i++ {
				guardSleepTimes[currentGuard][i]++  // incrememnt current slept count
				guardSleepTimes[currentGuard][60]++ // total minutes slept buffer
			}
			sleepTimer = 0

		}

	}
	// Find sleepiest Guard
	sleepiestGuard := 0
	sleepiestMinute := 0
	sleepBuf := 0
	sleepiestGuardMin := 0
	for guard, sleepTime := range guardSleepTimes {
		// only get guards that have slept
		if sleepTime[60] == 0 {
			continue
		}
		if sleepTime[60] >= guardSleepTimes[sleepiestGuard][60] {
			sleepiestGuard = guard

		}
		for minute, sleepMin := range sleepTime {
			if sleepMin > sleepBuf && minute != 60 {
				sleepiestMinute = minute
				sleepBuf = sleepMin
				sleepiestGuardMin = guard
			}
		}
	}
	// Find sleepiest hour for sleepiest guard
	sleepiestHour := 0
	sleepBuf = 0
	for hour, sleepTime := range guardSleepTimes[sleepiestGuard] {
		if sleepTime > sleepBuf && hour != 60 {
			sleepiestHour = hour
			sleepBuf = sleepTime
		}
	}
	fmt.Println("Sleepiest Guard:", sleepiestGuard)
	fmt.Println("Sleepiest hour:", sleepiestHour)
	fmt.Println("Part 1 Code:", sleepiestGuard*sleepiestHour)
	fmt.Println("Part 2 Code:", sleepiestGuardMin*sleepiestMinute)
	fmt.Println(time.Since(start))
}