You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
aoc2020/day13/solve2.go

51 lines
1.1 KiB

package day13
import (
"log"
)
func Solve2() {
// Storage if it's a valid run or not
isValidRun := false
i := 1
for isValidRun == false {
// Calculate the departure time for this first run
departureTime := i * schedule.Lines[0]
// Storage if it's a valid start or not
isValidStart := true
// Go throgh the rest of the lines
for offset, line := range schedule.Lines {
// Ignore first column which has no offset which we base
// this brute force on anyways.
if offset == 0 {
continue
}
// Calculate next departure time for this line
nextDeparture := departureTime/line*line + line
// If this doesn't match the rules to follow the offsets
// to go a minute after previous bus, break the loop and
// set it as an invalid start.
if departureTime+offset != nextDeparture {
isValidStart = false
break
}
}
if isValidStart {
isValidRun = true
}
i++
}
validRunId := i * schedule.Lines[0]
log.Printf("2020-12-13.02: Answer: %d\n", validRunId)
log.Printf("2020-12-13.02: Answer: %d\n", validRunId-schedule.Lines[0])
}