1
0
Fork 0
mirror of https://github.com/Luzifer/aoc2019.git synced 2024-12-21 21:41:16 +00:00

Add solution for Day 1

This commit is contained in:
Knut Ahlers 2019-12-01 14:28:58 +01:00
commit d2a16c6281
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 203 additions and 0 deletions

59
day01.go Normal file
View file

@ -0,0 +1,59 @@
package aoc2019
import (
"bufio"
"os"
"strconv"
"github.com/pkg/errors"
)
func calculateDay1FuelForMass(mass int64) int64 {
return mass/3 - 2
}
func calculateDay1FuelForMassRecurse(mass int64) int64 {
sumFuel := mass/3 - 2
var addFuel = calculateDay1FuelForMass(sumFuel)
for addFuel > 0 {
sumFuel += addFuel
addFuel = calculateDay1FuelForMass(addFuel)
}
return sumFuel
}
func solveDay1FromInput(inFile string, sumFn func(int64) int64) (int64, error) {
f, err := os.Open(inFile)
if err != nil {
return 0, errors.Wrap(err, "Unable to open input file")
}
defer f.Close()
var sumFuel int64
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if scanner.Err() != nil {
return 0, errors.Wrap(err, "Unable to scan file")
}
mass, err := strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "Unable to parse integer input %q", scanner.Text())
}
sumFuel += sumFn(mass)
}
return sumFuel, nil
}
func solveDay1Part1(inFile string) (int64, error) {
return solveDay1FromInput(inFile, calculateDay1FuelForMass)
}
func solveDay1Part2(inFile string) (int64, error) {
return solveDay1FromInput(inFile, calculateDay1FuelForMassRecurse)
}

100
day01_input.txt Normal file
View file

@ -0,0 +1,100 @@
62371
94458
78824
57296
84226
133256
101771
61857
120186
132234
50964
97800
81275
109561
145666
134029
81625
61963
83820
104210
62264
146376
91889
116069
54596
132877
70341
89983
84627
51617
84846
114416
132268
136516
104082
133669
86585
96389
111737
51954
132971
84097
66260
133883
84720
51985
61024
55912
125334
69541
58806
56014
62563
80799
67284
93971
147238
114830
61376
65096
73498
54792
88590
63225
129226
67872
55563
110467
91120
100281
148236
121886
75671
124736
90588
52175
140673
71029
73865
142021
140326
77894
61245
96492
136329
132967
83975
53082
56784
50024
131154
138517
130787
103334
104287
140644
148945
58945
62153
93488

44
day01_test.go Normal file
View file

@ -0,0 +1,44 @@
package aoc2019
import "testing"
func TestCalculateDay1_Examples(t *testing.T) {
for mass, expFuel := range map[int64]int64{
12: 2,
14: 2,
1969: 654,
100756: 33583,
} {
if f := calculateDay1FuelForMass(mass); f != expFuel {
t.Errorf("Mismatch for mass of %d, expected %d, got %d", mass, expFuel, f)
}
}
for mass, expFuel := range map[int64]int64{
14: 2,
1969: 966,
100756: 50346,
} {
if f := calculateDay1FuelForMassRecurse(mass); f != expFuel {
t.Errorf("Mismatch in recurse for mass of %d, expected %d, got %d", mass, expFuel, f)
}
}
}
func TestCalculateDay1_Part1(t *testing.T) {
fuel, err := solveDay1Part1("day01_input.txt")
if err != nil {
t.Fatalf("Day 1 solver failed: %s", err)
}
t.Logf("Solution Day 1 Part 1: %d", fuel)
}
func TestCalculateDay1_Part2(t *testing.T) {
fuel, err := solveDay1Part2("day01_input.txt")
if err != nil {
t.Fatalf("Day 1 solver failed: %s", err)
}
t.Logf("Solution Day 1 Part 2: %d", fuel)
}