Renamed to have nicer folder order

This commit is contained in:
Ceferino Patino 2024-12-11 17:37:51 -06:00
commit 773a676ba0
Signed by: c4patino
SSH key fingerprint: SHA256:Wu+cU1t+7zVT9wzew/4meNmeulo66NzMqc22MdDBgXI
40 changed files with 48 additions and 149 deletions

View file

@ -7,34 +7,33 @@ import (
"strconv"
"cpatino.com/advent-of-code/2024/cmd/generate"
"cpatino.com/advent-of-code/2024/cmd/submit"
"github.com/spf13/cobra"
"cpatino.com/advent-of-code/2024/day1"
"cpatino.com/advent-of-code/2024/day01"
"cpatino.com/advent-of-code/2024/day02"
"cpatino.com/advent-of-code/2024/day03"
"cpatino.com/advent-of-code/2024/day04"
"cpatino.com/advent-of-code/2024/day05"
"cpatino.com/advent-of-code/2024/day06"
"cpatino.com/advent-of-code/2024/day07"
"cpatino.com/advent-of-code/2024/day08"
"cpatino.com/advent-of-code/2024/day09"
"cpatino.com/advent-of-code/2024/day10"
"cpatino.com/advent-of-code/2024/day11"
"cpatino.com/advent-of-code/2024/day2"
"cpatino.com/advent-of-code/2024/day3"
"cpatino.com/advent-of-code/2024/day4"
"cpatino.com/advent-of-code/2024/day5"
"cpatino.com/advent-of-code/2024/day6"
"cpatino.com/advent-of-code/2024/day7"
"cpatino.com/advent-of-code/2024/day8"
"cpatino.com/advent-of-code/2024/day9"
)
var days = map[int]func(string) (int, int){
1: day1.Run,
1: day01.Run,
2: day02.Run,
3: day03.Run,
4: day04.Run,
5: day05.Run,
6: day06.Run,
7: day07.Run,
8: day08.Run,
9: day09.Run,
10: day10.Run,
11: day11.Run,
2: day2.Run,
3: day3.Run,
4: day4.Run,
5: day5.Run,
6: day6.Run,
7: day7.Run,
8: day8.Run,
9: day9.Run,
}
var rootCmd = &cobra.Command{
@ -49,7 +48,7 @@ var rootCmd = &cobra.Command{
file, _ := cmd.Flags().GetString("file")
if file == "" {
file = fmt.Sprintf("./day%d/input.txt", day)
file = fmt.Sprintf("./day%02d/input.txt", day)
}
if _, err := os.Stat(file); os.IsNotExist(err) {
@ -67,7 +66,6 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(generate.GenerateCmd)
rootCmd.AddCommand(submit.SubmitCmd)
rootCmd.Flags().String("file", "", "Path to a file")
}

View file

@ -1,40 +0,0 @@
package submit
import (
"log"
"os"
"strconv"
"github.com/spf13/cobra"
)
func handleErr(err error, folder string) {
os.RemoveAll(folder)
log.Fatal(err)
}
var SubmitCmd = &cobra.Command{
Use: "submit",
Short: "Submit solution to Advent of Code 2024",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
day, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}
part, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal(err)
}
answer, err := strconv.Atoi(args[2])
if err != nil {
log.Fatal(err)
}
if err := submitInput(day, part, answer); err != nil {
log.Fatal(err)
}
},
}

View file

@ -1,63 +0,0 @@
package submit
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
)
func submitInput(day, level, answer int) error {
cookieValue := os.Getenv("COOKIE")
if cookieValue == "" {
return errors.New("COOKIE environment variable not set")
}
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
data := url.Values{}
data.Set("level", fmt.Sprintf("%d", level))
data.Set("answer", fmt.Sprintf("%d", answer))
client := &http.Client{Jar: jar}
reqUrl := fmt.Sprintf("https://adventofcode.com/2024/day/%d/answer", day)
req, err := http.NewRequest("POST", reqUrl, strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Set("User-Agent", "github.com/C4theBomb/advent-of-code by c4patino@gmail.com")
cookie := &http.Cookie{Name: "session", Value: cookieValue}
req.AddCookie(cookie)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("Unexpected status code: %d\nBody: %s", resp.StatusCode, string(body)))
}
filename := fmt.Sprintf("./day%02d/input.txt", day)
if err := os.WriteFile(filename, []byte(body), 0755); err != nil {
return err
}
return nil
}

View file

@ -2,9 +2,11 @@ package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
@ -19,7 +21,6 @@ import (
"github.com/spf13/cobra"
"cpatino.com/advent-of-code/2024/cmd/generate"
"cpatino.com/advent-of-code/2024/cmd/submit"
%s
)
@ -40,7 +41,7 @@ var rootCmd = &cobra.Command{
file, _ := cmd.Flags().GetString("file")
if file == "" {
file = fmt.Sprintf("./day%%d/input.txt", day)
file = fmt.Sprintf("./day%%02d/input.txt", day)
}
if _, err := os.Stat(file); os.IsNotExist(err) {
@ -58,7 +59,6 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(generate.GenerateCmd)
rootCmd.AddCommand(submit.SubmitCmd)
rootCmd.Flags().String("file", "", "Path to a file")
}
@ -85,11 +85,15 @@ func Execute() error {
return nil
}
dayStr := info.Name()
dayNum := strings.TrimPrefix(dayStr, "day")
dayStr := strings.TrimPrefix(info.Name(), "day")
imports.WriteString(fmt.Sprintf("\t\"cpatino.com/advent-of-code/2024/%s\"\n", dayStr))
mappings.WriteString(fmt.Sprintf("\t%s: %s.Run,\n", dayNum, dayStr))
day, err := strconv.Atoi(dayStr)
if err != nil {
log.Fatal(err)
}
imports.WriteString(fmt.Sprintf("\t\"cpatino.com/advent-of-code/2024/day%02d\"\n", day))
mappings.WriteString(fmt.Sprintf("\t%d: day%02d.Run,\n", day, day))
return nil
})

View file

@ -1,4 +1,4 @@
package day1
package day01
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day1
package day01
import (
"os"

View file

@ -1,4 +1,4 @@
package day2
package day02
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day2
package day02
import (
"os"

View file

@ -1,4 +1,4 @@
package day3
package day03
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day3
package day03
import (
"os"

View file

@ -1,4 +1,4 @@
package day4
package day04
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day4
package day04
import (
"os"

View file

@ -1,4 +1,4 @@
package day5
package day05
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day5
package day05
import (
"os"

View file

@ -1,4 +1,4 @@
package day6
package day06
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day6
package day06
import (
"os"

View file

@ -1,4 +1,4 @@
package day7
package day07
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day7
package day07
import (
"os"

View file

@ -1,4 +1,4 @@
package day8
package day08
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day8
package day08
import (
"os"

View file

@ -1,4 +1,4 @@
package day9
package day09
import (
"bufio"

View file

@ -1,4 +1,4 @@
package day9
package day09
import (
"os"