2018 Day 4 complete
This commit is contained in:
parent
036ee59bf7
commit
797bb1d62e
152
2018/day04/day04.go
Normal file
152
2018/day04/day04.go
Normal file
@ -0,0 +1,152 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var guards map[int]*Guard
|
||||
|
||||
func main() {
|
||||
inp := StdinToStringSlice()
|
||||
buildGuardMap(inp)
|
||||
part1()
|
||||
part2()
|
||||
}
|
||||
|
||||
func buildGuardMap(inp []string) {
|
||||
sort.Strings(inp)
|
||||
guards = make(map[int]*Guard)
|
||||
var currGuard int
|
||||
var sleepStart time.Time
|
||||
for _, v := range inp {
|
||||
t, _ := time.Parse("2006-01-02 15:04", v[1:17])
|
||||
actionParts := strings.Split(v[19:], " ")
|
||||
switch actionParts[0][0] {
|
||||
case 'G':
|
||||
currGuard = Atoi(actionParts[1][1:])
|
||||
case 'f':
|
||||
sleepStart = t
|
||||
case 'w':
|
||||
if _, ok := guards[currGuard]; !ok {
|
||||
guards[currGuard] = &Guard{
|
||||
id: currGuard,
|
||||
totalSleepTime: 0,
|
||||
sleepLog: make([]Nap, 0, 5),
|
||||
}
|
||||
}
|
||||
guards[currGuard].TakeNap(sleepStart, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func part1() {
|
||||
var sleepiest int
|
||||
var sleepiestTime int
|
||||
for k, v := range guards {
|
||||
if v.TotalNapTime() > sleepiestTime {
|
||||
sleepiest = k
|
||||
sleepiestTime = v.TotalNapTime()
|
||||
}
|
||||
}
|
||||
min, _ := guards[sleepiest].GetSleepiestMinute()
|
||||
fmt.Println("= Part 1 =")
|
||||
fmt.Println("Sleepiest Guard:", sleepiest, "(", sleepiestTime, ") Sleepiest Minute:", min)
|
||||
fmt.Println("= Answer =")
|
||||
fmt.Println(sleepiest * min)
|
||||
}
|
||||
|
||||
func part2() {
|
||||
var sleepiestTime int
|
||||
type tuple struct {
|
||||
v1, v2 int
|
||||
}
|
||||
sleepiestMinutes := make(map[int]tuple)
|
||||
for _, v := range guards {
|
||||
v1, v2 := v.GetSleepiestMinute()
|
||||
sleepiestMinutes[v.id] = tuple{v1, v2}
|
||||
if v.TotalNapTime() > sleepiestTime {
|
||||
sleepiestTime = v.TotalNapTime()
|
||||
}
|
||||
}
|
||||
var highestCount, highestCountGuard, highestCountMinute int
|
||||
for k, v := range sleepiestMinutes {
|
||||
if v.v2 > highestCount {
|
||||
highestCount = v.v2
|
||||
highestCountGuard = k
|
||||
highestCountMinute = v.v1
|
||||
}
|
||||
}
|
||||
fmt.Println("= Part 2 =")
|
||||
fmt.Printf("Guard: %d\nMinute: %d\nCount: %d\n", highestCountGuard, highestCountMinute, highestCount)
|
||||
fmt.Println("= Answer =")
|
||||
fmt.Println(highestCountGuard * highestCountMinute)
|
||||
}
|
||||
|
||||
type Guard struct {
|
||||
id int
|
||||
totalSleepTime int
|
||||
sleepLog []Nap
|
||||
}
|
||||
|
||||
func (g *Guard) TakeNap(start, end time.Time) {
|
||||
g.sleepLog = append(
|
||||
g.sleepLog,
|
||||
Nap{start: start, end: end})
|
||||
}
|
||||
|
||||
func (g *Guard) TotalNapTime() int {
|
||||
var ret int
|
||||
for _, v := range g.sleepLog {
|
||||
ret += int(v.end.Sub(v.start).Minutes())
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (g *Guard) GetSleepiestMinute() (int, int) {
|
||||
minuteMap := make(map[int]int)
|
||||
for _, v := range g.sleepLog {
|
||||
wrkMin := v.start.Minute()
|
||||
for wrkMin < v.end.Minute() {
|
||||
minuteMap[wrkMin]++
|
||||
wrkMin++
|
||||
}
|
||||
}
|
||||
var sleepiestMinute, sleepiestCount int
|
||||
for k, v := range minuteMap {
|
||||
if v > sleepiestCount {
|
||||
sleepiestMinute = k
|
||||
sleepiestCount = v
|
||||
}
|
||||
}
|
||||
return sleepiestMinute, minuteMap[sleepiestMinute]
|
||||
}
|
||||
|
||||
type Nap struct {
|
||||
start time.Time
|
||||
end time.Time
|
||||
}
|
||||
|
||||
func StdinToStringSlice() []string {
|
||||
var input []string
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
input = append(input, scanner.Text())
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func Atoi(i string) int {
|
||||
var ret int
|
||||
var err error
|
||||
if ret, err = strconv.Atoi(i); err != nil {
|
||||
log.Fatal("Invalid Atoi")
|
||||
}
|
||||
return ret
|
||||
}
|
1
2018/day04/go.mod
Normal file
1
2018/day04/go.mod
Normal file
@ -0,0 +1 @@
|
||||
module day04
|
1088
2018/day04/input
Normal file
1088
2018/day04/input
Normal file
File diff suppressed because it is too large
Load Diff
17
2018/day04/testinput
Normal file
17
2018/day04/testinput
Normal file
@ -0,0 +1,17 @@
|
||||
[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
|
Loading…
Reference in New Issue
Block a user