diff --git a/13/input.txt b/13/input.txt new file mode 100644 index 0000000..793ebb1 --- /dev/null +++ b/13/input.txt @@ -0,0 +1,2 @@ +1002561 +17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,409,x,29,x,x,x,x,x,x,x,x,x,x,13,x,x,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,373,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,19 \ No newline at end of file diff --git a/13/main.go b/13/main.go new file mode 100644 index 0000000..12d1066 --- /dev/null +++ b/13/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "strconv" + "strings" +) + +func main() { + var earliest int + var buslines []int + + file, _ := os.Open("input.txt") + defer file.Close() + scanner := bufio.NewScanner(file) + scanner.Scan() + earliest, _ = strconv.Atoi(scanner.Text()) + scanner.Scan() + for _, s := range strings.Split(scanner.Text(), ",") { + if s != "x" { + line, _ := strconv.Atoi(s) + buslines = append(buslines, line) + } else { + buslines = append(buslines, -1) + } + } + + favLine := 0 + waitTime := math.MaxInt64 + for _, l := range buslines { + if l != -1 { + if t := l - earliest%l; t < waitTime { + favLine = l + waitTime = t + } + } + } + + fmt.Println("Part 1:", favLine*waitTime) + + t, step := 0, 1 + for i, l := range buslines { + if l == -1 { + continue + } + for (t+i)%l != 0 { + t += step + } + step *= l + } + + fmt.Println("Part 2:", t) +}