aboutsummaryrefslogtreecommitdiff
path: root/modules/numbers.go
blob: 2d19b9fbe376a9bc96716628e88fa608c647ce39 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package modules

import (
	"errors"
)

type Pattern [][]bool

var DigitPatterns = map[int]Pattern{
	0: {
		{true, true, true},
		{true, false, true},
		{true, false, true},
		{true, false, true},
		{true, true, true},
	},
	1: {
		{false, true, false},
		{true, true, false},
		{false, true, false},
		{false, true, false},
		{true, true, true},
	},
	2: {
		{true, true, true},
		{false, false, true},
		{true, true, true},
		{true, false, false},
		{true, true, true},
	},
	3: {
		{true, true, true},
		{false, false, true},
		{true, true, true},
		{false, false, true},
		{true, true, true},
	},
	4: {
		{true, false, true},
		{true, false, true},
		{true, true, true},
		{false, false, true},
		{false, false, true},
	},
	5: {
		{true, true, true},
		{true, false, false},
		{true, true, true},
		{false, false, true},
		{true, true, true},
	},
	6: {
		{true, true, true},
		{true, false, false},
		{true, true, true},
		{true, false, true},
		{true, true, true},
	},
	7: {
		{true, true, true},
		{false, false, true},
		{false, true, false},
		{false, true, false},
		{false, true, false},
	},
	8: {
		{true, true, true},
		{true, false, true},
		{true, true, true},
		{true, false, true},
		{true, true, true},
	},
	9: {
		{true, true, true},
		{true, false, true},
		{true, true, true},
		{false, false, true},
		{true, true, true},
	},
}

var SymbolPatterns = map[string]Pattern{
	"celsius": {
		{true, false, false, false, false},
		{false, false, true, true, false},
		{false, true, false, false, false},
		{false, true, false, false, false},
		{false, false, true, true, false},
	},
	"fahrenheit": {
		{true, false, true, true, false},
		{false, false, true, false, false},
		{false, false, true, true, false},
		{false, false, true, false, false},
		{false, false, true, false, false},
	},
	"percent": {
		{false, false, false, false, false},
		{false, true, false, false, true},
		{false, false, false, true, false},
		{false, false, true, false, false},
		{false, true, false, false, true},
	},
}

func InsertPattern(grid [][]bool, pattern Pattern, row, col int) {
	for i, rowPattern := range pattern {
		for j, val := range rowPattern {
			if row+i < len(grid) && col+j < len(grid[0]) {
				grid[row+i][col+j] = val
			}
		}
	}
}

func CreateNumberGrid(value int, symbol string, row int) ([][]bool, error) {
	if value < 0 || value >= 1000 {
		return nil, errors.New("value must be between 0 and 999")
	}
	if _, ok := SymbolPatterns[symbol]; !ok {
		return nil, errors.New("unsupported symbol")
	}

	grid := make([][]bool, 14)
	for i := range grid {
		grid[i] = make([]bool, 14)
	}

	var (
		digits    []int
		symbolCol int
	)

	if value < 100 {
		digits = []int{value / 10, value % 10}
		symbolCol = 9
	} else {
		digits = []int{value / 100, (value % 100) / 10, value % 10}
		symbolCol = 13
	}

	col := 1
	for _, digit := range digits {
		InsertPattern(grid, DigitPatterns[digit], row, col)
		col += 4
	}
	InsertPattern(grid, SymbolPatterns[symbol], row, symbolCol)

	return grid, nil
}