aboutsummaryrefslogtreecommitdiff
path: root/one.go
blob: cf27820265e431dec47868c8c07193307fc89fdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
}