Day 1 Success

This commit is contained in:
Alphyron 2019-12-02 19:27:03 +01:00
parent aee622018c
commit 9c2d4c65fa
3 changed files with 206 additions and 0 deletions

100
day1/input.txt Normal file
View File

@ -0,0 +1,100 @@
101005
139223
112833
70247
131775
106730
118388
138683
80439
71060
120862
67201
70617
79783
114813
77907
78814
107515
113507
81865
88130
75120
66588
56023
98080
128472
96031
118960
54069
112000
62979
105518
73342
52270
128841
68267
70789
94792
100738
102331
83082
77124
97360
86165
66120
139042
50390
105308
94607
58225
77894
118906
127277
101446
58897
93876
53312
117154
77448
62041
99069
87375
134854
108561
126406
53809
90760
121650
79573
134734
148021
84263
54390
132706
148794
67302
146885
76108
76270
54548
146920
145282
129509
144139
141713
62547
149898
96746
83583
107758
63912
142036
112281
91775
75809
82250
144667
140140
98276
103479

54
day1/main.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"bufio"
"log"
"os"
"strconv"
)
func main() {
file, err := os.Open("input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
completeFuel := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
txt := scanner.Text()
convert, err := strconv.ParseFloat(txt, 64)
if err != nil {
log.Fatal(err)
}
completeFuel += CalcFuelRecusive(convert)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
log.Println(completeFuel)
}
func CalcFuelRecusive(mass float64) int {
sum := 0
for mass > 0 {
fuel := CalcRequiredFuel(mass)
if fuel > 0 {
sum += fuel
}
mass = float64(fuel)
}
return sum
}
func CalcRequiredFuel(mass float64) int {
fuel := mass / 3
return int(fuel) - 2
}

52
day1/main_test.go Normal file
View File

@ -0,0 +1,52 @@
package main
import "testing"
func TestCalcRequiredFuel1(t *testing.T) {
total := CalcRequiredFuel(12)
if total != 2 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 2)
}
}
func TestCalcRequiredFuel2(t *testing.T) {
total := CalcRequiredFuel(14)
if total != 2 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 2)
}
}
func TestCalcRequiredFuel3(t *testing.T) {
total := CalcRequiredFuel(1968)
if total != 654 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 654)
}
}
func TestCalcRequiredFuel4(t *testing.T) {
total := CalcRequiredFuel(100756)
if total != 33583 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 33583)
}
}
func TestCalcFuelRecusive1(t *testing.T) {
total := CalcFuelRecusive(14)
if total != 2 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 2)
}
}
func TestCalcFuelRecusive2(t *testing.T) {
total := CalcFuelRecusive(100756)
if total != 50346 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 50346)
}
}
func TestCalcFuelRecusive3(t *testing.T) {
total := CalcFuelRecusive(1969)
if total != 966 {
t.Errorf("CalcRequiredFuel was incorrect, got: %d, want: %d.", total, 966)
}
}