Skip to content

Commit f44f85e

Browse files
committed
Solve 2019 - Day 1
Signed-off-by: Alexander Kurbatov <sir.alkurbatov@yandex.ru>
1 parent 78dbb07 commit f44f85e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

2019/1/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// --- Day 1: The Tyranny of the Rocket Equation ---
2+
// https://adventofcode.com/2019/day/1
3+
package main
4+
5+
import (
6+
"bufio"
7+
"log"
8+
"os"
9+
"strconv"
10+
)
11+
12+
func readInput(path string) ([]int, error) {
13+
input, err := os.Open(path)
14+
if err != nil {
15+
return nil, err
16+
}
17+
defer input.Close()
18+
19+
output := make([]int, 0)
20+
scanner := bufio.NewScanner(input)
21+
22+
for scanner.Scan() {
23+
value, err := strconv.Atoi(scanner.Text())
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
output = append(output, value)
29+
}
30+
31+
return output, scanner.Err()
32+
}
33+
34+
func calcFuel(mass int) int {
35+
return max(mass/3-2, 0)
36+
}
37+
38+
func calcFuelWithAdditionalFuel(mass int) int {
39+
result := 0
40+
41+
for mass > 0 {
42+
fuel := calcFuel(mass)
43+
44+
mass = fuel
45+
result += fuel
46+
}
47+
48+
return result
49+
}
50+
51+
func main() {
52+
input, err := readInput("./2019/1/input.txt")
53+
if err != nil {
54+
log.Printf("Failed to read data from input.txt: %v", err)
55+
return
56+
}
57+
58+
var firstPartResult, secondPartResult int
59+
60+
for _, mass := range input {
61+
firstPartResult += calcFuel(mass)
62+
secondPartResult += calcFuelWithAdditionalFuel(mass)
63+
}
64+
65+
log.Printf("Part 1 result: %d", firstPartResult)
66+
log.Printf("Part 2 result: %d", secondPartResult)
67+
}

0 commit comments

Comments
 (0)