From 559d91b9a6c6743789988bb38f1373aef4937cea Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sun, 13 Dec 2020 11:46:57 -0500 Subject: [PATCH] part one works, part two... eh. --- 13/main.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 13/main.go diff --git a/13/main.go b/13/main.go new file mode 100644 index 0000000..13cfc71 --- /dev/null +++ b/13/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "strconv" + "strings" + "time" +) + +func main() { + start := time.Now() + partOne() + duration := time.Since(start) + partTwo() + duration2 := time.Since(start) + fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) +} + +func partOne() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + busWaitTimes := map[int]int{} + shortestWait := -1 + + // don't actually need loop logic for a two-line input + scanner.Scan() + ts := scanner.Text() + currentTimestamp, _ := strconv.Atoi(ts) + + scanner.Scan() + buses := strings.Split(scanner.Text(), ",") + for _, b := range buses { + if b != "x" { + j, _ := strconv.Atoi(b) + busWaitTimes[j] = (((currentTimestamp / j) + 1) * j) - currentTimestamp + if shortestWait == -1 || busWaitTimes[shortestWait] > busWaitTimes[j] { + shortestWait = j + } + } + } + + fmt.Println(busWaitTimes[shortestWait] * shortestWait) +} + +type bus struct { + route float64 + offset float64 +} + +func partTwo() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + buses := []bus{} + + scanner.Scan() // discard first line + scanner.Scan() + line := strings.Split(scanner.Text(), ",") + for i, b := range line { + if b != "x" { + j, _ := strconv.Atoi(b) + buses = append(buses, bus{ + route: float64(j), + offset: float64(i), + }) + } + } + + // walking the list works fine for the small test inputs but takes far too long on the real schedule + t := float64(0) + for { + loop: + t = t + buses[0].route + for i := range buses { + if math.Remainder(t+buses[i].offset, buses[i].route) != 0 { + goto loop + } + } + break + } + + fmt.Println(int(t)) +}