Skip to content

Instantly share code, notes, and snippets.

@galElmalah
Created December 4, 2022 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galElmalah/3a9b01c98d46ed8f52d8a85c419b2296 to your computer and use it in GitHub Desktop.
Save galElmalah/3a9b01c98d46ed8f52d8a85c419b2296 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 1 Solution in Go
package main
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func main() {
chunks := parse()
result := []int{}
for _, chunk := range chunks {
result = append(result, sumChunk(chunk))
}
sort.Ints(result)
fmt.Println("Part 1")
fmt.Println(result[len(result)-1])
fmt.Println("Part 2")
fmt.Println(result[len(result)-1] + result[len(result)-2] + result[len(result)-3])
}
func sumChunk(chunk string) int {
sum := 0
for _, num := range strings.Split(chunk, "\n") {
v, _ := strconv.Atoi(num)
sum += v
}
return sum
}
func parse() []string {
data, _ := os.ReadFile("./input.txt")
chunks := strings.Split(string(data), "\n\n")
return chunks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment