part one works, part two... eh.

This commit is contained in:
David 2020-12-13 11:46:57 -05:00
parent 6ed45d319b
commit 559d91b9a6
1 changed files with 89 additions and 0 deletions

89
13/main.go Normal file
View File

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