Browse Source

[2020-12-13] Add solutions for day13.01

master
Elis Hirwing 9 months ago
parent
commit
c5ea100f8e
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 2
      day13/example.txt
  3. 2
      day13/input.txt
  4. 34
      day13/parse.go
  5. 28
      day13/solve1.go
  6. 12
      day13/solve2.go
  7. 4
      main.go

2
Makefile

@ -39,6 +39,8 @@ day12:
go run main.go day12 input
day13:
go run main.go day13 input
day14:
day15:
day16:

2
day13/example.txt

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

2
day13/input.txt

@ -0,0 +1,2 @@
1002461
29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,521,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,17,x,x,x,x,x,x,x,x,x,x,x,x,x,601,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,x,x,19

34
day13/parse.go

@ -0,0 +1,34 @@
package day13
import (
"strconv"
"strings"
"git.elis.nu/etu/aoc2020/utils"
)
var schedule struct {
Departure int
Lines []int
}
func ParseFile(input string) {
// Parse file
rows := utils.GetLinesFromFile("day13/" + input + ".txt")
// Convert time
time, _ := strconv.Atoi(rows[0])
// Store time
schedule.Departure = time
for _, row := range strings.Split(rows[1], ",") {
if row != "x" {
// Convert bus number
line, _ := strconv.Atoi(row)
// Add to schedule
schedule.Lines = append(schedule.Lines, line)
}
}
}

28
day13/solve1.go

@ -0,0 +1,28 @@
package day13
import (
"log"
)
func Solve1() {
// Big number to esaier spot smaller numbers
lowestNextDeparture := schedule.Departure * 1000
lowestNextDepartureLine := 0
// Look for next departure times for lines
for _, line := range schedule.Lines {
// Calculate next departure
nextDeparture := schedule.Departure/line*line + line
// Look for lowest departure time
if nextDeparture < lowestNextDeparture {
lowestNextDeparture = nextDeparture
lowestNextDepartureLine = line
}
}
// Calculate next departure id
nextDepartureId := (lowestNextDeparture - schedule.Departure) * lowestNextDepartureLine
log.Printf("2020-12-13.01: Answer: %d\n", nextDepartureId)
}

12
day13/solve2.go

@ -0,0 +1,12 @@
package day13
import (
"fmt"
"log"
)
func Solve2() {
fmt.Println(len(schedule.Lines))
log.Printf("2020-12-13.02: Answer: %d\n", len(schedule.Lines))
}

4
main.go

@ -16,6 +16,7 @@ import (
"git.elis.nu/etu/aoc2020/day10"
"git.elis.nu/etu/aoc2020/day11"
"git.elis.nu/etu/aoc2020/day12"
"git.elis.nu/etu/aoc2020/day13"
"git.elis.nu/etu/aoc2020/utils"
)
@ -60,5 +61,8 @@ func main() {
case "day12":
utils.Perf("2020-12-12", day12.ParseFile, day12.Solve1, day12.Solve2)
case "day13":
utils.Perf("2020-12-13", day13.ParseFile, day13.Solve1, day13.Solve2)
}
}

Loading…
Cancel
Save