github.com/mnlphlp/aoc22@v0.0.0-20230330151331-c1dc4bff1b9b/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "strconv" 8 "strings" 9 "sync" 10 "time" 11 12 "github.com/mnlphlp/aoc22/day01" 13 "github.com/mnlphlp/aoc22/day02" 14 "github.com/mnlphlp/aoc22/day03" 15 "github.com/mnlphlp/aoc22/day04" 16 "github.com/mnlphlp/aoc22/day05" 17 "github.com/mnlphlp/aoc22/day06" 18 "github.com/mnlphlp/aoc22/day07" 19 "github.com/mnlphlp/aoc22/day08" 20 "github.com/mnlphlp/aoc22/day09" 21 "github.com/mnlphlp/aoc22/day10" 22 "github.com/mnlphlp/aoc22/day11" 23 "github.com/mnlphlp/aoc22/day12" 24 "github.com/mnlphlp/aoc22/day13" 25 "github.com/mnlphlp/aoc22/day14" 26 "github.com/mnlphlp/aoc22/day15" 27 "github.com/mnlphlp/aoc22/day16" 28 "github.com/mnlphlp/aoc22/day17" 29 "github.com/mnlphlp/aoc22/day18" 30 "github.com/mnlphlp/aoc22/day19" 31 "github.com/mnlphlp/aoc22/day20" 32 "github.com/mnlphlp/aoc22/day21" 33 "github.com/mnlphlp/aoc22/day22" 34 "github.com/mnlphlp/aoc22/day23" 35 "github.com/mnlphlp/aoc22/day24" 36 "github.com/mnlphlp/aoc22/day25" 37 "github.com/mnlphlp/aoc22/util" 38 ) 39 40 var dayFuncs = [...]func(string, bool, int) (string, string){ 41 day01.Solve, 42 day02.Solve, 43 day03.Solve, 44 day04.Solve, 45 day05.Solve, 46 day06.Solve, 47 day07.Solve, 48 day08.Solve, 49 day09.Solve, 50 day10.Solve, 51 day11.Solve, 52 day12.Solve, 53 day13.Solve, 54 day14.Solve, 55 day15.Solve, 56 day16.Solve, 57 day17.Solve, 58 day18.Solve, 59 day19.Solve, 60 day20.Solve, 61 day21.Solve, 62 day22.Solve, 63 day23.Solve, 64 day24.Solve, 65 day25.Solve, 66 } 67 68 func capLength(str string, length int) string { 69 if len(str) > length { 70 return str[:length-3] + "..." 71 } 72 return str 73 } 74 75 func calcDay(day int, i int, results1 []string, results2 []string, times []time.Duration, test bool, task int, debug bool) { 76 fmt.Printf("\n##################\ncalculating day %d \n##################\n", day) 77 start := time.Now() 78 input := util.ReadInput(day, test) 79 res1, res2 := dayFuncs[day-1](input, debug, task) 80 times[i] = time.Since(start) 81 results1[i] = res1 82 results2[i] = res2 83 } 84 85 func main() { 86 dayStr := flag.String("d", "", "day") 87 daysString := flag.String("days", "", "days") 88 test := flag.Bool("t", false, "use test input") 89 task := flag.Int("task", 0, "task (0=both, 1=task1, 2=task2)") 90 updateReadme := flag.Bool("readme", false, "updateReadme") 91 parallel := flag.Bool("p", false, "run in parallel (one goroutine per day)") 92 debug := flag.Bool("debug", false, "debug flag for more printing") 93 flag.Parse() 94 *dayStr = strings.Trim(*dayStr, "day.go") 95 day, _ := strconv.Atoi(*dayStr) 96 days := []int{} 97 if day != 0 { 98 days = append(days, day) 99 } 100 if *daysString != "" { 101 for _, day := range strings.Split(*daysString, ",") { 102 d, _ := strconv.Atoi(day) 103 days = append(days, d) 104 } 105 } 106 // if nothin is set, run all days 107 if len(days) == 0 { 108 for i := 1; i <= len(dayFuncs); i++ { 109 days = append(days, i) 110 } 111 } 112 113 fmt.Printf("calculating days: %v \n", days) 114 115 results1 := make([]string, len(days)) 116 results2 := make([]string, len(days)) 117 times := make([]time.Duration, len(days)) 118 119 start := time.Now() 120 if *parallel { 121 wg := sync.WaitGroup{} 122 for i, day := range days { 123 wg.Add(1) 124 go func(i int, day int) { 125 defer wg.Done() 126 calcDay(day, i, results1, results2, times, *test, *task, *debug) 127 }(i, day) 128 } 129 wg.Wait() 130 } else { 131 for i, day := range days { 132 calcDay(day, i, results1, results2, times, *test, *task, *debug) 133 } 134 } 135 overall := time.Since(start) 136 137 results := "## Results:\n" 138 results += "day | result 1 | result 2 | time (ms) | % of overall time\n" 139 results += "--: | :-------------: | :--------------:| --------: | :--------\n" 140 for i, day := range days { 141 results += fmt.Sprintf("%3d | %-15s | %-15s | %9.2f | %5.2f %%\n", 142 day, 143 capLength(results1[i], 15), 144 capLength(results2[i], 15), 145 float32(times[i].Microseconds())/1000, 146 float32(times[i].Microseconds())/float32(overall.Microseconds())*100) 147 } 148 results += fmt.Sprintf("\nOverall Time: %v\n", overall) 149 results += fmt.Sprintf("\nSummed Time: %v\n", util.Sum(times...)) 150 if *updateReadme { 151 content, _ := os.ReadFile("README.md") 152 startIndex := strings.Index(string(content), "## Results:\n") 153 endIndex := strings.Index(string(content), "Summed Time:") 154 start := []byte{} 155 if startIndex >= 0 { 156 start = content[:startIndex] 157 } 158 end := []byte{} 159 if endIndex >= 0 { 160 endIndex += strings.Index(string(content[endIndex:]), "\n") 161 end = content[endIndex:] 162 } 163 os.WriteFile("README.md", append(start, append([]byte(results), end...)...), 0644) 164 } 165 fmt.Printf("\n%s", results) 166 }