day two, readme

This commit is contained in:
2023-07-12 09:41:09 -05:00
parent d930d0b5ed
commit dfb70a837b
5 changed files with 139 additions and 46 deletions

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# Advent of Code 2022
My solutions to the [Advent of Code 2022](https://adventofcode.com/2022) puzzles, written in Golang.

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@@ -44,47 +43,3 @@ func dayOne(input string) (maxCalories int) {
return 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)
}

77
daytwo.go Normal file
View File

@@ -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
}

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/innocuous-symmetry/aoc module advent-of-code
go 1.20 go 1.20

58
main.go Normal file
View File

@@ -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)
}