From 9c2d4c65faed9571e58aa0fe4d9c909fb0f1e601 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Mon, 2 Dec 2019 19:27:03 +0100 Subject: [PATCH] :sparkles: Day 1 Success --- day1/input.txt | 100 ++++++++++++++++++++++++++++++++++++++++++++++ day1/main.go | 54 +++++++++++++++++++++++++ day1/main_test.go | 52 ++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 day1/input.txt create mode 100644 day1/main.go create mode 100644 day1/main_test.go diff --git a/day1/input.txt b/day1/input.txt new file mode 100644 index 0000000..462aafb --- /dev/null +++ b/day1/input.txt @@ -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 \ No newline at end of file diff --git a/day1/main.go b/day1/main.go new file mode 100644 index 0000000..efb90a4 --- /dev/null +++ b/day1/main.go @@ -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 +} diff --git a/day1/main_test.go b/day1/main_test.go new file mode 100644 index 0000000..ad82031 --- /dev/null +++ b/day1/main_test.go @@ -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) + } +}