Created
December 4, 2022 11:35
-
-
Save galElmalah/3a9b01c98d46ed8f52d8a85c419b2296 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 1 Solution in Go
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
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