Skip to content

Commit

Permalink
D1: implement puzzle part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
benhsm committed Dec 2, 2022
1 parent 6366fd0 commit 6f7c61d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 43 deletions.
78 changes: 78 additions & 0 deletions day01/day01.go
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
}
17 changes: 15 additions & 2 deletions day_1/day_1_test.go → day01/day01_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

func TestFindMostCalories(t *testing.T) {
t.Run("basic case", func(t *testing.T) {
input := strings.NewReader(`1000
input := strings.NewReader(`1000
2000
3000
Expand All @@ -21,9 +20,23 @@ func TestFindMostCalories(t *testing.T) {
9000
10000`)

t.Run("basic case", func(t *testing.T) {
answer := findMostCalories(input)
if answer != 24000 {
t.Errorf("want answer 24000, got %v", answer)
}
})

t.Run("find top three", func(t *testing.T) {
input.Seek(0, 0)
answer := findTopThree(input)
want := []int{24000, 11000, 10000}
for i := range want {
if answer[i] != want[i] {
t.Errorf("want answer %v, got %v", want, answer)
break
}
}
})
}
3 changes: 3 additions & 0 deletions day01/go.mod
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.
38 changes: 0 additions & 38 deletions day_1/day_1_.go

This file was deleted.

3 changes: 0 additions & 3 deletions day_1/go.mod

This file was deleted.

0 comments on commit 6f7c61d

Please sign in to comment.