29
loading...
This website collects cookies to deliver better user experience
buffer.plot_triangle((5,0), (9,9), (0,9))
buffer.plot_line((0,5), (9,5))
log2(v+1) = n
sqrt(v+1) / 2 = n
v
it's possible to have n
unique IDs. (Here we expect n
to be a byte-aligned datatype).log2(256) = 8
0000 0001 = 1
0000 0010 = 2
0000 0100 = 4
0000 1000 = 8
0001 0000 = 16
0010 0000 = 32
0100 0000 = 64
1000 0000 = 128
ID: 0000 0001
ID: 0000 0010 +
SUM 0000 0011 = 3
get_amount_of_ids()
that I wrote in the V programming language.fn main() {
mut buffer := Buffer{
width: 9
height: 9
}
buffer.data = [
byte(4),4, 4, 4, 13, 4, 4, 4, 4,
4, 0, 0, 1, 08, 1, 0, 0, 4,
4, 0, 0, 1, 08, 1, 0, 0, 4,
4, 0, 1, 0, 08, 0, 1, 0, 4,
6, 2, 3, 2, 10, 2, 3, 2, 6,
4, 1, 0, 0, 08, 0, 0, 1, 4,
4, 1, 0, 0, 08, 0, 0, 1, 4,
5, 0, 0, 0, 08, 0, 0, 0, 5,
5, 5, 5, 5, 13, 5, 5, 5, 5
]
mut possible := []byte{}
for i := 1; i < 256; i *= 2 {
possible << byte(i)
}
buffer.get_amount_of_ids(possible)
}
4
which is the amount of unique IDs we're using to draw 4 shapes.fn get_ids(arr []byte, target int) []int {
mut ids := []int
for i := 0; i < arr.len; i++ {
if (target & (1 << i)) != 0 { {
ids << arr[i]
}
}
return ids
}
get_ids()
which takes an array of bytes arr
(containing the possible unique IDs) and a target value (which is an integer / natural number). Then we also set this function to return []int
which is an array of integers, that is going to contain the overlapping IDs.mut ids := []int
for i := 0; i < arr.len; i++ {
if (target & (1 << i)) != 0 {
ids << arr[i]
}
}
amount_of_ids - 1
(0 to 7) for i := 0; i < arr.len; i++ {
(target & (1 << i))
does NOT equal 0
.&
, the binary "AND" operator.0100 0110 (70)
|
0001 0011 (19)
v
0000 0010 (2)
70 & 19 = 2
, but why is this? This is because &
only uses a bit if it's 1
in both cases. Here's a another example:1111 (15)
| |
0101 (5)
v v
0101 (5)
15
and 5
only have the first and third bit in common, therefore the output is 5
.<<
operator. This is the binary left shift operator.33: 0010 0001
<<
66: 0100 0010
a << b
a * (2^b)
17 << 3 = 17 * (2^3) = 136
>>
. This would be division instead of multiplication:a / (2^b)
for i := 0; i < arr.len; i++ {
if (target & (1 << i)) != 0 {
ids << arr[i]
}
}
for i := 0; i < arr.len; i++ {
if (target & math.pow(2, i)) != 0 {
ids << arr[i]
}
}
(target & math.pow(2, i))
. What IDs make up the top center point 13
in our merged buffers?13
is quite a small number, you might have already figured it out to be 1 + 4 + 8
. But let's try to code it:get_ids([1,2,4,8,16,32,64,128], 13)
for i := 0; i < arr.len; i++ {
if (13 & math.pow(2, i)) != 0 {
ids << arr[i]
}
}
i
starts by being 0
(corresponding to ID 1
in arr
), so let's replace math.pow(2, i)
with math.pow(2, 0)
:for i := 0; i < arr.len; i++ {
if (13 & math.pow(2, 0)) != 0 {
ids << arr[i]
}
}
math.pow(2, 0)
becomes 1
:for i := 0; i < arr.len; i++ {
if (13 & 1) != 0 {
ids << arr[i]
}
}
13 & 1
:0000 1101 (13)
& |
0000 0001 (1) math.pow(2, 0) i=0
= v
0000 0001 (1)
13
and 1
both have the first bit in common, this means that the result is 1
, and now we check if it is NOT 0
:if 1 != 0 {
ids << arr[0]
}
0
, that means we have found one of the overlapping IDs.1
. Now let's keep increasing i
:0000 1101 (13)
&
0000 0010 (2) math.pow(2, 1) i=1
=
0000 0000 (0)
13
and 2
have no bits in common.0000 1101 (13)
& |
0000 0100 (4) math.pow(2, 2) i=2
= v
0000 0100 (4)
i
is 2
, then math.pow(2, i)
is equal to 4
which has a bit in common with 13
, meaning that the next overlapping ID is 4
.i
goes through all possible IDs.0000 1101 (13)
& |
0000 1000 (8) math.pow(2, 3) i=3
= v
0000 1000 (8)
target
value, [1, 4, 8]
. We can verify this by getting the sum of 1+4+8
which is 13
, the number we wanted to get the IDs from.ids
array and then we return it at the end of the function.