diff --git a/cmd/aocli/aocli b/cmd/aocli/aocli new file mode 100755 index 0000000..9ed75a6 Binary files /dev/null and b/cmd/aocli/aocli differ diff --git a/cmd/aocli/aocli.go b/cmd/aocli/aocli.go index 7336bf0..466e1c3 100644 --- a/cmd/aocli/aocli.go +++ b/cmd/aocli/aocli.go @@ -8,6 +8,7 @@ import ( "time" "dalton.dog/aocgo/internal/cache" + "dalton.dog/aocgo/internal/dirparse" "dalton.dog/aocgo/internal/resources" "dalton.dog/aocgo/internal/session" "dalton.dog/aocgo/internal/tui" @@ -85,14 +86,16 @@ func main() { // submit(args) case "leaderboard": leaderboard(args) + case "load-user": + loadUser(args, user) // case "run": // run(args) case "view": view(args, user) case "test": test(user) - case "clear-data": - clearData(user) + case "clear-user": + clearUser(user) case "update": update() default: @@ -101,7 +104,7 @@ func main() { return } -func clearData(user *resources.User) { +func clearUser(user *resources.User) { cache.ClearUserDatabase(user.SessionTok) } @@ -204,6 +207,67 @@ func leaderboard(args []string) { lb.Display() } +func loadUser(args []string, user *resources.User) { + logger := log.New(os.Stdout) + year := 2015 + var maxYear int + if time.Now().Month() == time.December { + maxYear = time.Now().Year() + } else { + maxYear = time.Now().Year() - 1 + } + + if len(args) > 2 { + parseYear, err := dirparse.ParseYear(args[2]) + if err != nil { + log.Fatal(err) + } + + year = parseYear + maxYear = parseYear + } + + numStars := make(map[int]int) + + for year <= maxYear { + logger.Info("Loading year", "year", year) + numStars[year] = 0 + day := 1 + for day <= 25 { + logger.Info("Loading day", "day", day) + puzzle := resources.LoadOrCreatePuzzle(year, day, user.GetToken()) + + user.Years[year][day] = puzzle + + if puzzle.AnswerOne != "" { + logger.Info("Answer one found!", "year", year, "day", day, "answer", puzzle.AnswerOne) + user.NumStars++ + numStars[year]++ + if puzzle.AnswerTwo != "" { + logger.Info("Answer two found!", "year", year, "day", day, "answer", puzzle.AnswerTwo) + user.NumStars++ + numStars[year]++ + } + } + + day++ + } + + // There's only 1 puzzle on Day 25, so if they've earned 49 stars, they get the 50th for free + if numStars[year] == 49 { + user.NumStars++ + numStars[year]++ + } + logger.Info("Ending year", "Stars found", numStars[year]) + + year++ + } + + for val, key := range numStars { + fmt.Printf("%d -- %d\n", key, val) + } +} + // `submit [year] [day] [part] [answer]` command func submit(args []string) { diff --git a/internal/resources/Puzzle.go b/internal/resources/Puzzle.go index 2fa4197..c9d937f 100644 --- a/internal/resources/Puzzle.go +++ b/internal/resources/Puzzle.go @@ -101,7 +101,7 @@ func (p *Puzzle) GetPrettyPageData() []string { sOut := p.ArticleOne if p.AnswerOne != "" { - sOut = append(sOut, "\n"+p.AnswerOne) + sOut = append(sOut, "Answer: "+p.AnswerOne) } if len(p.ArticleTwo) != 0 { @@ -112,7 +112,7 @@ func (p *Puzzle) GetPrettyPageData() []string { sOut = append(sOut, "\n") if p.AnswerTwo != "" { - sOut = append(sOut, p.AnswerTwo) + sOut = append(sOut, "Answer: "+p.AnswerTwo) } } return sOut @@ -174,11 +174,17 @@ func (p *Puzzle) processPageContents(mainContents *goquery.Selection) { // This should only grab "Your puzzle answer was: " tags mainContents.Find("article + p").Each(func(i int, s *goquery.Selection) { - outStr := s.Text() - if p.AnswerOne == "" { - p.AnswerOne = outStr - } else { - p.AnswerTwo = outStr + if strings.Contains(s.Text(), "answer") { + outStr := s.Find("code").Text() + if outStr != "" { + if p.AnswerOne == "" { + log.Debug("Answer found!", "year", p.Year, "day", p.Day, "answer", outStr) + p.AnswerOne = outStr + } else { + log.Debug("Answer found!", "year", p.Year, "day", p.Day, "answer", outStr) + p.AnswerTwo = outStr + } + } } }) } diff --git a/internal/resources/User.go b/internal/resources/User.go index 2dd0b91..a82de66 100644 --- a/internal/resources/User.go +++ b/internal/resources/User.go @@ -13,7 +13,7 @@ import ( // User represents a session token and accompanying puzzles. type User struct { NumStars int - Years map[int][]Puzzle + Years map[int][]*Puzzle SessionTok string } @@ -37,12 +37,13 @@ func NewUser(token string) (*User, error) { if token == "" { return nil, errors.New("Token was still empty after load attempts.") } + token = strings.TrimSpace(token) api.InitClient(token) - yearMap := make(map[int][]Puzzle) + yearMap := make(map[int][]*Puzzle) for i := 2015; i <= time.Now().Year(); i++ { - yearMap[i] = make([]Puzzle, 25) + yearMap[i] = make([]*Puzzle, 26) } return &User{