[2020-12-13] Add solutions for day13.01
parent
962a7b810b
commit
c5ea100f8e
@ -0,0 +1,2 @@
|
||||
939
|
||||
7,13,x,x,59,x,31,19
|
@ -0,0 +1,2 @@
|
||||
1002461
|
||||
29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,521,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,17,x,x,x,x,x,x,x,x,x,x,x,x,x,601,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,x,x,19
|
@ -0,0 +1,34 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.elis.nu/etu/aoc2020/utils"
|
||||
)
|
||||
|
||||
var schedule struct {
|
||||
Departure int
|
||||
Lines []int
|
||||
}
|
||||
|
||||
func ParseFile(input string) {
|
||||
// Parse file
|
||||
rows := utils.GetLinesFromFile("day13/" + input + ".txt")
|
||||
|
||||
// Convert time
|
||||
time, _ := strconv.Atoi(rows[0])
|
||||
|
||||
// Store time
|
||||
schedule.Departure = time
|
||||
|
||||
for _, row := range strings.Split(rows[1], ",") {
|
||||
if row != "x" {
|
||||
// Convert bus number
|
||||
line, _ := strconv.Atoi(row)
|
||||
|
||||
// Add to schedule
|
||||
schedule.Lines = append(schedule.Lines, line)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func Solve1() {
|
||||
// Big number to esaier spot smaller numbers
|
||||
lowestNextDeparture := schedule.Departure * 1000
|
||||
lowestNextDepartureLine := 0
|
||||
|
||||
// Look for next departure times for lines
|
||||
for _, line := range schedule.Lines {
|
||||
// Calculate next departure
|
||||
nextDeparture := schedule.Departure/line*line + line
|
||||
|
||||
// Look for lowest departure time
|
||||
if nextDeparture < lowestNextDeparture {
|
||||
lowestNextDeparture = nextDeparture
|
||||
lowestNextDepartureLine = line
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate next departure id
|
||||
nextDepartureId := (lowestNextDeparture - schedule.Departure) * lowestNextDepartureLine
|
||||
|
||||
log.Printf("2020-12-13.01: Answer: %d\n", nextDepartureId)
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func Solve2() {
|
||||
fmt.Println(len(schedule.Lines))
|
||||
|
||||
log.Printf("2020-12-13.02: Answer: %d\n", len(schedule.Lines))
|
||||
}
|
Loading…
Reference in New Issue