Day 1: Simplify Rust and Go solutions

This commit is contained in:
kageru 2019-12-01 20:26:37 +01:00
parent 2f3e4c6576
commit e096d69ea7
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 9 additions and 16 deletions

View File

@ -11,26 +11,20 @@ func main() {
inFile, _ := ioutil.ReadFile("input")
lines := strings.Split(string(inFile), "\n")
fmt.Printf("Part 1: %d\n", part1(lines))
fmt.Printf("Part 2: %d\n", part2(lines))
part1, part2 := solve(lines)
fmt.Printf("Part 1: %d\n", part1)
fmt.Printf("Part 2: %d\n", part2)
}
func part1(lines []string) int {
func solve(lines []string) (int, int) {
fuel := 0
fuelRec := 0
for _, line := range lines {
mass, _ := strconv.Atoi(line)
fuel += cost(mass)
fuelRec += costRec(mass, 0)
}
return fuel
}
func part2(lines []string) int {
fuel := 0
for _, line := range lines {
mass, _ := strconv.Atoi(line)
fuel += costRec(mass, 0)
}
return fuel
return fuel, fuelRec
}
func cost(mass int) int {

View File

@ -24,9 +24,8 @@ fn cost(mass: u32) -> u32 {
}
fn cost_rec(mass: u32, acc: u32) -> u32 {
let c = cost(mass);
match c {
match cost(mass) {
0 => acc,
_ => cost_rec(c, acc + c)
c => cost_rec(c, acc + c)
}
}