Skip to content

Commit

Permalink
feat(1): december 1st
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinMcPhail committed Dec 3, 2019
0 parents commit 707d35b
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
100 changes: 100 additions & 0 deletions one/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
95065
129298
145573
95743
59139
78323
124445
69015
81990
83254
139274
92101
74245
104038
61955
80642
110376
89992
84392
117830
140144
80076
111285
107135
98741
103753
141922
130503
60409
73891
84781
118319
93610
143228
99616
65353
102388
123813
88335
95459
133635
108771
101999
73850
106490
53396
110330
140258
73958
60273
101401
128995
61495
114674
71955
107049
79374
52359
107925
91789
69174
133966
85063
62856
96965
97100
81638
104488
131368
59015
149357
65193
61489
126089
141224
100596
93144
109421
121988
135890
70141
53531
59900
98981
66796
113700
109535
100721
87240
99883
81637
80064
143154
75778
64835
59235
103907
121637
118525
125730
62 changes: 62 additions & 0 deletions one/one.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"io/ioutil"
"log"
"math"
"strconv"
"strings"
)

/*
--- Day 1: The Tyranny of the Rocket Equation ---
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
*/

func getFuelCost(m []string) int {
sum := 0
for _, module := range m {
mass, err := strconv.ParseInt(module, 10, 64)
if err != nil {
log.Fatalf("Something went wrong while parsing the mass: %s", err)
panic(err)
}

fuelCost := int(math.Floor(float64(mass) / 3)) - 2
for fuelCost > 0 {
sum += fuelCost
fuelCost = int(math.Floor(float64(fuelCost) / 3)) - 2
}
}
return sum
}

func main() {
moduleMasses, err := ioutil.ReadFile("input")
if err != nil {
log.Fatal("Something went wrong while reading the input file.")
panic(err)
}
mArr := strings.ReplaceAll(string(moduleMasses), "\r", "")
massArr := strings.Split(mArr, "\n")

println(getFuelCost(massArr))
}

0 comments on commit 707d35b

Please sign in to comment.