mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
cleanup effort pt 1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -10,7 +11,16 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
var part int
|
||||
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
ans := part1(util.ReadFile("./input.txt"), part)
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
func part1(input string, part int) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
// sort inputs by time stamp, string sorting is sufficient
|
||||
@@ -24,6 +34,8 @@ func main() {
|
||||
// find which guard has slept the most total time
|
||||
// then find which minute he is asleep at most frequently
|
||||
mapIDGuard := make(map[int]*guard)
|
||||
// for part 2, track each guard for the actual minute asleep
|
||||
mapIDToMinutesArray := make(map[int]*[60]int)
|
||||
lastGuardID := timeEntries[0].ID
|
||||
for i, timeEntry := range timeEntries {
|
||||
if timeEntry.ID != 0 {
|
||||
@@ -39,40 +51,60 @@ func main() {
|
||||
if mapIDGuard[lastGuardID] == nil {
|
||||
mapIDGuard[lastGuardID] = &guard{}
|
||||
}
|
||||
// part 2 parsing
|
||||
if mapIDToMinutesArray[lastGuardID] == nil {
|
||||
mapIDToMinutesArray[lastGuardID] = &[60]int{}
|
||||
}
|
||||
|
||||
mapIDGuard[lastGuardID].totalTimeAsleep += endTime - startTime
|
||||
for startTime < endTime {
|
||||
mapIDGuard[lastGuardID].minutesAsleep[startTime]++
|
||||
mapIDToMinutesArray[lastGuardID][startTime]++
|
||||
startTime++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// who sleeps the most
|
||||
var IDOfSleepiestGuard, bestMinute, highestFreq int
|
||||
for i, g := range mapIDGuard {
|
||||
if IDOfSleepiestGuard == 0 {
|
||||
IDOfSleepiestGuard = i
|
||||
if part == 1 {
|
||||
// who sleeps the most
|
||||
var IDOfSleepiestGuard, bestMinute, highestFreq int
|
||||
for i, g := range mapIDGuard {
|
||||
if IDOfSleepiestGuard == 0 {
|
||||
IDOfSleepiestGuard = i
|
||||
}
|
||||
if g.totalTimeAsleep > mapIDGuard[IDOfSleepiestGuard].totalTimeAsleep {
|
||||
IDOfSleepiestGuard = i
|
||||
}
|
||||
}
|
||||
if g.totalTimeAsleep > mapIDGuard[IDOfSleepiestGuard].totalTimeAsleep {
|
||||
IDOfSleepiestGuard = i
|
||||
|
||||
// find the minute they are the asleep the most
|
||||
for min, freq := range mapIDGuard[IDOfSleepiestGuard].minutesAsleep {
|
||||
if freq > highestFreq {
|
||||
bestMinute = min
|
||||
highestFreq = freq
|
||||
}
|
||||
}
|
||||
|
||||
// print ID * time (minute)
|
||||
return IDOfSleepiestGuard * bestMinute
|
||||
}
|
||||
|
||||
// part 2 stuff
|
||||
var highestFreq, ID, bestMinute int
|
||||
// find the minute they are the asleep the most
|
||||
for min, freq := range mapIDGuard[IDOfSleepiestGuard].minutesAsleep {
|
||||
if freq > highestFreq {
|
||||
bestMinute = min
|
||||
highestFreq = freq
|
||||
for i, arr := range mapIDToMinutesArray {
|
||||
for min, freq := range arr {
|
||||
if freq > highestFreq {
|
||||
bestMinute = min
|
||||
highestFreq = freq
|
||||
ID = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print ID * time (minute)
|
||||
fmt.Printf("Sleepiest guard is %v, at minute %v.\ngithub.com/alexchao26/advent-of-code-go answer: %v\n",
|
||||
IDOfSleepiestGuard,
|
||||
bestMinute,
|
||||
IDOfSleepiestGuard*bestMinute,
|
||||
)
|
||||
return ID * bestMinute
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
@@ -81,7 +113,6 @@ type entry struct {
|
||||
}
|
||||
|
||||
func makeEntry(line string) entry {
|
||||
fmt.Println(line)
|
||||
var e entry
|
||||
e.year, _ = strconv.Atoi(line[1:5])
|
||||
e.month, _ = strconv.Atoi(line[6:8])
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_part1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
part int
|
||||
want int
|
||||
}{
|
||||
{"actual", util.ReadFile("input.txt"), 1, 38813},
|
||||
{"actual", util.ReadFile("input.txt"), 2, 141071},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := part1(tt.input, tt.part); got != tt.want {
|
||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
// sort inputs by time stamp, string sorting is sufficient
|
||||
sort.Strings(lines)
|
||||
|
||||
timeEntries := make([]entry, len(lines))
|
||||
for i, line := range lines {
|
||||
timeEntries[i] = makeEntry(line)
|
||||
}
|
||||
|
||||
// find which guard has slept the most total time
|
||||
// then find which minute he is asleep at most frequently
|
||||
mapIDGuard := make(map[int]*[60]int)
|
||||
lastGuardID := timeEntries[0].ID
|
||||
for i, timeEntry := range timeEntries {
|
||||
if timeEntry.ID != 0 {
|
||||
lastGuardID = timeEntry.ID
|
||||
} else {
|
||||
// if the time entry is awake, then check the next one entry
|
||||
// if the next entry is the same day, then assume its the same guard
|
||||
// update that guard's stats
|
||||
if !timeEntry.awake && i+1 != len(timeEntries) &&
|
||||
timeEntries[i+1].day == timeEntry.day {
|
||||
endTime := timeEntries[i+1].minute
|
||||
startTime := timeEntry.minute
|
||||
if mapIDGuard[lastGuardID] == nil {
|
||||
mapIDGuard[lastGuardID] = &[60]int{}
|
||||
}
|
||||
for startTime < endTime {
|
||||
mapIDGuard[lastGuardID][startTime]++
|
||||
startTime++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var highestFreq, ID, bestMinute int
|
||||
// find the minute they are the asleep the most
|
||||
for i, arr := range mapIDGuard {
|
||||
for min, freq := range arr {
|
||||
if freq > highestFreq {
|
||||
bestMinute = min
|
||||
highestFreq = freq
|
||||
ID = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print ID * time (minute)
|
||||
fmt.Printf("Guard %v is asleep the most at minute %v.\ngithub.com/alexchao26/advent-of-code-go answer: %v\n",
|
||||
ID,
|
||||
bestMinute,
|
||||
ID*bestMinute,
|
||||
)
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
ID, year, month, day, minute int
|
||||
awake bool
|
||||
}
|
||||
|
||||
func makeEntry(line string) entry {
|
||||
fmt.Println(line)
|
||||
var e entry
|
||||
e.year, _ = strconv.Atoi(line[1:5])
|
||||
e.month, _ = strconv.Atoi(line[6:8])
|
||||
e.day, _ = strconv.Atoi(line[9:11])
|
||||
|
||||
hour, _ := strconv.Atoi(line[12:14])
|
||||
// if started before midnight, zero out minute value
|
||||
if hour != 0 {
|
||||
e.minute = 0
|
||||
} else {
|
||||
e.minute, _ = strconv.Atoi(line[15:17])
|
||||
}
|
||||
|
||||
// if the instruction is that the guard has fallen asleep, leave awake=false
|
||||
if strings.Contains(line, "falls asleep") {
|
||||
return e
|
||||
}
|
||||
|
||||
e.awake = true
|
||||
if strings.Contains(line, "Guard") {
|
||||
e.ID, _ = strconv.Atoi(line[strings.Index(line, "#")+1 : strings.Index(line, " begins")])
|
||||
}
|
||||
return e
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
--- Day 4: Repose Record ---
|
||||
You've sneaked into another supply closet - this time, it's across from the prototype suit manufacturing lab. You need to sneak inside and fix the issues with the suit, but there's a guard stationed outside the lab, so this is as close as you can safely get.
|
||||
|
||||
As you search the closet for anything that might help, you discover that you're not the first person to want to sneak in. Covering the walls, someone has spent an hour starting every midnight for the past few months secretly observing this guard post! They've been writing down the ID of the one guard on duty that night - the Elves seem to have decided that one guard was enough for the overnight shift - as well as when they fall asleep or wake up while at their post (your puzzle input).
|
||||
|
||||
For example, consider the following records, which have already been organized into chronological order:
|
||||
|
||||
[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
|
||||
Timestamps are written using year-month-day hour:minute format. The guard falling asleep or waking up is always the one whose shift most recently started. Because all asleep/awake times are during the midnight hour (00:00 - 00:59), only the minute portion (00 - 59) is relevant for those events.
|
||||
|
||||
Visually, these records show that the guards are asleep at these times:
|
||||
|
||||
Date ID Minute
|
||||
000000000011111111112222222222333333333344444444445555555555
|
||||
012345678901234567890123456789012345678901234567890123456789
|
||||
11-01 #10 .....####################.....#########################.....
|
||||
11-02 #99 ........................................##########..........
|
||||
11-03 #10 ........................#####...............................
|
||||
11-04 #99 ....................................##########..............
|
||||
11-05 #99 .............................................##########.....
|
||||
The columns are Date, which shows the month-day portion of the relevant day; ID, which shows the guard on duty that day; and Minute, which shows the minutes during which the guard was asleep within the midnight hour. (The Minute column's header shows the minute's ten's digit in the first row and the one's digit in the second row.) Awake is shown as ., and asleep is shown as #.
|
||||
|
||||
Note that guards count as asleep on the minute they fall asleep, and they count as awake on the minute they wake up. For example, because Guard #10 wakes up at 00:25 on 1518-11-01, minute 25 is marked as awake.
|
||||
|
||||
If you can figure out the guard most likely to be asleep at a specific time, you might be able to trick that guard into working tonight so you can have the best chance of sneaking in. You have two strategies for choosing the best guard/minute combination.
|
||||
|
||||
Strategy 1: Find the guard that has the most minutes asleep. What minute does that guard spend asleep the most?
|
||||
|
||||
In the example above, Guard #10 spent the most minutes asleep, a total of 50 minutes (20+25+5), while Guard #99 only slept for a total of 30 minutes (10+10+10). Guard #10 was asleep most during minute 24 (on two days, whereas any other minute the guard was asleep was only seen on one day).
|
||||
|
||||
While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.
|
||||
|
||||
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.)
|
||||
|
||||
Your puzzle answer was 38813.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
|
||||
--- Part Two ---
|
||||
Strategy 2: Of all guards, which guard is most frequently asleep on the same minute?
|
||||
|
||||
In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
|
||||
|
||||
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
|
||||
Reference in New Issue
Block a user