Browse Source

Day13

master
Karl Spickermann 1 year ago
parent
commit
b52a39ccee
  1. 81
      day13/day13.go
  2. 2
      day13/day13Input.txt
  3. 2
      day13/day13Test.txt

81
day13/day13.go

@ -0,0 +1,81 @@
package main
import (
"AoC2020/helper"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
part1(input)
part2(input)
}
func checkCorrect(time int, bustimes []int) bool {
p := 0
fmt.Print(bustimes)
for _, bustime := range bustimes {
if time%bustime > p {
return false
}
p = time % bustime
}
return true
}
func part2(input []string) {
busses := strings.Split(input[1], ",")
var busTimes []int
for _, val := range busses {
if val != "x" {
busTime, _ := strconv.Atoi(val)
busTimes = append(busTimes, busTime)
} else {
busTimes = append(busTimes, 0)
}
}
step := busTimes[0]
time := 0
for i, val := range busTimes[1:] {
if val != 0 {
fmt.Printf("Value: %d Step: %d \n",val,step)
for (time+i+1)%val != 0 {
time += step
}
step = step * val
}
}
fmt.Println(time)
}
func part1(input []string) {
arrival, _ := strconv.Atoi(input[0])
busses := strings.Split(input[1], ",")
minWaitTime := 999
choosenBus := 0
for _, val := range busses {
if val != "x" {
busTime, _ := strconv.Atoi(val)
diff := arrival / busTime
waitTime := busTime*(diff+1) - arrival
if minWaitTime > waitTime {
minWaitTime = waitTime
choosenBus = busTime
}
}
}
fmt.Println(minWaitTime*choosenBus)
}
func timestampToMinutes(timeStamp string) int {
hours := int(timeStamp[0] - '0')
minutes, _ := strconv.Atoi(string(timeStamp[1:]))
return hours*60 + minutes
}

2
day13/day13Input.txt

@ -0,0 +1,2 @@
1000390
23,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,383,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,19,x,x,x,x,x,x,x,x,x,29,x,503,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37

2
day13/day13Test.txt

@ -0,0 +1,2 @@
939
7,13,x,x,59,x,31,19
Loading…
Cancel
Save