i miss go, idk if it misses me

This commit is contained in:
alexchao26
2022-12-01 10:41:44 -05:00
parent 94d09b9836
commit dbcb2fbadc
2 changed files with 126 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
package main
import (
_ "embed"
"flag"
"fmt"
"sort"
"strings"
"github.com/alexchao26/advent-of-code-go/cast"
"github.com/alexchao26/advent-of-code-go/mathy"
"github.com/alexchao26/advent-of-code-go/util"
)
//go:embed input.txt
var input string
func init() {
// do this in init (not main) so test file has same input
input = strings.TrimRight(input, "\n")
if len(input) == 0 {
panic("empty input.txt file")
}
}
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(input)
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(input)
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
elves := parseInput(input)
totals := []int{}
for _, items := range elves {
totals = append(totals, mathy.SumIntSlice(items))
}
return mathy.MaxInt(totals...)
}
func part2(input string) int {
elves := parseInput(input)
totals := []int{}
for _, items := range elves {
totals = append(totals, mathy.SumIntSlice(items))
}
sort.Ints(totals)
topThree := 0
for i := 0; i < 3; i++ {
topThree += totals[len(totals)-1-i]
}
return topThree
}
func parseInput(input string) (ans [][]int) {
for _, group := range strings.Split(input, "\n\n") {
row := []int{}
for _, line := range strings.Split(group, "\n") {
row = append(row, cast.ToInt(line))
}
ans = append(ans, row)
}
return ans
}
+47
View File
@@ -0,0 +1,47 @@
package main
import (
"testing"
)
func Test_part1(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{
name: "actual",
input: input,
want: 66719,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
func Test_part2(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{
name: "actual",
input: input,
want: 198551,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
}
})
}
}