diff --git a/pkg/aoc/aoc.go b/pkg/aoc/aoc.go index 940cc2d..5c94299 100644 --- a/pkg/aoc/aoc.go +++ b/pkg/aoc/aoc.go @@ -87,6 +87,7 @@ func GetPuzzles(day string, year string) []puzzle.Puzzle { rawInput := getBodyFromUrl(inputURL, aocConfig.SessionId) response, err := ParsePuzzles(day, year, body, rawInput) + if err != nil { log.Fatalf("Error parsing puzzles: %s", err) } diff --git a/pkg/aoc/aoc_service.go b/pkg/aoc/aoc_service.go index caf64f2..cbbe452 100644 --- a/pkg/aoc/aoc_service.go +++ b/pkg/aoc/aoc_service.go @@ -1,42 +1,41 @@ package aoc import ( - "context" - "io" - "log" - "net/http" - "time" + "context" + "io" + "log" + "net/http" + "time" ) - func getBodyFromUrl(url string, cookie string) []byte { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) - req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + log.Fatalf("Error making request: %s", err) + } - if err != nil { - log.Fatalf("Error making request: %s", err) - } + if cookie == "" { + log.Fatal("Advent of Code requires a session and your cookie is empty") + } - if cookie == "" { - log.Fatal("Advent of Code requires a session and your cookie is empty") - } + sessionCookie := http.Cookie{Name: "session", Value: cookie} + req.AddCookie(&sessionCookie) - sessionCookie := http.Cookie{Name: "session", Value: cookie} - req.AddCookie(&sessionCookie) + res, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatalf("Error fetching the page: %s", err) + } - res, err := http.DefaultClient.Do(req) - if err != nil { - log.Fatalf("Error fetching the page: %s", err) - } + defer res.Body.Close() + body, err := io.ReadAll(res.Body) - defer res.Body.Close() - body, err := io.ReadAll(res.Body) - - if err != nil { - log.Fatalf("Error reading the body of the request: %s", err) - } + if err != nil { + log.Fatalf("Error reading the body of the request: %s", err) + } - return body + return body } diff --git a/pkg/aoc/html_utils.go b/pkg/aoc/html_utils.go index 0204b28..011f56a 100644 --- a/pkg/aoc/html_utils.go +++ b/pkg/aoc/html_utils.go @@ -10,52 +10,52 @@ import ( ) func ParsePuzzles(day string, year string, responseBody []byte, input []byte) ([]puzzle.Puzzle, error) { - node, err := html.Parse(bytes.NewReader(responseBody)) - - if err != nil { - log.Fatalf("Error parsing the body: %s", err) - } - - puzzlePartsHTMLNodes := findRootNodesPuzzle(node) - - var parts []puzzle.Puzzle - for _, node := range puzzlePartsHTMLNodes { - parts = append(parts, puzzle.NewPuzzleFromHTML(day, year, parsePuzzleHTML(node), input)) - } - return parts, nil + node, err := html.Parse(bytes.NewReader(responseBody)) + + if err != nil { + log.Fatalf("Error parsing the body: %s", err) + } + + puzzlePartsHTMLNodes := findRootNodesPuzzle(node) + + var parts []puzzle.Puzzle + for _, node := range puzzlePartsHTMLNodes { + parts = append(parts, puzzle.NewPuzzleFromHTML(day, year, parsePuzzleHTML(node), input)) + } + return parts, nil } func findRootNodesPuzzle(node *html.Node) []*html.Node { - var nodes []*html.Node - - for child := node.FirstChild; child != nil; child = child.NextSibling { - if child.Type == html.ElementNode && child.Data == "article" && hasAttr(child.Attr, "day-desc") { - nodes = append(nodes, child) - } - nodes = append(nodes, findRootNodesPuzzle(child)...) - } - - return nodes + var nodes []*html.Node + + for child := node.FirstChild; child != nil; child = child.NextSibling { + if child.Type == html.ElementNode && child.Data == "article" && hasAttr(child.Attr, "day-desc") { + nodes = append(nodes, child) + } + nodes = append(nodes, findRootNodesPuzzle(child)...) + } + + return nodes } func hasAttr(attrs []html.Attribute, attr string) bool { - for _, a := range attrs { - if a.Key == "class" && a.Val == attr { - return true - } - } - return false + for _, a := range attrs { + if a.Key == "class" && a.Val == attr { + return true + } + } + return false } func parsePuzzleHTML(node *html.Node) string { - buffer := strings.Builder{} - - for child := node.FirstChild; child != nil; child = child.NextSibling { - if child.Type == html.TextNode { - buffer.WriteString(child.Data) - } - buffer.WriteString(parsePuzzleHTML(child)) - } - - return buffer.String() + buffer := strings.Builder{} + + for child := node.FirstChild; child != nil; child = child.NextSibling { + if child.Type == html.TextNode { + buffer.WriteString(child.Data) + } + buffer.WriteString(parsePuzzleHTML(child)) + } + + return buffer.String() } diff --git a/pkg/puzzle/puzzle.go b/pkg/puzzle/puzzle.go index 300b4f0..ef5ed4a 100644 --- a/pkg/puzzle/puzzle.go +++ b/pkg/puzzle/puzzle.go @@ -1,111 +1,132 @@ package puzzle -import( - "gopkg.in/yaml.v3" - "log" - "os" - "errors" - "fmt" - "strings" +import ( + "errors" + "fmt" + "log" + "os" + "strings" + + "gopkg.in/yaml.v3" ) type PuzzleStatus string type PuzzleMetadata struct { - Day string `yaml:"day"` - Title string `yaml:"title"` - Year string `yaml:"year"` + Day string `yaml:"day"` + Title string `yaml:"title"` + Year string `yaml:"year"` } type PuzzlePart struct { - Answer string `yaml:"answer,omitempty"` - Description string `yaml:"description"` - Status PuzzleStatus `yaml:"status"` + Answer string `yaml:"answer,omitempty"` + Description string `yaml:"description"` + Status PuzzleStatus `yaml:"status"` - RawInput []byte + RawInput []byte } type Puzzle struct { - Metadata PuzzleMetadata `yaml:"metadata"` - Puzzles []PuzzlePart `yaml:"puzzles"` + Metadata PuzzleMetadata `yaml:"metadata"` + Puzzles []PuzzlePart `yaml:"puzzles"` } func NewPuzzleFromHTML(day string, year string, htmlString string, input []byte) Puzzle { - return Puzzle{ - Metadata: PuzzleMetadata{ - Day: day, - Year: year, - Title: getTitleFromBody(htmlString), - }, - Puzzles: []PuzzlePart{}, - } + return Puzzle{ + Metadata: PuzzleMetadata{ + Day: day, + Year: year, + Title: getTitleFromBody(htmlString), + }, + Puzzles: getPuzzlePartsFromHTMLString(htmlString), + } } // The field status is a collection of status and we need to validate that the // status is in the set of valid statuses func (p *Puzzle) ParseFields() error { - mapStatus := map[string]PuzzleStatus{ - "UNSOLVED": Unsolved, - "SOLVED": Solved, - "UNREACHABLE": Unreachable, - } - for i, puzzle := range p.Puzzles { - status := strings.ToUpper(string(puzzle.Status)) - if _, ok := mapStatus[status]; ok { - p.Puzzles[i].Status = mapStatus[status] - } else { - errorMessage := fmt.Sprintf("cannot parse Puzzle Part %d", i) - return NewError(ErrInvalidStatus, errors.New(errorMessage)) - } - } - return nil + mapStatus := map[string]PuzzleStatus{ + "UNSOLVED": Unsolved, + "SOLVED": Solved, + "UNREACHABLE": Unreachable, + } + for i, puzzle := range p.Puzzles { + status := strings.ToUpper(string(puzzle.Status)) + if _, ok := mapStatus[status]; ok { + p.Puzzles[i].Status = mapStatus[status] + } else { + errorMessage := fmt.Sprintf("cannot parse Puzzle Part %d", i) + return NewError(ErrInvalidStatus, errors.New(errorMessage)) + } + } + return nil } func NewPuzzleFromCache(filepath string, inputFilepath []string) (Puzzle, error) { - var puzzle Puzzle - yamlFile, err := os.ReadFile(filepath) - if err != nil { - log.Printf("Error trying to read the YAML file err = #%v ", err) - return Puzzle{}, err - } - err = yaml.Unmarshal(yamlFile, &puzzle) - if err != nil { - log.Fatalf("Unmarshal: %v", err) - return Puzzle{}, err - } - - // yaml.Umarshall does not have validation on sets like the status field - // We need to map the status of the puzzle - err = puzzle.ParseFields() - if err != nil { - log.Fatalf("Error trying to parse the Puzzle err = #%v ", err) - return Puzzle{}, err - } - - for i, inputFile := range inputFilepath { - rawInput, err := os.ReadFile(inputFile) - if err != nil { - log.Fatalf("Error trying to read the input for Puzzle Part %d err #%v ", i, err) - return Puzzle{}, err - } - // we need to delete the last byte of the input because it is a newline or EOF - rawInput = rawInput[:len(rawInput)-1] - puzzle.Puzzles[i].RawInput = rawInput - } - return puzzle, nil + var puzzle Puzzle + yamlFile, err := os.ReadFile(filepath) + if err != nil { + log.Printf("Error trying to read the YAML file err = #%v ", err) + return Puzzle{}, err + } + err = yaml.Unmarshal(yamlFile, &puzzle) + if err != nil { + log.Fatalf("Unmarshal: %v", err) + return Puzzle{}, err + } + + // yaml.Umarshall does not have validation on sets like the status field + // We need to map the status of the puzzle + err = puzzle.ParseFields() + if err != nil { + log.Fatalf("Error trying to parse the Puzzle err = #%v ", err) + return Puzzle{}, err + } + + for i, inputFile := range inputFilepath { + rawInput, err := os.ReadFile(inputFile) + if err != nil { + log.Fatalf("Error trying to read the input for Puzzle Part %d err #%v ", i, err) + return Puzzle{}, err + } + // we need to delete the last byte of the input because it is a newline or EOF + rawInput = rawInput[:len(rawInput)-1] + puzzle.Puzzles[i].RawInput = rawInput + } + return puzzle, nil } func getTitleFromBody(body string) string { - return "Title of the Puzzle Part" + parts := strings.Split(body, "---") + titleParts := strings.Split(parts[1], ":") + return strings.TrimSpace(titleParts[1]) +} + +func getPuzzlePartsFromHTMLString(body string) []PuzzlePart { + puzzleParts := make([]PuzzlePart, 0) + + parts := strings.Split(body, "---") + + if len(parts) < 3 { + log.Fatal("Description of the puzzle is not found") + return puzzleParts + } + puzzleParts = append(puzzleParts, PuzzlePart{ + Answer: "", + Status: "UNSOLVED", + Description: parts[2], + }) + + return puzzleParts } type PuzzleSolver[T any] struct { - Puzzle Puzzle - NormalizeInput func(string) T - Solve func() Response + Puzzle Puzzle + NormalizeInput func(string) T + Solve func() Response } type Response struct { - Value string - Error error + Value string + Error error } diff --git a/pkg/puzzle/puzzle_test.go b/pkg/puzzle/puzzle_test.go index 1e21df1..0ea26a7 100644 --- a/pkg/puzzle/puzzle_test.go +++ b/pkg/puzzle/puzzle_test.go @@ -1,30 +1,49 @@ package puzzle import ( - "testing" - "os" - "path/filepath" + "os" + "path/filepath" + "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/assert" ) -func TestSolve(t *testing.T) { - ROOT_DIR := os.Getenv("PWD") - basicYaml := filepath.Join(ROOT_DIR, "test_data/basic.yaml") - subject, err := NewPuzzleFromCache(basicYaml, []string{}) +func TestParsePuzzleFromCache(t *testing.T) { + ROOT_DIR := os.Getenv("PWD") + basicYaml := filepath.Join(ROOT_DIR, "test_data/basic.yaml") + subject, err := NewPuzzleFromCache(basicYaml, []string{}) - // Basic Yaml has no errors - assert.Nil(t, err) + // Basic Yaml has no errors + assert.Nil(t, err) - // Metada is set correctly - assert.Equal(t, subject.Metadata.Year, "year_basic") - assert.Equal(t, subject.Metadata.Day, "day_basic") - assert.Equal(t, subject.Metadata.Title, "title_basic") + // Metada is set correctly + assert.Equal(t, subject.Metadata.Year, "year_basic") + assert.Equal(t, subject.Metadata.Day, "day_basic") + assert.Equal(t, subject.Metadata.Title, "title_basic") - // Puzzles check - assert.Equal(t, len(subject.Puzzles), 2) - assert.Equal(t, subject.Puzzles[0].Description, "description_1_basic") - assert.Equal(t, subject.Puzzles[0].Answer, "answer_1_basic") - assert.Equal(t, subject.Puzzles[0].Status, Solved) + // Puzzles check + assert.Equal(t, len(subject.Puzzles), 2) + assert.Equal(t, subject.Puzzles[0].Description, "description_1_basic") + assert.Equal(t, subject.Puzzles[0].Answer, "answer_1_basic") + assert.Equal(t, subject.Puzzles[0].Status, Solved) } +func TestParsePuzzleFromHTML(t *testing.T) { + + ROOT_DIR := os.Getenv("PWD") + testFilePath := filepath.Join(ROOT_DIR, "test_data/puzzle_with_one_question.txt") + file, err := os.ReadFile(testFilePath) + + if err != nil { + t.Fail() + } + + puzzle := NewPuzzleFromHTML("day", "year", string(file), []byte{}) + + assert.Equal(t, "day", puzzle.Metadata.Day) + assert.Equal(t, "year", puzzle.Metadata.Year) + assert.Equal(t, "Trebuchet?!", puzzle.Metadata.Title) + assert.Equal(t, 1, len(puzzle.Puzzles)) + assert.Equal(t, 1827, len(puzzle.Puzzles[0].Description)) + +} diff --git a/pkg/puzzle/test_data/puzzle_with_one_question.txt b/pkg/puzzle/test_data/puzzle_with_one_question.txt new file mode 100644 index 0000000..552fb0b --- /dev/null +++ b/pkg/puzzle/test_data/puzzle_with_one_question.txt @@ -0,0 +1,14 @@ +--- Day 1: Trebuchet?! ---Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems. +You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th. +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! +You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in"). +As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document. +The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number. +For example: +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet + +In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142. +Consider your entire calibration document. What is the sum of all of the calibration values?