-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
D1: implement puzzle part 2 solution
- Loading branch information
Showing
7 changed files
with
96 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
inputFile, err := os.OpenFile("input", os.O_RDWR, 0666) | ||
if err != nil { | ||
log.Fatalf("error reading input data %v", err) | ||
} | ||
fmt.Printf("The elf with the most calories has %d calories\n", findMostCalories(inputFile)) | ||
inputFile.Seek(0, 0) | ||
top3 := findTopThree(inputFile) | ||
sum := 0 | ||
for _, n := range top3 { | ||
sum += n | ||
} | ||
fmt.Printf("The three elves with the most calories together have %d calories\n", sum) | ||
} | ||
|
||
func findMostCalories(input io.Reader) int { | ||
r := bufio.NewScanner(input) | ||
var maxSeen, calories int | ||
for r.Scan() { | ||
if r.Text() == "" { | ||
if calories > maxSeen { | ||
maxSeen = calories | ||
} | ||
calories = 0 | ||
} else { | ||
toAdd, err := strconv.Atoi(r.Text()) | ||
if err != nil { | ||
log.Fatalf("could not interpret data as calories: %v", err) | ||
} | ||
calories += toAdd | ||
} | ||
} | ||
return maxSeen | ||
} | ||
|
||
func findTopThree(input io.Reader) []int { | ||
r := bufio.NewScanner(input) | ||
var calories int | ||
top := []int{0, 0, 0} | ||
|
||
for r.Scan() { | ||
if r.Text() == "" { | ||
for i := range top { | ||
if calories > top[i] { | ||
tmp := top[i] | ||
top[i] = calories | ||
calories = tmp | ||
} | ||
} | ||
calories = 0 | ||
} else { | ||
toAdd, err := strconv.Atoi(r.Text()) | ||
if err != nil { | ||
log.Fatalf("could not interpret data as calories: %v", err) | ||
} | ||
calories += toAdd | ||
} | ||
} | ||
for i := range top { | ||
if calories > top[i] { | ||
tmp := top[i] | ||
top[i] = calories | ||
calories = tmp | ||
} | ||
} | ||
return top | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module aoc_day01 | ||
|
||
go 1.19 |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.