From 66a9f3f9e60228373bee52baf25ebc3c471d1b17 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 5 Dec 2024 22:51:31 +0200 Subject: [PATCH] clean up main --- main.go | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index ae5502a..a1d1d52 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,12 @@ func showUsage() { fmt.Printf("Usage: %s [input.txt]\n", os.Args[0]) } +type DayPartSolver func([]string) error +type DaySolver struct { + day1 DayPartSolver + day2 DayPartSolver +} + func main() { if (len(os.Args) < 3) { showUsage() @@ -35,29 +41,26 @@ func main() { lines := strings.Split(strings.TrimRight(string(contents), "\n"), "\n") - if day == "1" { + solvers := make(map[string]DaySolver) + solvers["1"] = DaySolver{ day1: Day1Part1, day2: Day1Part2 } + solvers["2"] = DaySolver{ day1: Day2Part1, day2: Day2Part2 } + solvers["3"] = DaySolver{ day1: Day3Part1, day2: Day3Part2 } + solvers["4"] = DaySolver{ day1: Day4Part1, day2: Day4Part2 } + + solver, ok := solvers[day] + + if ok { + var err error if part == "1" { - Day1Part1(lines) + err = solver.day1(lines) } else if part == "2" { - Day1Part2(lines) + err = solver.day1(lines) } - } else if day == "2" { - if part == "1" { - Day2Part1(lines) - } else if part == "2" { - Day2Part2(lines) - } - } else if day == "3" { - if part == "1" { - Day3Part1(lines) - } else if part == "2" { - Day3Part2(lines) - } - } else if day == "4" { - if part == "1" { - Day4Part1(lines) - } else if part == "2" { - Day4Part2(lines) + + if err != nil { + log.Fatal(err) } + } else { + log.Fatal("Day not found") } }