From b52a39cceed8cc2b4f0ea89fd07d7d00e38f5e8b Mon Sep 17 00:00:00 2001 From: Karl Spickermann Date: Sun, 13 Dec 2020 15:52:15 +0100 Subject: [PATCH] Day13 --- day13/day13.go | 81 ++++++++++++++++++++++++++++++++++++++++++++ day13/day13Input.txt | 2 ++ day13/day13Test.txt | 2 ++ 3 files changed, 85 insertions(+) create mode 100644 day13/day13.go create mode 100644 day13/day13Input.txt create mode 100644 day13/day13Test.txt diff --git a/day13/day13.go b/day13/day13.go new file mode 100644 index 0000000..73cfdc2 --- /dev/null +++ b/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 +} diff --git a/day13/day13Input.txt b/day13/day13Input.txt new file mode 100644 index 0000000..7e74881 --- /dev/null +++ b/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 diff --git a/day13/day13Test.txt b/day13/day13Test.txt new file mode 100644 index 0000000..e473080 --- /dev/null +++ b/day13/day13Test.txt @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 \ No newline at end of file