Browse Source

Day 13

master
Jan-Lukas Else 4 months ago
parent
commit
576b414008
2 changed files with 58 additions and 0 deletions
  1. +2
    -0
      13/input.txt
  2. +56
    -0
      13/main.go

+ 2
- 0
13/input.txt View File

@ -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

+ 56
- 0
13/main.go View File

@ -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)
}

Loading…
Cancel
Save