diff --git a/day_01/solution_test.go b/day_01/solution_test.go index ff50118..22ce146 100644 --- a/day_01/solution_test.go +++ b/day_01/solution_test.go @@ -2,6 +2,7 @@ package day_01_test import ( "advent_of_code_2024/day_01" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,15 +11,9 @@ import ( var BasicExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_01.SolveBasic(BasicExampleData) - if result != 11 { - t.Fatalf("Expected 11 received %d", result) - } + helpers.Check(t, day_01.SolveBasic, helpers.Format(BasicExampleData), 11) } func TestComplexSolutionExample(t *testing.T) { - result := day_01.SolveComplex(BasicExampleData) - if result != 31 { - t.Fatalf("Expected 31 received %d", result) - } + helpers.Check(t, day_01.SolveComplex, helpers.Format(BasicExampleData), 31) } diff --git a/day_02/solution_test.go b/day_02/solution_test.go index 8025b48..ba072f2 100644 --- a/day_02/solution_test.go +++ b/day_02/solution_test.go @@ -2,6 +2,7 @@ package day_02_test import ( "advent_of_code_2024/day_02" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,15 +11,9 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_02.SolveBasic(ExampleData) - if result != 2 { - t.Fatalf("Expected 2 received %d", result) - } + helpers.Check(t, day_02.SolveBasic, helpers.Format(ExampleData), 2) } func TestComplexSolutionExample(t *testing.T) { - result := day_02.SolveComplex(ExampleData) - if result != 4 { - t.Fatalf("Expected 4 received %d", result) - } + helpers.Check(t, day_02.SolveComplex, helpers.Format(ExampleData), 4) } diff --git a/day_03/solution_test.go b/day_03/solution_test.go index c062db9..12f654e 100644 --- a/day_03/solution_test.go +++ b/day_03/solution_test.go @@ -2,6 +2,7 @@ package day_03_test import ( "advent_of_code_2024/day_03" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -13,15 +14,9 @@ var ExampleData string var ComplexExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_03.SolveBasic(ExampleData) - if result != 161 { - t.Fatalf("Expected 161 received %d", result) - } + helpers.Check(t, day_03.SolveBasic, helpers.Format(ExampleData), 161) } func TestComplexSolutionExample(t *testing.T) { - result := day_03.SolveComplex(ComplexExampleData) - if result != 48 { - t.Fatalf("Expected 48 received %d", result) - } + helpers.Check(t, day_03.SolveComplex, helpers.Format(ComplexExampleData), 48) } diff --git a/day_04/solution_test.go b/day_04/solution_test.go index b5e1dfc..5f41631 100644 --- a/day_04/solution_test.go +++ b/day_04/solution_test.go @@ -2,6 +2,7 @@ package day_04_test import ( "advent_of_code_2024/day_04" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -13,15 +14,9 @@ var ExampleData string var ComplexExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_04.SolveBasic(ExampleData) - if result != 18 { - t.Fatalf("Expected 18 received %d", result) - } + helpers.Check(t, day_04.SolveBasic, helpers.Format(ExampleData), 18) } func TestComplexSolutionExample(t *testing.T) { - result := day_04.SolveComplex(ComplexExampleData) - if result != 9 { - t.Fatalf("Expected 9 received %d", result) - } + helpers.Check(t, day_04.SolveComplex, helpers.Format(ComplexExampleData), 9) } diff --git a/day_05/solution_test.go b/day_05/solution_test.go index 31a2417..6b3c96f 100644 --- a/day_05/solution_test.go +++ b/day_05/solution_test.go @@ -2,6 +2,7 @@ package day_05_test import ( "advent_of_code_2024/day_05" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,15 +11,9 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_05.SolveBasic(ExampleData) - if result != 143 { - t.Fatalf("Expected 143 received %d", result) - } + helpers.Check(t, day_05.SolveBasic, helpers.Format(ExampleData), 143) } func TestComplexSolutionExample(t *testing.T) { - result := day_05.SolveComplex(ExampleData) - if result != 123 { - t.Fatalf("Expected 123 received %d", result) - } + helpers.Check(t, day_05.SolveComplex, helpers.Format(ExampleData), 123) } diff --git a/day_06/solution_test.go b/day_06/solution_test.go index 6d5aa10..5954118 100644 --- a/day_06/solution_test.go +++ b/day_06/solution_test.go @@ -2,6 +2,7 @@ package day_06_test import ( "advent_of_code_2024/day_06" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,15 +11,9 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_06.SolveBasic(ExampleData) - if result != 41 { - t.Fatalf("Expected 41 received %d", result) - } + helpers.Check(t, day_06.SolveBasic, helpers.Format(ExampleData), 41) } func TestComplexSolutionExample(t *testing.T) { - result := day_06.SolveComplex(ExampleData) - if result != 6 { - t.Fatalf("Expected 6 received %d", result) - } + helpers.Check(t, day_06.SolveComplex, helpers.Format(ExampleData), 6) } diff --git a/day_07/solution_test.go b/day_07/solution_test.go index 58ba45b..646e44b 100644 --- a/day_07/solution_test.go +++ b/day_07/solution_test.go @@ -2,6 +2,7 @@ package day_07_test import ( "advent_of_code_2024/day_07" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,25 +11,9 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_07.SolveBasic(ExampleData) - if result != 3749 { - t.Fatalf("Expected 3749 received %d", result) - } + helpers.Check(t, day_07.SolveBasic, helpers.Format(ExampleData), 3749) } func TestComplexSolutionExample(t *testing.T) { - result := day_07.SolveComplex(ExampleData) - if result != 11387 { - t.Fatalf("Expected 11387 received %d", result) - } -} - -func TestSolvable(t *testing.T) { - if !day_07.Solvable(day_07.Equation{ - Result: 27100, - Variables: []int{3, 9, 100}, - }, 3) { - t.Fatalf("Expected true") - } - + helpers.Check(t, day_07.SolveComplex, helpers.Format(ExampleData), 11387) } diff --git a/day_08/solution_test.go b/day_08/solution_test.go index 9331286..d858efb 100644 --- a/day_08/solution_test.go +++ b/day_08/solution_test.go @@ -2,6 +2,7 @@ package day_08_test import ( "advent_of_code_2024/day_08" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -19,36 +20,21 @@ var ExampleData02 string var ExampleData03 string func TestBasicSolutionExample(t *testing.T) { - result := day_08.SolveBasic(ExampleData) - if result != 14 { - t.Fatalf("Expected 14 received %d", result) - } -} - -func TestBasicSolutionExample01(t *testing.T) { - result := day_08.SolveBasic(ExampleData01) - if result != 2 { - t.Fatalf("Expected 2 received %d", result) - } -} - -func TestBasicSolutionExample02(t *testing.T) { - result := day_08.SolveBasic(ExampleData02) - if result != 4 { - t.Fatalf("Expected 4 received %d", result) - } -} - -func TestComplexSolutionExample03(t *testing.T) { - result := day_08.SolveComplex(ExampleData03) - if result != 9 { - t.Fatalf("Expected 9 received %d", result) - } + helpers.Check(t, day_08.SolveBasic, helpers.Format(ExampleData), 14) } func TestComplexSolutionExample(t *testing.T) { - result := day_08.SolveComplex(ExampleData) - if result != 34 { - t.Fatalf("Expected 34 received %d", result) - } + helpers.Check(t, day_08.SolveComplex, helpers.Format(ExampleData), 34) +} + +func TestBasicSolutionExample01(t *testing.T) { + helpers.Check(t, day_08.SolveBasic, helpers.Format(ExampleData01), 2) +} + +func TestBasicSolutionExample02(t *testing.T) { + helpers.Check(t, day_08.SolveBasic, helpers.Format(ExampleData02), 4) +} + +func TestComplexSolutionExample03(t *testing.T) { + helpers.Check(t, day_08.SolveComplex, helpers.Format(ExampleData03), 9) } diff --git a/day_09/solution_test.go b/day_09/solution_test.go index 6114676..17f0729 100644 --- a/day_09/solution_test.go +++ b/day_09/solution_test.go @@ -2,6 +2,7 @@ package day_09_test import ( "advent_of_code_2024/day_09" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,15 +11,9 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_09.SolveBasic(ExampleData) - if result != 1928 { - t.Fatalf("Expected 1928 received %d", result) - } + helpers.Check(t, day_09.SolveBasic, helpers.Format(ExampleData), 1928) } func TestComplexSolutionExample(t *testing.T) { - result := day_09.SolveComplex(ExampleData) - if result != 2858 { - t.Fatalf("Expected 2858 received %d", result) - } + helpers.Check(t, day_09.SolveComplex, helpers.Format(ExampleData), 2858) } diff --git a/day_10/solution_test.go b/day_10/solution_test.go index daabdae..dfd4643 100644 --- a/day_10/solution_test.go +++ b/day_10/solution_test.go @@ -2,6 +2,7 @@ package day_10_test import ( "advent_of_code_2024/day_10" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -22,50 +23,29 @@ var ExampleData03 string var ExampleData04 string func TestBasicSolutionExample(t *testing.T) { - result := day_10.SolveBasic(ExampleData) - if result != 36 { - t.Fatalf("Expected 36 received %d", result) - } + helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData), 36) } func TestBasicSolutionExample01(t *testing.T) { - result := day_10.SolveBasic(ExampleData01) - if result != 2 { - t.Fatalf("Expected 2 received %d", result) - } + helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData01), 2) } func TestBasicSolutionExample02(t *testing.T) { - result := day_10.SolveBasic(ExampleData02) - if result != 3 { - t.Fatalf("Expected 3 received %d", result) - } + helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData02), 3) } func TestBasicSolutionExample03(t *testing.T) { - result := day_10.SolveBasic(ExampleData03) - if result != 4 { - t.Fatalf("Expected 4 received %d", result) - } + helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData03), 4) } func TestComplexSolutionExample(t *testing.T) { - result := day_10.SolveComplex(ExampleData) - if result != 81 { - t.Fatalf("Expected 81 received %d", result) - } + helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData), 81) } func TestComplexSolutionExample03(t *testing.T) { - result := day_10.SolveComplex(ExampleData03) - if result != 13 { - t.Fatalf("Expected 13 received %d", result) - } + helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData03), 13) } func TestComplexSolutionExample04(t *testing.T) { - result := day_10.SolveComplex(ExampleData04) - if result != 227 { - t.Fatalf("Expected 227 received %d", result) - } + helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData04), 227) } diff --git a/day_11/solution_test.go b/day_11/solution_test.go index 85fd8bf..65ff45c 100644 --- a/day_11/solution_test.go +++ b/day_11/solution_test.go @@ -2,6 +2,7 @@ package day_11_test import ( "advent_of_code_2024/day_11" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -10,8 +11,5 @@ import ( var ExampleData string func TestBasicSolutionExample(t *testing.T) { - result := day_11.SolveBasic(ExampleData) - if result != 55312 { - t.Fatalf("Expected 55312 received %d", result) - } + helpers.Check(t, day_11.SolveBasic, helpers.Format(ExampleData), 55312) } diff --git a/day_12/solution.go b/day_12/solution.go index 4423009..e653dec 100644 --- a/day_12/solution.go +++ b/day_12/solution.go @@ -234,7 +234,6 @@ func visit(farmMap Map, field Visited, x int, y int, value rune) { } func loadMap(input string) Map { - input = strings.ReplaceAll(input, "\r", "") topographicMap := Map{} for _, line := range strings.Split(input, "\n") { topographicMap = append(topographicMap, []rune(line)) diff --git a/day_12/solution_test.go b/day_12/solution_test.go index 29a5048..5c3d558 100644 --- a/day_12/solution_test.go +++ b/day_12/solution_test.go @@ -2,6 +2,7 @@ package day_12_test import ( "advent_of_code_2024/day_12" + "advent_of_code_2024/helpers" _ "embed" "testing" ) @@ -13,22 +14,13 @@ var ExampleData string var ExampleData01 string func TestBasicSolutionExample(t *testing.T) { - result := day_12.SolveBasic(ExampleData) - if result != 1930 { - t.Fatalf("Expected 1930 received %d", result) - } + helpers.Check(t, day_12.SolveBasic, helpers.Format(ExampleData), 1930) } func TestComplexSolutionExample(t *testing.T) { - result := day_12.SolveComplex(ExampleData) - if result != 1206 { - t.Fatalf("Expected 1206 received %d", result) - } + helpers.Check(t, day_12.SolveComplex, helpers.Format(ExampleData), 1206) } func TestComplexSolutionExample01(t *testing.T) { - result := day_12.SolveComplex(ExampleData01) - if result != 80 { - t.Fatalf("Expected 80 received %d", result) - } + helpers.Check(t, day_12.SolveComplex, helpers.Format(ExampleData01), 80) } diff --git a/example/data.txt b/example/data.txt new file mode 100644 index 0000000..e69de29 diff --git a/example/example_data.txt b/example/example_data.txt new file mode 100644 index 0000000..e69de29 diff --git a/example/solution.go b/example/solution.go new file mode 100644 index 0000000..f9fa2fe --- /dev/null +++ b/example/solution.go @@ -0,0 +1,11 @@ +package example + +// task:https://adventofcode.com/2024/day/13 +func SolveBasic(input string) int { + return 0 +} + +// task:https://adventofcode.com/2024/day/12#part2 +func SolveComplex(input string) int { + return 0 +} diff --git a/example/solution_test.go b/example/solution_test.go new file mode 100644 index 0000000..d8d4602 --- /dev/null +++ b/example/solution_test.go @@ -0,0 +1,19 @@ +package example_test + +import ( + "advent_of_code_2024/example" + "advent_of_code_2024/helpers" + _ "embed" + "testing" +) + +//go:embed example_data.txt +var ExampleData string + +func TestBasicSolutionExample(t *testing.T) { + helpers.Check(t, example.SolveBasic, helpers.Format(ExampleData), 0) +} + +func TestComplexSolutionExample(t *testing.T) { + helpers.Check(t, example.SolveComplex, helpers.Format(ExampleData), 0) +} diff --git a/helpers/helpers.go b/helpers/helpers.go new file mode 100644 index 0000000..5e8ab87 --- /dev/null +++ b/helpers/helpers.go @@ -0,0 +1,18 @@ +package helpers + +import ( + "strings" + "testing" +) + +type Function func(input string) int + +func Check(t *testing.T, f Function, input string, expected int) { + if actual := f(input); actual != expected { + t.Fatalf("Expected %v, got %v", expected, actual) + } +} + +func Format(input string) string { + return strings.ReplaceAll(input, "\r", "") +} diff --git a/main.go b/main.go index 6cd5255..affc8fd 100644 --- a/main.go +++ b/main.go @@ -1,83 +1,29 @@ package main import ( - "advent_of_code_2024/day_01" - "advent_of_code_2024/day_02" - "advent_of_code_2024/day_03" - "advent_of_code_2024/day_04" - "advent_of_code_2024/day_05" - "advent_of_code_2024/day_06" - "advent_of_code_2024/day_07" - "advent_of_code_2024/day_08" - "advent_of_code_2024/day_09" - "advent_of_code_2024/day_10" - "advent_of_code_2024/day_11" - "advent_of_code_2024/day_12" + "advent_of_code_2024/helpers" _ "embed" "fmt" "os" "time" ) +type Solution struct { + function helpers.Function + input string +} + func main() { selectedDay := selectSolutionRun() - start := time.Now() - - switch selectedDay { - case "01-basic": - fmt.Printf("Day 01. Basic: %d\n", day_01.SolveBasic(Day01Data)) // 2113135 - case "01-complex": - fmt.Printf("Day 01. Complex: %d\n", day_01.SolveComplex(Day01Data)) // 19097157 - case "02-basic": - fmt.Printf("Day 02. Basic: %d\n", day_02.SolveBasic(Day02Data)) // 690 - case "02-complex": - fmt.Printf("Day 02. Complex: %d\n", day_02.SolveComplex(Day02Data)) // 710 - case "03-basic": - fmt.Printf("Day 03. Basic: %d\n", day_03.SolveBasic(Day03Data)) // 189527826 - case "03-complex": - fmt.Printf("Day 03. Complex: %d\n", day_03.SolveComplex(Day03Data)) // 63013756 - case "04-basic": - fmt.Printf("Day 04. Basic: %d\n", day_04.SolveBasic(Day04Data)) // 2560 - case "04-complex": - fmt.Printf("Day 04. Complex: %d\n", day_04.SolveComplex(Day04Data)) // 1980 - case "05-basic": - fmt.Printf("Day 05. Basic: %d\n", day_05.SolveBasic(Day05Data)) // 2560 - case "05-complex": - fmt.Printf("Day 05. Complex: %d\n", day_05.SolveComplex(Day05Data)) // 63013756 - case "06-basic": - fmt.Printf("Day 06. Basic: %d\n", day_06.SolveBasic(Day06Data)) // - case "06-complex": - fmt.Printf("Day 06. Complex: %d\n", day_06.SolveComplex(Day06Data)) // - case "07-basic": - fmt.Printf("Day 07. Basic: %d\n", day_07.SolveBasic(Day07Data)) // - case "07-complex": - fmt.Printf("Day 07. Complex: %d\n", day_07.SolveComplex(Day07Data)) // - case "08-basic": - fmt.Printf("Day 08. Basic: %d\n", day_08.SolveBasic(Day08Data)) // - case "08-complex": - fmt.Printf("Day 08. Complex: %d\n", day_08.SolveComplex(Day08Data)) // - case "09-basic": - fmt.Printf("Day 09. Basic: %d\n", day_09.SolveBasic(Day09Data)) // - case "09-complex": - fmt.Printf("Day 09. Complex: %d\n", day_09.SolveComplex(Day09Data)) // - case "10-basic": - fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) // - case "10-complex": - fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) // - case "11-basic": - fmt.Printf("Day 11. Basic: %d\n", day_11.SolveBasic(Day11Data)) // - case "11-complex": - fmt.Printf("Day 11. Complex: %d\n", day_11.SolveComplex(Day11Data)) // - case "12-basic": - fmt.Printf("Day 12. Basic: %d\n", day_12.SolveBasic(Day12Data)) // - case "12-complex": - fmt.Printf("Day 12. Complex: %d\n", day_12.SolveComplex(Day12Data)) // - case "12-complex-fast": - fmt.Printf("Day 12. Complex (fast): %d\n", day_12.SolveComplexFast(Day12Data)) // + if solution, present := solutions[selectedDay]; present { + start := time.Now() + result := solution.function(helpers.Format(solution.input)) + elapsed := time.Since(start) + fmt.Printf("Solution for '%s': %d, took: %s", selectedDay, result, elapsed) + } else { + fmt.Printf("Cannot run '%s', invalid request", selectedDay) } - elapsed := time.Since(start) - fmt.Printf("Solution took: %s", elapsed) } func selectSolutionRun() string { diff --git a/solutions.go b/solutions.go new file mode 100644 index 0000000..2ec338c --- /dev/null +++ b/solutions.go @@ -0,0 +1,44 @@ +package main + +import ( + "advent_of_code_2024/day_01" + "advent_of_code_2024/day_02" + "advent_of_code_2024/day_03" + "advent_of_code_2024/day_04" + "advent_of_code_2024/day_05" + "advent_of_code_2024/day_06" + "advent_of_code_2024/day_07" + "advent_of_code_2024/day_08" + "advent_of_code_2024/day_09" + "advent_of_code_2024/day_10" + "advent_of_code_2024/day_11" + "advent_of_code_2024/day_12" +) + +var solutions = map[string]Solution{ + "01-basic": {day_01.SolveBasic, Day01Data}, + "01-complex": {day_01.SolveComplex, Day01Data}, + "02-basic": {day_02.SolveBasic, Day02Data}, + "02-complex": {day_02.SolveComplex, Day02Data}, + "03-basic": {day_03.SolveBasic, Day03Data}, + "03-complex": {day_03.SolveComplex, Day03Data}, + "04-basic": {day_04.SolveBasic, Day04Data}, + "04-complex": {day_04.SolveComplex, Day04Data}, + "05-basic": {day_05.SolveBasic, Day05Data}, + "05-complex": {day_05.SolveComplex, Day05Data}, + "06-basic": {day_06.SolveBasic, Day06Data}, + "06-complex": {day_06.SolveComplex, Day06Data}, + "07-basic": {day_07.SolveBasic, Day07Data}, + "07-complex": {day_07.SolveComplex, Day07Data}, + "08-basic": {day_08.SolveBasic, Day08Data}, + "08-complex": {day_08.SolveComplex, Day08Data}, + "09-basic": {day_09.SolveBasic, Day08Data}, + "09-complex": {day_09.SolveComplex, Day08Data}, + "10-basic": {day_10.SolveBasic, Day10Data}, + "10-complex": {day_10.SolveComplex, Day10Data}, + "11-basic": {day_11.SolveBasic, Day11Data}, + "11-complex": {day_11.SolveComplex, Day11Data}, + "12-basic": {day_12.SolveBasic, Day12Data}, + "12-complex": {day_12.SolveComplex, Day12Data}, + "12-complex-fast": {day_12.SolveComplexFast, Day12Data}, +}