-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (96 loc) · 3.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"os"
"strconv"
"github.com/mitchellh/go-wordwrap"
log "github.com/sirupsen/logrus"
"github.com/yarsiemanym/advent-of-code-2023/common"
"github.com/yarsiemanym/advent-of-code-2023/day00"
"github.com/yarsiemanym/advent-of-code-2023/day01"
"github.com/yarsiemanym/advent-of-code-2023/day02"
"github.com/yarsiemanym/advent-of-code-2023/day03"
"github.com/yarsiemanym/advent-of-code-2023/vt100"
)
func main() {
common.InitLogging()
common.InitSession()
checkForHelpCommand()
log.Info("Begin execution.")
puzzle := setupPuzzle()
answer := puzzle.Solve()
vt100.Printf("%d Day %d\n", []interface{}{answer.Year, answer.Day}, vt100.GreenForegroundAttribute)
vt100.Print("Part 1 Answer: ", vt100.DimAttribute)
vt100.Println(answer.Part1, vt100.YellowForegroundAttribute)
vt100.Print("Part 2 Answer: ", vt100.DimAttribute)
vt100.Println(answer.Part2, vt100.YellowForegroundAttribute)
vt100.Print("Total Execution Time: ", vt100.DimAttribute)
vt100.Println(answer.ExecutionTime.String(), vt100.RedForegroundAttribute)
log.Info("End execution.")
}
func checkForHelpCommand() {
arg1 := os.Args[1]
if arg1 == "-h" || arg1 == "--help" || arg1 == "help" {
printUsage()
os.Exit(0)
}
}
func printUsage() {
limit := uint(100)
description1 := "Run the solution for the puzzle from specified day of Advent of Code 2023. If a local copy of your puzzle input does not exist, it will attempt to automatically download it using your session token. When complete, the answers to parts 1 and 2 will be printed to the terminal."
description2 := "Day 0 is a special day with a mock-puzzle to exercise the application before the first real puzzle unlocks."
fmt.Println(wordwrap.WrapString(description1, limit))
fmt.Println("")
fmt.Println(wordwrap.WrapString(description2, limit))
fmt.Println("")
fmt.Println("Usage:")
fmt.Println("")
fmt.Println("\tadvent-of-code-2023 <day>")
fmt.Println("")
fmt.Println("Parameters:")
fmt.Println("")
fmt.Println("\tday\t\t\tRun the solution for the specified day, i.e. 0-25.")
fmt.Println("")
fmt.Println("Environment Variables:")
fmt.Println("")
fmt.Println("\tAOC_SESSION_TOKEN\tSet your Advent of Code session token.")
fmt.Println("\tAOC_LOG_LEVEL\t\tSet the log level. Defaults to \"warn\" if not set.")
fmt.Println("")
}
func setupPuzzle() common.Puzzle {
log.Debug("Setting up puzzle.")
day := sanitizeDayArg(os.Args[1])
puzzle := common.Puzzle{
Year: 2023,
Day: day,
}
if len(os.Args) >= 3 {
puzzle.InputFile = os.Args[2]
}
switch puzzle.Day {
case 0:
puzzle.SetSolution(day00.Solve)
case 1:
puzzle.SetSolution(day01.Solve)
case 2:
puzzle.SetSolution(day02.Solve)
case 3:
puzzle.SetSolution(day03.Solve)
default:
log.Fatalf("Day %d has no solution yet.", puzzle.Day)
}
return puzzle
}
func sanitizeDayArg(arg string) int {
log.Debug("Sanitizing day argument.")
log.Tracef("arg = \"%s\"", arg)
day, err := strconv.Atoi(arg)
if err != nil {
log.Fatalf("\"%s\" is not an integer.", arg)
}
if day < 0 || day > 25 {
log.Fatalf("%d is not between 0 and 25.", day)
}
log.Tracef("day = %d", day)
return day
}