diff --git a/README.md b/README.md new file mode 100644 index 0000000..6495762 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Advent of Code 2022 + +My solutions to the [Advent of Code 2022](https://adventofcode.com/2022) puzzles, written in Golang. diff --git a/dayone.go b/dayone.go index 7a8c831..65b3497 100644 --- a/dayone.go +++ b/dayone.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "math" "strconv" "strings" @@ -44,47 +43,3 @@ func dayOne(input string) (maxCalories int) { return } - -func main() { - var result int; - var input string; - - input = - `1000 - 2000 - 3000 - - 4000 - - 5000 - 6000 - - 7000 - 8000 - 9000 - - 10000` - - result = dayOne(input) - - fmt.Println(result) - - input = - ` - 245 - 256 - 939 - - -12039 - - - 4949 - - 2 - 2 - 2 - ` - - result = dayOne(input) - fmt.Println(result) -} diff --git a/daytwo.go b/daytwo.go new file mode 100644 index 0000000..bab95b1 --- /dev/null +++ b/daytwo.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "strings" +) + +func contains(s []string, e string) bool { + for _, a := range s { + if strings.EqualFold(a, e) { + return true + } + } + return false +} + +func playValueOf(s string) int { + fmt.Println("played value: " + s) + fmt.Println("last char: " + string(s[len(s)-1])) + + switch string(s[len(s)-1]) { + case "Z": + return 3 + case "Y": + return 2 + case "X": + return 1 + default: + panic("Invalid input received at func playValueOf") + } +} + +func dayTwo(input string) int { + // split input by newlines + var eachPart = strings.Split(input, "\n") + + // trim all remaining whitespace + for j := range eachPart { + eachPart[j] = strings.TrimSpace(eachPart[j]) + } + + winningResults := []string{ + "A Y", + "B Z", + "C X", + } + + drawResult := []string{ + "A X", + "B Y", + "C Z", + } + + losingResult := []string{ + "A Z", + "B X", + "C Y", + } + + score := 0 + + for i := 0; i <= 3; i++ { + switch true { + case contains(winningResults, eachPart[i]): + score += playValueOf(eachPart[i]) + 6 + case contains(drawResult, eachPart[i]): + score += playValueOf(eachPart[i]) + 3 + case contains(losingResult, eachPart[i]): + score += playValueOf(eachPart[i]) + default: + score += 0 + } + } + + return score +} + diff --git a/go.mod b/go.mod index 8200add..a5e5ec0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/innocuous-symmetry/aoc +module advent-of-code go 1.20 diff --git a/main.go b/main.go new file mode 100644 index 0000000..3969e87 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("ADVENT OF CODE 2022 SOLUTIONS -- GOLANG") + + /** DAY ONE */ + fmt.Println("\nDAY ONE") + + var dayOneResult = dayOne( + `1000 + 2000 + 3000 + + 4000 + + 5000 + 6000 + + 7000 + 8000 + 9000 + + 10000`) + + fmt.Println(dayOneResult) + + dayOneResult = dayOne( + `245 + 256 + 939 + + -12039 + + + 4949 + + 2 + 2 + 2`) + + fmt.Println(dayOneResult) + + /** DAY TWO */ + fmt.Println("\nDAY TWO") + + var dayTwoResult = dayTwo( + ` + A Y + B X + C Z + `, + ) + fmt.Println(dayTwoResult) +}