complete day 13 part 1

This commit is contained in:
Nico 2020-12-13 14:47:59 +00:00
parent 358d1ad23d
commit e94f26233d
3 changed files with 47 additions and 0 deletions

2
day13/input Normal file
View File

@ -0,0 +1,2 @@
1000066
13,x,x,41,x,x,x,37,x,x,x,x,x,659,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,23,x,x,x,x,x,29,x,409,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17

2
day13/input.example Normal file
View File

@ -0,0 +1,2 @@
939
7,13,x,x,59,x,31,19

43
day13/main.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"io/ioutil"
"strings"
"strconv"
)
type Bus struct {
Num int
Arrival int
}
func main() {
d, _ := ioutil.ReadFile("input")
lines := strings.Split(string(d),"\n")
departure, _ := strconv.Atoi(lines[0]) // Earliest departure time
busstrings := strings.Split(lines[1],",")
var busses []int
for _, b := range busstrings {
if b != "x" {
n, _ := strconv.Atoi(b)
busses = append(busses, n)
}
}
var bustimes []Bus
for _, b := range busses {
i := b
for i < departure {
i += b
}
bustimes = append(bustimes, Bus{Num: b, Arrival: i})
}
earliestBus := Bus{}
for _, bus := range bustimes {
if earliestBus.Num == 0 { // If we have the first real bus
earliestBus = bus
} else if earliestBus.Arrival > bus.Arrival {
earliestBus = bus
}
}
fmt.Println("Part 1:", earliestBus.Num * (earliestBus.Arrival - departure))
}