Initial commit
This commit is contained in:
commit
0e7e628f0c
79
.gitignore
vendored
Normal file
79
.gitignore
vendored
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
### Go template
|
||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
### GoLand template
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# Exclude all IDE stuff.
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
cmake-build-*/
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
.idea/**/mongoSettings.xml
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
.idea/replstate.xml
|
||||||
|
|
||||||
|
# SonarLint plugin
|
||||||
|
.idea/sonarlint/
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
.idea/httpRequests
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
56
Y22/day1.go
Normal file
56
Y22/day1.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package Y22
|
||||||
|
|
||||||
|
import (
|
||||||
|
"adventofcode/helpers"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Day1() helpers.DayResults {
|
||||||
|
day1Example := helpers.ReadFileByLine("inputs/Y22/day1-example.txt")
|
||||||
|
day1 := helpers.ReadFileByLine("inputs/Y22/day1.txt")
|
||||||
|
|
||||||
|
calorieCount := calculateCalories(day1Example)
|
||||||
|
part1Example := calorieCount[len(calorieCount)-1]
|
||||||
|
part2Example := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
|
||||||
|
|
||||||
|
calorieCount = calculateCalories(day1)
|
||||||
|
part1Result := calorieCount[len(calorieCount)-1]
|
||||||
|
part2Result := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
|
||||||
|
|
||||||
|
results := helpers.DayResults{
|
||||||
|
Day: 1,
|
||||||
|
Year: 2022,
|
||||||
|
Part1Example: part1Example,
|
||||||
|
Part1: part1Result,
|
||||||
|
Part2Example: part2Example,
|
||||||
|
Part2: part2Result,
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateCalories(data []string) []int {
|
||||||
|
var calorieCount []int
|
||||||
|
total := 0
|
||||||
|
for _, line := range data {
|
||||||
|
if line == "" {
|
||||||
|
calorieCount = append(calorieCount, total)
|
||||||
|
total = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err := strconv.Atoi(line)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
total += val
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(calorieCount, func(i, j int) bool {
|
||||||
|
return calorieCount[i] < calorieCount[j]
|
||||||
|
})
|
||||||
|
|
||||||
|
return calorieCount
|
||||||
|
}
|
93
Y22/day2.go
Normal file
93
Y22/day2.go
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package Y22
|
||||||
|
|
||||||
|
import (
|
||||||
|
"adventofcode/helpers"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
aRock string = "A"
|
||||||
|
aPaper string = "B"
|
||||||
|
aScissors string = "C"
|
||||||
|
|
||||||
|
bRock string = "X"
|
||||||
|
bPaper string = "Y"
|
||||||
|
bScissors string = "Z"
|
||||||
|
|
||||||
|
resultLose string = "X"
|
||||||
|
resultDraw string = "Y"
|
||||||
|
resultWin string = "Z"
|
||||||
|
)
|
||||||
|
|
||||||
|
var scoreTable = map[string]map[string]int{
|
||||||
|
aRock: {
|
||||||
|
bRock: 4,
|
||||||
|
bPaper: 8,
|
||||||
|
bScissors: 3,
|
||||||
|
},
|
||||||
|
aPaper: {
|
||||||
|
bRock: 1,
|
||||||
|
bPaper: 5,
|
||||||
|
bScissors: 9,
|
||||||
|
},
|
||||||
|
aScissors: {
|
||||||
|
bRock: 7,
|
||||||
|
bPaper: 2,
|
||||||
|
bScissors: 6,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var winOrLoseTable = map[string]map[string]string{
|
||||||
|
aRock: {
|
||||||
|
resultWin: bPaper,
|
||||||
|
resultDraw: bRock,
|
||||||
|
resultLose: bScissors,
|
||||||
|
},
|
||||||
|
aPaper: {
|
||||||
|
resultWin: bScissors,
|
||||||
|
resultDraw: bPaper,
|
||||||
|
resultLose: bRock,
|
||||||
|
},
|
||||||
|
aScissors: {
|
||||||
|
resultWin: bRock,
|
||||||
|
resultDraw: bScissors,
|
||||||
|
resultLose: bPaper,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func Day2() helpers.DayResults {
|
||||||
|
day2Example := helpers.ReadFileByLine("inputs/Y22/day2-example.txt")
|
||||||
|
day2 := helpers.ReadFileByLine("inputs/Y22/day2.txt")
|
||||||
|
|
||||||
|
results := helpers.DayResults{
|
||||||
|
Day: 2,
|
||||||
|
Year: 2022,
|
||||||
|
Part1Example: part1(day2Example),
|
||||||
|
Part1: part1(day2),
|
||||||
|
Part2Example: part2(day2Example),
|
||||||
|
Part2: part2(day2),
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(input []string) int {
|
||||||
|
score := 0
|
||||||
|
for _, line := range input {
|
||||||
|
data := strings.Split(line, " ")
|
||||||
|
score += scoreTable[data[0]][data[1]]
|
||||||
|
}
|
||||||
|
|
||||||
|
return score
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(input []string) int {
|
||||||
|
score := 0
|
||||||
|
for _, line := range input {
|
||||||
|
data := strings.Split(line, " ")
|
||||||
|
scoreValue := winOrLoseTable[data[0]][data[1]]
|
||||||
|
score += scoreTable[data[0]][scoreValue]
|
||||||
|
}
|
||||||
|
|
||||||
|
return score
|
||||||
|
}
|
17
adventofcode.go
Normal file
17
adventofcode.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"adventofcode/Y22"
|
||||||
|
"adventofcode/helpers"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
runY22()
|
||||||
|
}
|
||||||
|
|
||||||
|
func runY22() {
|
||||||
|
color.HiYellow("\n--- Advent of Code 2022 ---\n\n")
|
||||||
|
helpers.PrintDayResults(Y22.Day1())
|
||||||
|
helpers.PrintDayResults(Y22.Day2())
|
||||||
|
}
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module adventofcode
|
||||||
|
|
||||||
|
go 1.21.4
|
||||||
|
|
||||||
|
require github.com/fatih/color v1.16.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
golang.org/x/sys v0.14.0 // indirect
|
||||||
|
)
|
11
go.sum
Normal file
11
go.sum
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||||
|
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||||
|
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
28
helpers/Day.go
Normal file
28
helpers/Day.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DayResults struct {
|
||||||
|
Day int `json:"day"`
|
||||||
|
Year int `json:"year"`
|
||||||
|
Part1Example int `json:"part1-example"`
|
||||||
|
Part1 int `json:"part1"`
|
||||||
|
Part2Example int `json:"part2-example"`
|
||||||
|
Part2 int `json:"part2"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintDayResults(results DayResults) {
|
||||||
|
yellow := color.New(color.FgYellow).SprintFunc()
|
||||||
|
gray := color.New(color.FgHiBlack).SprintFunc()
|
||||||
|
|
||||||
|
color.HiBlue("Day %d%s", results.Day, gray("/"+strconv.Itoa(results.Year)))
|
||||||
|
color.Green(" Part 1 (example): %s", yellow(results.Part1Example))
|
||||||
|
color.HiGreen(" Part 1 : %s", yellow(results.Part1))
|
||||||
|
color.Blue(" " + strings.Repeat("-", 23))
|
||||||
|
color.Green(" Part 2 (example): %s", yellow(results.Part2Example))
|
||||||
|
color.HiGreen(" Part 2 : %s", yellow(results.Part2))
|
||||||
|
}
|
32
helpers/ReadFileByLine.go
Normal file
32
helpers/ReadFileByLine.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadFileByLine(path string) []string {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
var returnData []string
|
||||||
|
for scanner.Scan() {
|
||||||
|
returnData = append(returnData, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnData
|
||||||
|
}
|
14
inputs/Y22/day1-example.txt
Normal file
14
inputs/Y22/day1-example.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000
|
2252
inputs/Y22/day1.txt
Normal file
2252
inputs/Y22/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
inputs/Y22/day2-example.txt
Normal file
3
inputs/Y22/day2-example.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
2500
inputs/Y22/day2.txt
Normal file
2500
inputs/Y22/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user