From fd70759166d6da2c65622ec7dff80390f6d17205 Mon Sep 17 00:00:00 2001 From: j-james Date: Tue, 23 Jun 2020 11:35:38 -0700 Subject: Day One --- one.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 one.go diff --git a/one.go b/one.go new file mode 100644 index 0000000..cf27820 --- /dev/null +++ b/one.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + start, sum, fuelsum := 0, 0, 0 + if len(os.Args) < 2 { + panic("runtime error: missing operand") + } + file, err := ioutil.ReadFile(os.Args[1]) + if err != nil { + panic(err) + } + for i := 0; i < len(file); i++ { + if file[i] == '\n' { + arg, err := strconv.Atoi(string(file[start:i])) // i-1?? + if err != nil { + panic(err) + } + start = i + 1 + sum += (arg / 3) - 2 + fuelsum += tyranny(arg) + } + } + fmt.Println(sum) + fmt.Println(fuelsum) +} + +func tyranny(mass int) int { + subtotal := 0 + for fuel := (mass / 3) - 2; fuel >= 0; fuel = (fuel / 3) - 2 { + subtotal += fuel + } + return subtotal +} -- cgit v1.2.3-70-g09d2