Cleanup and reorganization

This commit is contained in:
Borna Rajković 2024-12-12 22:12:54 +01:00
parent 0af5c11e6d
commit 2107b9c990
20 changed files with 158 additions and 215 deletions

View File

@ -2,6 +2,7 @@ package day_01_test
import ( import (
"advent_of_code_2024/day_01" "advent_of_code_2024/day_01"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,15 +11,9 @@ import (
var BasicExampleData string var BasicExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_01.SolveBasic(BasicExampleData) helpers.Check(t, day_01.SolveBasic, helpers.Format(BasicExampleData), 11)
if result != 11 {
t.Fatalf("Expected 11 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_01.SolveComplex(BasicExampleData) helpers.Check(t, day_01.SolveComplex, helpers.Format(BasicExampleData), 31)
if result != 31 {
t.Fatalf("Expected 31 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_02_test
import ( import (
"advent_of_code_2024/day_02" "advent_of_code_2024/day_02"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,15 +11,9 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_02.SolveBasic(ExampleData) helpers.Check(t, day_02.SolveBasic, helpers.Format(ExampleData), 2)
if result != 2 {
t.Fatalf("Expected 2 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_02.SolveComplex(ExampleData) helpers.Check(t, day_02.SolveComplex, helpers.Format(ExampleData), 4)
if result != 4 {
t.Fatalf("Expected 4 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_03_test
import ( import (
"advent_of_code_2024/day_03" "advent_of_code_2024/day_03"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -13,15 +14,9 @@ var ExampleData string
var ComplexExampleData string var ComplexExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_03.SolveBasic(ExampleData) helpers.Check(t, day_03.SolveBasic, helpers.Format(ExampleData), 161)
if result != 161 {
t.Fatalf("Expected 161 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_03.SolveComplex(ComplexExampleData) helpers.Check(t, day_03.SolveComplex, helpers.Format(ComplexExampleData), 48)
if result != 48 {
t.Fatalf("Expected 48 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_04_test
import ( import (
"advent_of_code_2024/day_04" "advent_of_code_2024/day_04"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -13,15 +14,9 @@ var ExampleData string
var ComplexExampleData string var ComplexExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_04.SolveBasic(ExampleData) helpers.Check(t, day_04.SolveBasic, helpers.Format(ExampleData), 18)
if result != 18 {
t.Fatalf("Expected 18 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_04.SolveComplex(ComplexExampleData) helpers.Check(t, day_04.SolveComplex, helpers.Format(ComplexExampleData), 9)
if result != 9 {
t.Fatalf("Expected 9 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_05_test
import ( import (
"advent_of_code_2024/day_05" "advent_of_code_2024/day_05"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,15 +11,9 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_05.SolveBasic(ExampleData) helpers.Check(t, day_05.SolveBasic, helpers.Format(ExampleData), 143)
if result != 143 {
t.Fatalf("Expected 143 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_05.SolveComplex(ExampleData) helpers.Check(t, day_05.SolveComplex, helpers.Format(ExampleData), 123)
if result != 123 {
t.Fatalf("Expected 123 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_06_test
import ( import (
"advent_of_code_2024/day_06" "advent_of_code_2024/day_06"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,15 +11,9 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_06.SolveBasic(ExampleData) helpers.Check(t, day_06.SolveBasic, helpers.Format(ExampleData), 41)
if result != 41 {
t.Fatalf("Expected 41 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_06.SolveComplex(ExampleData) helpers.Check(t, day_06.SolveComplex, helpers.Format(ExampleData), 6)
if result != 6 {
t.Fatalf("Expected 6 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_07_test
import ( import (
"advent_of_code_2024/day_07" "advent_of_code_2024/day_07"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,25 +11,9 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_07.SolveBasic(ExampleData) helpers.Check(t, day_07.SolveBasic, helpers.Format(ExampleData), 3749)
if result != 3749 {
t.Fatalf("Expected 3749 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_07.SolveComplex(ExampleData) helpers.Check(t, day_07.SolveComplex, helpers.Format(ExampleData), 11387)
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")
}
} }

View File

@ -2,6 +2,7 @@ package day_08_test
import ( import (
"advent_of_code_2024/day_08" "advent_of_code_2024/day_08"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -19,36 +20,21 @@ var ExampleData02 string
var ExampleData03 string var ExampleData03 string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_08.SolveBasic(ExampleData) helpers.Check(t, day_08.SolveBasic, helpers.Format(ExampleData), 14)
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)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_08.SolveComplex(ExampleData) helpers.Check(t, day_08.SolveComplex, helpers.Format(ExampleData), 34)
if result != 34 { }
t.Fatalf("Expected 34 received %d", result)
} 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)
} }

View File

@ -2,6 +2,7 @@ package day_09_test
import ( import (
"advent_of_code_2024/day_09" "advent_of_code_2024/day_09"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,15 +11,9 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_09.SolveBasic(ExampleData) helpers.Check(t, day_09.SolveBasic, helpers.Format(ExampleData), 1928)
if result != 1928 {
t.Fatalf("Expected 1928 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_09.SolveComplex(ExampleData) helpers.Check(t, day_09.SolveComplex, helpers.Format(ExampleData), 2858)
if result != 2858 {
t.Fatalf("Expected 2858 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_10_test
import ( import (
"advent_of_code_2024/day_10" "advent_of_code_2024/day_10"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -22,50 +23,29 @@ var ExampleData03 string
var ExampleData04 string var ExampleData04 string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_10.SolveBasic(ExampleData) helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData), 36)
if result != 36 {
t.Fatalf("Expected 36 received %d", result)
}
} }
func TestBasicSolutionExample01(t *testing.T) { func TestBasicSolutionExample01(t *testing.T) {
result := day_10.SolveBasic(ExampleData01) helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData01), 2)
if result != 2 {
t.Fatalf("Expected 2 received %d", result)
}
} }
func TestBasicSolutionExample02(t *testing.T) { func TestBasicSolutionExample02(t *testing.T) {
result := day_10.SolveBasic(ExampleData02) helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData02), 3)
if result != 3 {
t.Fatalf("Expected 3 received %d", result)
}
} }
func TestBasicSolutionExample03(t *testing.T) { func TestBasicSolutionExample03(t *testing.T) {
result := day_10.SolveBasic(ExampleData03) helpers.Check(t, day_10.SolveBasic, helpers.Format(ExampleData03), 4)
if result != 4 {
t.Fatalf("Expected 4 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_10.SolveComplex(ExampleData) helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData), 81)
if result != 81 {
t.Fatalf("Expected 81 received %d", result)
}
} }
func TestComplexSolutionExample03(t *testing.T) { func TestComplexSolutionExample03(t *testing.T) {
result := day_10.SolveComplex(ExampleData03) helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData03), 13)
if result != 13 {
t.Fatalf("Expected 13 received %d", result)
}
} }
func TestComplexSolutionExample04(t *testing.T) { func TestComplexSolutionExample04(t *testing.T) {
result := day_10.SolveComplex(ExampleData04) helpers.Check(t, day_10.SolveComplex, helpers.Format(ExampleData04), 227)
if result != 227 {
t.Fatalf("Expected 227 received %d", result)
}
} }

View File

@ -2,6 +2,7 @@ package day_11_test
import ( import (
"advent_of_code_2024/day_11" "advent_of_code_2024/day_11"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -10,8 +11,5 @@ import (
var ExampleData string var ExampleData string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_11.SolveBasic(ExampleData) helpers.Check(t, day_11.SolveBasic, helpers.Format(ExampleData), 55312)
if result != 55312 {
t.Fatalf("Expected 55312 received %d", result)
}
} }

View File

@ -234,7 +234,6 @@ func visit(farmMap Map, field Visited, x int, y int, value rune) {
} }
func loadMap(input string) Map { func loadMap(input string) Map {
input = strings.ReplaceAll(input, "\r", "")
topographicMap := Map{} topographicMap := Map{}
for _, line := range strings.Split(input, "\n") { for _, line := range strings.Split(input, "\n") {
topographicMap = append(topographicMap, []rune(line)) topographicMap = append(topographicMap, []rune(line))

View File

@ -2,6 +2,7 @@ package day_12_test
import ( import (
"advent_of_code_2024/day_12" "advent_of_code_2024/day_12"
"advent_of_code_2024/helpers"
_ "embed" _ "embed"
"testing" "testing"
) )
@ -13,22 +14,13 @@ var ExampleData string
var ExampleData01 string var ExampleData01 string
func TestBasicSolutionExample(t *testing.T) { func TestBasicSolutionExample(t *testing.T) {
result := day_12.SolveBasic(ExampleData) helpers.Check(t, day_12.SolveBasic, helpers.Format(ExampleData), 1930)
if result != 1930 {
t.Fatalf("Expected 1930 received %d", result)
}
} }
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_12.SolveComplex(ExampleData) helpers.Check(t, day_12.SolveComplex, helpers.Format(ExampleData), 1206)
if result != 1206 {
t.Fatalf("Expected 1206 received %d", result)
}
} }
func TestComplexSolutionExample01(t *testing.T) { func TestComplexSolutionExample01(t *testing.T) {
result := day_12.SolveComplex(ExampleData01) helpers.Check(t, day_12.SolveComplex, helpers.Format(ExampleData01), 80)
if result != 80 {
t.Fatalf("Expected 80 received %d", result)
}
} }

0
example/data.txt Normal file
View File

0
example/example_data.txt Normal file
View File

11
example/solution.go Normal file
View File

@ -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
}

19
example/solution_test.go Normal file
View File

@ -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)
}

18
helpers/helpers.go Normal file
View File

@ -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", "")
}

80
main.go
View File

@ -1,83 +1,29 @@
package main package main
import ( import (
"advent_of_code_2024/day_01" "advent_of_code_2024/helpers"
"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"
_ "embed" _ "embed"
"fmt" "fmt"
"os" "os"
"time" "time"
) )
type Solution struct {
function helpers.Function
input string
}
func main() { func main() {
selectedDay := selectSolutionRun() selectedDay := selectSolutionRun()
start := time.Now() if solution, present := solutions[selectedDay]; present {
start := time.Now()
switch selectedDay { result := solution.function(helpers.Format(solution.input))
case "01-basic": elapsed := time.Since(start)
fmt.Printf("Day 01. Basic: %d\n", day_01.SolveBasic(Day01Data)) // 2113135 fmt.Printf("Solution for '%s': %d, took: %s", selectedDay, result, elapsed)
case "01-complex": } else {
fmt.Printf("Day 01. Complex: %d\n", day_01.SolveComplex(Day01Data)) // 19097157 fmt.Printf("Cannot run '%s', invalid request", selectedDay)
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)) //
} }
elapsed := time.Since(start)
fmt.Printf("Solution took: %s", elapsed)
} }
func selectSolutionRun() string { func selectSolutionRun() string {

44
solutions.go Normal file
View File

@ -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},
}