mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 03:36:39 +02:00
Restyled README and file structure
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
41
2019/day_01/day_01.go
Normal file
41
2019/day_01/day_01.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func requiredFuel(mass int) int {
|
||||
// Part 1
|
||||
fuelReq := (mass / 3) - 2
|
||||
// Part 2
|
||||
if fuelReq <= 0 {
|
||||
return 0
|
||||
}
|
||||
return fuelReq + requiredFuel(fuelReq)
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := os.Open("./input")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var fuelReq int = 0
|
||||
|
||||
scan := bufio.NewScanner(file)
|
||||
|
||||
for scan.Scan() {
|
||||
mass, err := strconv.Atoi(scan.Text())
|
||||
if err != nil {
|
||||
log.Printf("Error converting line to int: %v", err)
|
||||
} else {
|
||||
fuelReq += requiredFuel(mass)
|
||||
}
|
||||
}
|
||||
fmt.Println(fuelReq)
|
||||
}
|
Reference in New Issue
Block a user