summaryrefslogtreecommitdiff
path: root/go/2023/internal/day_03/part_01/part_01_test.go
blob: a077fbd74dc894ee8bae1d05b05696052152eabf (plain)
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
package part_01

import (
	"os"
	"strings"
	"testing"
)

func TestWalkLines(t *testing.T) {
	lines := getSampleLines()
	expected := 4361

	validNumbers := walkLines(lines)

	actual := 0
	for _, number := range validNumbers {
		actual += number
	}

	if actual != expected {
		t.Errorf("Expected answer to be %d, got %d", expected, actual)
	}
}

func TestCheckValidity(t *testing.T) {
	lines := getSampleLines()

	tests := []struct {
		x        int
		y        int
		length   int
		expected bool
	}{
		{3, 0, 3, true},  // 467
		{8, 0, 3, false}, // 114
		{9, 0, 1, false}, //   3 - Added to check for '\r' counting as a symbol
		{4, 2, 2, true},  //  35
		{9, 2, 3, true},  // 633
		{9, 3, 1, false}, //   3 - Added to check if number continues to next line
		{3, 4, 3, true},  // 617
		{9, 5, 2, false}, //  58
		{5, 6, 3, true},  // 592
		{9, 7, 3, true},  // 775
		{4, 9, 3, true},  // 664
		{8, 9, 3, true},  // 598
	}

	for _, test := range tests {
		actual := checkValidity(lines, test.x, test.y, test.length)

		if actual != test.expected {
			t.Errorf("Expected %d, %d, %d to be %t, got %t", test.x, test.y, test.length, test.expected, actual)
		}
	}
}

func getSampleLines() []string {
	contents, err := os.ReadFile("../../../../_input/day_03_part_01_sample.txt")
	if err != nil {
		panic(err)
	}

	lines := strings.Split(string(contents), "\n")

	var filteredLines []string
	for _, line := range lines {
		if line == "" || line == "\n" {
			continue
		}

		// Counts as a symbol...
		filteredLine := strings.ReplaceAll(line, "\r", "")

		filteredLines = append(filteredLines, filteredLine)
	}

	return filteredLines
}