-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (66 loc) · 1.14 KB
/
main.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
)
func main() {
// Use Flags to run a part
methodP := flag.String("method", "p1", "The method/part that should be run, valid are p1,p2 and test")
flag.Parse()
switch *methodP {
case "p1":
PartOne()
break
case "p2":
PartTwo()
break
case "test":
break
}
}
func PartOne() {
input := readInput()
tf := 0
for _, value := range input {
mass, _ := strconv.Atoi(value)
if mass == 0 {
continue
}
tf += int(mass/3) - 2
}
fmt.Println("Fuel needed is", tf)
}
func PartTwo() {
input := readInput()
tf := 0
for _, value := range input {
mass, _ := strconv.Atoi(value)
f := int(mass/3) - 2
tf += f
for {
mass = f
fuelNeeded := int(mass/3) - 2
if fuelNeeded <= 0 {
break
} else {
f = fuelNeeded
tf += f
}
}
}
fmt.Println("Fuel needed is", tf)
}
// Read data from input.txt
// Return the string, so that we can deal with it however
func readInput() []string {
var input []string
f, _ := os.Open("input.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}