Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.8 kB
3
Indexable
Never
package main

import (
        "bufio"
        "flag"
        "fmt"
        "os"
        "strconv"
        "strings"
)

var input = flag.String("input", "real", "input file data")

func read() *bufio.Scanner {
        f, err := os.Open(*input)
        if err != nil {
                panic(err)

        }
        return bufio.NewScanner(f)

}

func main() {
        flag.Parse()
        data := read()
        var time, distance []string
        for data.Scan() {
                line := strings.Split(data.Text(), ":")
                switch line[0] {
                case "Time":
                        time = strings.Fields(line[1])
                case "Distance":
                        distance = strings.Fields(line[1])
                }
        }
        times := make([]int, len(time))
        distances := make([]int, len(distance))
        for i, s := range time {
                times[i], _ = strconv.Atoi(s)
        }

        for i, s := range distance {
                distances[i], _ = strconv.Atoi(s)
        }

        fmt.Println(times, distances)

        var held int
        var totals []int
        for race, t := range times {
                ways := 0
                for i := 1; i <= t; i++ {
                        total := t
                        held = i
                        remain := total - held
                        if remain >= t {
                                continue
                        }
                        covered := remain * held
                        if covered > distances[race] {
                                ways++
                        }
                }
                totals = append(totals, ways)
        }
        final := 1
        for _, t := range totals {
                final = final * t
        }
        fmt.Println(final)
}
Leave a Comment