[wip] [2020-12-13] Add solutions for day13.02
parent
c5ea100f8e
commit
f65fdd2e87
@ -1,12 +1,51 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func Solve2() {
|
||||
fmt.Println(len(schedule.Lines))
|
||||
// Storage if it's a valid run or not
|
||||
isValidRun := false
|
||||
|
||||
log.Printf("2020-12-13.02: Answer: %d\n", len(schedule.Lines))
|
||||
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])
|
||||
}
|
||||
|
Loading…
Reference in New Issue