Create basic directory structure

master
Jonas Letzbor 2022-12-06 22:10:52 +01:00
commit 172efc02f9
30 changed files with 517 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
/session.txt
/inputData/*

View File

@ -0,0 +1,97 @@
package main
import (
"os"
"strconv"
"rpjosh.de/adventOfCode2022/internal/day_01"
"rpjosh.de/adventOfCode2022/internal/day_02"
"rpjosh.de/adventOfCode2022/internal/day_03"
"rpjosh.de/adventOfCode2022/internal/day_04"
"rpjosh.de/adventOfCode2022/internal/day_05"
"rpjosh.de/adventOfCode2022/internal/day_06"
"rpjosh.de/adventOfCode2022/internal/day_07"
"rpjosh.de/adventOfCode2022/internal/day_08"
"rpjosh.de/adventOfCode2022/internal/day_09"
"rpjosh.de/adventOfCode2022/internal/day_10"
"rpjosh.de/adventOfCode2022/internal/day_11"
"rpjosh.de/adventOfCode2022/internal/day_12"
"rpjosh.de/adventOfCode2022/internal/day_13"
"rpjosh.de/adventOfCode2022/internal/day_14"
"rpjosh.de/adventOfCode2022/internal/day_15"
"rpjosh.de/adventOfCode2022/internal/day_16"
"rpjosh.de/adventOfCode2022/internal/day_17"
"rpjosh.de/adventOfCode2022/internal/day_18"
"rpjosh.de/adventOfCode2022/internal/day_19"
"rpjosh.de/adventOfCode2022/internal/day_20"
"rpjosh.de/adventOfCode2022/internal/day_21"
"rpjosh.de/adventOfCode2022/internal/day_22"
"rpjosh.de/adventOfCode2022/internal/day_23"
"rpjosh.de/adventOfCode2022/internal/day_24"
"rpjosh.de/adventOfCode2022/internal/day_25"
"rpjosh.de/adventOfCode2022/pkg/utils"
)
func main() {
year := 2022
day := 1
if len(os.Args) >= 2 && os.Args[1] != "" {
var err error
day, err = strconv.Atoi(os.Args[1])
if err != nil {
utils.PrintError("Expected the day of the challenge (integer) as argument: %s", err)
}
}
data := utils.GetInputData(year, day)
challenge := getAllChallenges()[day-1]
part := 1
if len(os.Args) >= 3 && os.Args[2] != "" {
var err error
part, err = strconv.Atoi(os.Args[2])
if err != nil {
utils.PrintError("Expected the part of the challenge (integer) as argument: %s", err)
}
}
if part == 1 {
utils.CopyToClipboard(challenge.Part1(data))
} else {
utils.CopyToClipboard(challenge.Part2(data))
}
}
type Challenge interface {
Part1(string) string
Part2(string) string
}
func getAllChallenges() []Challenge {
return []Challenge{
&day_01.Day{},
&day_02.Day{},
&day_03.Day{},
&day_04.Day{},
&day_05.Day{},
&day_06.Day{},
&day_07.Day{},
&day_08.Day{},
&day_09.Day{},
&day_10.Day{},
&day_11.Day{},
&day_12.Day{},
&day_13.Day{},
&day_14.Day{},
&day_15.Day{},
&day_16.Day{},
&day_17.Day{},
&day_18.Day{},
&day_19.Day{},
&day_20.Day{},
&day_21.Day{},
&day_22.Day{},
&day_23.Day{},
&day_24.Day{},
&day_25.Day{},
}
}

3
go.mod 100644
View File

@ -0,0 +1,3 @@
module rpjosh.de/adventOfCode2022
go 1.18

View File

@ -0,0 +1,64 @@
package day_01
import (
"fmt"
"strings"
"rpjosh.de/adventOfCode2022/pkg/utils"
)
type Day struct{}
func (d *Day) Part1(in string) string {
current := 0
max := 0
for _, val := range strings.Split(in, "\n") {
if val == "" {
// Clear it
if current > max {
max = current
}
current = 0
continue
}
current += utils.ToInt(val)
}
return fmt.Sprintf("%d", max)
}
func (d *Day) Part2(in string) string {
current := 0
var totals []int
for _, val := range strings.Split(in, "\n") {
if val == "" {
// Clear it
totals = append(totals, current)
current = 0
continue
}
current += utils.ToInt(val)
}
totals = append(totals, current)
// Sort array
for i := 0; i < len(totals)-1; i++ {
if totals[i+1] > totals[i] {
tmp := totals[i]
totals[i] = totals[i+1]
totals[i+1] = tmp
i = -1
}
}
sum := 0
for i := 0; i < 3; i++ {
sum += totals[i]
}
return fmt.Sprintf("%d", sum)
}

View File

@ -0,0 +1,11 @@
package day_02
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_03
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_04
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_05
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_06
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_07
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_08
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_09
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_10
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_11
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_12
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_13
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_14
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_15
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_16
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_17
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_18
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_19
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_20
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_21
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_22
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_23
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_24
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_25
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

83
pkg/utils/utils.go 100644
View File

@ -0,0 +1,83 @@
package utils
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// Copies the given string to the clipboard.
// The tool 'xclip needs to be installed'
func CopyToClipboard(text string) {
command := fmt.Sprintf("echo '%s' | xclip -selection clipboard", strings.ReplaceAll(text, "'", "''"))
cmd := exec.Command("bash", "-c", command)
if err := cmd.Start(); err != nil {
PrintError("Error while executing command: %s", err)
}
fmt.Print(text)
}
// Get's the input data for the task
func GetInputData(year int, day int) string {
// Check if the ressource was already requested
fileLocation := fmt.Sprintf("./inputData/%d/day_%02d", year, day)
cachedInput, err := os.ReadFile(fileLocation)
if err == nil {
return string(cachedInput)
}
// Make a request to get the input from advent of code and save it in a file
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", year, day)
// Read cookie session vaue
session, err := os.ReadFile("./session.txt")
if err != nil {
PrintError("Failed to read cookie session file: %s", err)
}
client := http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
PrintError("%s", err)
}
req.AddCookie(&http.Cookie{Name: "session", Value: string(session)})
// Execute the query
res, err := client.Do(req)
if err != nil {
PrintError("Failed to execute request: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
PrintError("Incorrect status code: %d", res.StatusCode)
}
inputData, _ := io.ReadAll(res.Body)
err = os.WriteFile(fileLocation, inputData, 0644)
if err != nil {
PrintError("Failed to write input data to file: %s", err)
}
return string(inputData)
}
func PrintError(format string, a ...any) {
fmt.Printf(format, a...)
os.Exit(-1)
}
func ToInt(val string) int {
value, err := strconv.Atoi(val)
if err != nil {
PrintError("%s", err)
}
return value
}

3
run.sh 100755
View File

@ -0,0 +1,3 @@
#!/bin/sh
nodemon --quiet -e go,html,yaml --ignore web/app/ --signal SIGTERM --exec 'go run ./cmd/adventOfCode '$1' '$2' || exit 1'