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

3

u/drakmaniso Dec 04 '18

Go (golang)

Part 1:

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strings"
	"time"
)

type entry struct {
	date   time.Time
	action action
	guard  int
}

type action int

const (
	beginShift action = iota
	fallAsleep
	wakeUp
)

func main() {
	entries := read()

	var sleepyguard int
	asleep := map[int]int{}
	var guard, from int
	for _, e := range entries {
		switch e.action {
		case beginShift:
			guard = e.guard
		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			t := e.date.Minute() - from
			asleep[guard] += t
			if asleep[guard] > asleep[sleepyguard] {
				sleepyguard = guard
			}
		}
	}

	minutes := [60]int{}
	guard = -1
	var sleepyminute int
	for _, e := range entries {
		if e.action == beginShift {
			guard = e.guard
			continue
		}
		if guard != sleepyguard {
			continue
		}
		switch e.action {
		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			to := e.date.Minute()
			for i := from; i < to; i++ {
				minutes[i]++
				if minutes[i] > minutes[sleepyminute] {
					sleepyminute = i
				}
			}
		}
	}

	fmt.Printf("Answer: guard %d * minute %d = %d\n",
		sleepyguard, sleepyminute, sleepyguard*sleepyminute)
}

func read() []entry {
	entries := []entry{}
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		txt := s.Text()
		e := entry{guard: -1}

		var y, m, d, hr, mn int
		n, err := fmt.Sscanf(txt, "[%d-%d-%d %d:%d]", &y, &m, &d, &hr, &mn)
		if n < 5 || err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
			continue
		}
		e.date = time.Date(y, time.Month(m), d, hr, mn, 0, 0, time.UTC)

		i := strings.Index(txt, "] ")
		if i == -1 {
			fmt.Fprintf(os.Stderr, "ERROR: unable to parse message\n")
			continue
		}
		txt = txt[i+2:]
		n, err = fmt.Sscanf(txt, "Guard #%d begins shift", &e.guard)
		switch {
		case n == 1:
			e.action = beginShift
		case txt == "falls asleep":
			e.action = fallAsleep
		case txt == "wakes up":
			e.action = wakeUp
		default:
			fmt.Fprintf(os.Stderr, "ERROR: unknown action\n")
			continue
		}
		entries = append(entries, e)
	}

	sort.Slice(entries, func(i, j int) bool {
		return entries[i].date.Before(entries[j].date)
	})
	return entries
}

Part 2:

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strings"
	"time"
)

type entry struct {
	date   time.Time
	action action
	guard  int
}

type action int

const (
	beginShift action = iota
	fallAsleep
	wakeUp
)

func main() {
	entries := read()

	var sleepyguard, sleepyminute int
	minutes := map[int]*[60]int{}
	var guard, from int
	for _, e := range entries {
		switch e.action {
		case beginShift:
			guard = e.guard
			if minutes[guard] == nil {
				minutes[guard] = &[60]int{}
			}
			if minutes[sleepyguard] == nil {
				sleepyguard = guard
			}

		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			to := e.date.Minute()
			for i := from; i < to; i++ {
				minutes[guard][i]++
				if minutes[guard][i] > minutes[sleepyguard][sleepyminute] {
					sleepyguard = guard
					sleepyminute = i
				}
			}
		}
	}

	fmt.Printf("Answer: guard %d * minute %d = %d\n",
		sleepyguard, sleepyminute, sleepyguard*sleepyminute)
}

func read() []entry {
	entries := []entry{}
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		txt := s.Text()
		e := entry{guard: -1}

		var y, m, d, hr, mn int
		n, err := fmt.Sscanf(txt, "[%d-%d-%d %d:%d]", &y, &m, &d, &hr, &mn)
		if n < 5 || err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
			continue
		}
		e.date = time.Date(y, time.Month(m), d, hr, mn, 0, 0, time.UTC)

		i := strings.Index(txt, "] ")
		if i == -1 {
			fmt.Fprintf(os.Stderr, "ERROR: unable to parse message\n")
			continue
		}
		txt = txt[i+2:]
		n, err = fmt.Sscanf(txt, "Guard #%d begins shift", &e.guard)
		switch {
		case n == 1:
			e.action = beginShift
		case txt == "falls asleep":
			e.action = fallAsleep
		case txt == "wakes up":
			e.action = wakeUp
		default:
			fmt.Fprintf(os.Stderr, "ERROR: unknown action\n")
			continue
		}
		entries = append(entries, e)
	}

	sort.Slice(entries, func(i, j int) bool {
		return entries[i].date.Before(entries[j].date)
	})
	return entries
}

3

u/knotdjb Dec 05 '18

I did a solution in Go as well. I notice you parsed the date/time into a time struct. Since the date/time is in big-endian form, it naturally lends itself to string sorting.

For example, once you have all the lines in a string slice, you can simply do the following:

sort.Slice(lines, func(i, j int) bool {
    l := len("[1518-07-14 00:02]")
    return lines[i][0:l] < lines[j][0:l]
})

1

u/drakmaniso Dec 05 '18

You're right! I over-engineered... Thanks for pointing it out!

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))
}

1

u/frenetix Dec 05 '18

Another one:

package main

import (
	"io"
	"log"
	"regexp"
	"sort"
	"strconv"
)

func day4() Puzzle {
	type Nap struct {
		start int
		stop  int
	}

	guardRE := regexp.MustCompile(`#(\d+)`)
	wakeRE := regexp.MustCompile(`(\d\d)] w`)
	sleepRE := regexp.MustCompile(`(\d\d)] f`)
	parse := func(in io.Reader) map[int][]Nap {
		naps := make(map[int][]Nap)
		lines := lines(in)
		sort.Strings(lines)
		var current int
		var start int
		for _, line := range lines {
			match := guardRE.FindStringSubmatch(line)
			if match != nil {
				current, _ = strconv.Atoi(match[1])
			} else {
				match = sleepRE.FindStringSubmatch(line)
				if match != nil {
					start, _ = strconv.Atoi(match[1])
				} else {
					match = wakeRE.FindStringSubmatch(line)
					if match != nil {
						stop, _ := strconv.Atoi(match[1])

						napSlice, exists := naps[current]
						if !exists {
							napSlice = make([]Nap, 0)
							naps[current] = napSlice
						}
						naps[current] = append(napSlice, Nap{start: start, stop: stop})

					} else {
						log.Fatalf("Unexpected line %s\n", line)
					}
				}
			}
		}
		return naps
	}

	max := func(a []int) (int, int) {
		max := 0
		maxI := 0
		for i, n := range a {
			if a[i] > max {
				max = n
				maxI = i
			}
		}
		return max, maxI
	}

	sample :=
		`[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up
`
	samples1 := map[string]string{sample: "240"}
	part1 := func(in io.Reader) string {
		naps := parse(in)
		mostGuard := 0
		mostMinutes := 0
		for guard, napSlice := range naps {
			minutes := 0
			for _, n := range napSlice {
				minutes += n.stop - n.start
			}
			if minutes > mostMinutes {
				mostMinutes = minutes
				mostGuard = guard
			}
		}
		var minutes [60]int
		for _, n := range naps[mostGuard] {
			for i := n.start; i < n.stop; i++ {
				minutes[i] = minutes[i] + 1
			}
		}
		_, maxMinute := max(minutes[:])
		return strconv.Itoa(mostGuard * maxMinute)
	}

	samples2 := map[string]string{sample: "4455"}

	part2 := func(in io.Reader) string {
		napCount := make(map[int][]int)
		maxCount := 0
		maxGuard := 0
		maxMinute := 0
		for guard, naps := range parse(in) {
			napCount[guard] = make([]int, 60)
			for _, nap := range naps {
				for m := nap.start; m < nap.stop; m++ {
					count := napCount[guard][m] + 1
					if count > maxCount {
						maxCount = count
						maxGuard = guard
						maxMinute = m
					}
					minutes := napCount[guard]
					minutes[m] = count
				}
			}
		}
		return strconv.Itoa(maxGuard * maxMinute)
	}

	return Puzzle{"data/day4.txt", [2]Part{Part{samples1, part1}, Part{samples2, part2}}}
}

I'm not proud of that cascading regexp parser.