123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "strconv"
- "strings"
- )
- type point struct {
- min, max int
- }
- func readInput(file string) []point {
- content, err := ioutil.ReadFile(file)
- if err != nil {
- log.Fatal(err)
- }
- var points []point
- line := string(content)
- initialSplit := strings.Split(line, ", ")
- if len(initialSplit) != 2 {
- log.Fatal("Invalid input")
- }
- firstPartSplit := strings.Split(initialSplit[0], "=")
- if len(firstPartSplit) != 2 {
- log.Fatal("Invalid input")
- }
- firstPartNumbers := strings.Split(firstPartSplit[1], "..")
- if len(firstPartNumbers) != 2 {
- log.Fatal("Invalid input")
- }
- minX, err := strconv.Atoi(firstPartNumbers[0])
- if err != nil {
- log.Fatal(err)
- }
- maxX, err := strconv.Atoi(firstPartNumbers[1])
- if err != nil {
- log.Fatal(err)
- }
- points = append(points, point{minX, maxX})
- secondPartNumbers := strings.Split(initialSplit[1], "..")
- if len(secondPartNumbers) != 2 {
- log.Fatal("Invalid input")
- }
- minY, err := strconv.Atoi(strings.TrimLeft(secondPartNumbers[0], "y="))
- if err != nil {
- log.Fatal(err)
- }
- maxY, err := strconv.Atoi(secondPartNumbers[1])
- if err != nil {
- log.Fatal(err)
- }
- points = append(points, point{minY, maxY})
- return points
- }
- func findMinXVelocity(xPoint point) int {
- for i := 1; i < xPoint.min; i++ {
- v := i
- x := 0
- for {
- x += v
- if v > 0 {
- v--
- }
- if v == 0 {
- break
- }
- }
- if x >= xPoint.min {
- return i
- }
- }
- return 0
- }
- func main() {
- if len(os.Args) < 2 {
- log.Fatal("Please provide a file name as argument")
- }
- input := readInput(os.Args[1])
- fmt.Println(findMinXVelocity(input[0]))
- }
|