If number >= 24, get next number divisible by 12 - c#

I'm working on an inventory system for a game.
If the player inventory is empty or has less than 24 items, I want show 36 slots.
If the inventory has 24 or more items, I want to find the next number divisible by 12 and use that. Basically I always want to allow at least 12 extra slots, with a minimum of 36 total.
Here's the code I've tried which is not giving me desired results:
int testInventoryCount = 29;
int itemSlotCount = 36;
int itemSlotCount2 = 36;
int itemSlotCount3 = 36;
if (testInventoryCount >= 24)
{
itemSlotCount = ((testInventoryCount / 12) + 1) * 12;
itemSlotCount2 = (testInventoryCount + 12) - (testInventoryCount % 12);
itemSlotCount3 = (testInventoryCount + 12 / 2) / 12 * 12;
}
Debug.Log(testInventoryCount);
Debug.Log(itemSlotCount);
Debug.Log(itemSlotCount2);
Debug.Log(itemSlotCount3);
None of them are giving me the correct value.
For example, this is what I want:
If the inventory has 0 items, have 36 slots.
If the inventory has 20 items, have 36 slots.
If the inventory has 26 items, have 48 slots.
If the inventory has 30 items, have 48 slots.
If the inventory has 34 items, have 48 slots.
If the inventory has 60 items, have 72 slots.
If the inventory has 66 items, have 84 slots.
Etc...
I'm terrible at math. Halp.

I wrote a simple function for getting the number of necessary slots.
public static int get_slot_count(int item_count)
{
return Math.Max(36,(item_count / 12 + 2) * 12);
}
I tested and get the following results.
0 items / 36 slots
1 items / 36 slots
2 items / 36 slots
3 items / 36 slots
4 items / 36 slots
5 items / 36 slots
6 items / 36 slots
7 items / 36 slots
8 items / 36 slots
9 items / 36 slots
10 items / 36 slots
11 items / 36 slots
12 items / 36 slots
13 items / 36 slots
14 items / 36 slots
15 items / 36 slots
16 items / 36 slots
17 items / 36 slots
18 items / 36 slots
19 items / 36 slots
20 items / 36 slots
21 items / 36 slots
22 items / 36 slots
23 items / 36 slots
24 items / 48 slots
25 items / 48 slots
26 items / 48 slots
27 items / 48 slots
28 items / 48 slots
29 items / 48 slots
30 items / 48 slots
31 items / 48 slots
32 items / 48 slots
33 items / 48 slots
34 items / 48 slots
35 items / 48 slots
36 items / 60 slots
37 items / 60 slots
38 items / 60 slots
39 items / 60 slots
40 items / 60 slots
41 items / 60 slots
42 items / 60 slots
43 items / 60 slots
44 items / 60 slots
45 items / 60 slots
46 items / 60 slots
47 items / 60 slots
48 items / 72 slots
49 items / 72 slots
I am curious why you need more than 36 slots for 24 items. If you just need extra 12 slots, the following would be better and gives more reasonable results.
public static int get_slot_count(int item_count)
{
return Math.Max(36,((item_count - 1) / 12 + 2) * 12);
}
Test results.
0 items / 36 slots
1 items / 36 slots
2 items / 36 slots
3 items / 36 slots
4 items / 36 slots
5 items / 36 slots
6 items / 36 slots
7 items / 36 slots
8 items / 36 slots
9 items / 36 slots
10 items / 36 slots
11 items / 36 slots
12 items / 36 slots
13 items / 36 slots
14 items / 36 slots
15 items / 36 slots
16 items / 36 slots
17 items / 36 slots
18 items / 36 slots
19 items / 36 slots
20 items / 36 slots
21 items / 36 slots
22 items / 36 slots
23 items / 36 slots
24 items / 36 slots
25 items / 48 slots
26 items / 48 slots
27 items / 48 slots
28 items / 48 slots
29 items / 48 slots
30 items / 48 slots
31 items / 48 slots
32 items / 48 slots
33 items / 48 slots
34 items / 48 slots
35 items / 48 slots
36 items / 48 slots
37 items / 60 slots
38 items / 60 slots
39 items / 60 slots
40 items / 60 slots
41 items / 60 slots
42 items / 60 slots
43 items / 60 slots
44 items / 60 slots
45 items / 60 slots
46 items / 60 slots
47 items / 60 slots
48 items / 60 slots
49 items / 72 slots

Related

How to get item from a list that position in a certain place

Please consider this scenario:
For some math calculations I should find a number in specific place in a sorted list. For example consider this list:
1 - 2 - 3 - ... 17 - 18 - 19 - 20
I should to find number placed in 25% of count (count / 4). In above series I should get 5. It is worth noting that we haven't round count number but it's not a problem.
Now consider this table:
Type Number
----------------------
1 10
1 11
1 12
1 13
2 22
2 23
2 24
2 25
2 26
2 27
2 28
3 39
3 38
3 37
3 36
3 35
3 34
3 33
3 32
4 41
4 43
4 42
4 44
4 45
4 47
4 46
4 48
4 49
4 50
4 51
Another point is I'm sure that in every Type I have at least 1000
numbers, so above data in just for example.
according to above data I want to get this result:
Type Number
----------------------
1 11
2 23
3 33
4 43
One way to achieve this result is to loop throw distinct Type and get list of number and then sort it and then calculate count of that list and divide it by 4, then round the result and get specific Number with the index has been gotten.
But the problem with this approach is it needs many connection to database (1 for each Type). Is there any better solution to get desired result with 1 connection and 1 query execution. thanks
Interesting puzzle. In Sql Server you could use something like the following query;
select a.*
from (
select *, row_number() over(partition by type order by number) as row_number
from table_name
) a
join (
select type, count(*) as count
from table_name
group by type
) b on a.type = b.type
where a.row_number = b.count/4
(With whatever rounding you want for when count%4 != 0)
But I can't think how you would build that as a linq expression.
var percent = 0.25;
var val = res.GroupBy(x => x.type)
.ToDictionary(x => x.Key, x => x.OrderBy(y=>y).ToList());
var valuesTobeTaken = val.Select(x => new
{
x.Key,
index = ((int)Math.Round(x.Value.Count * percent))-1
});
Edge cases are not handled and the code is not too much optimized. You can work on that i guess
foreach (var rec in valuesTobeTaken)
{
Console.WriteLine(val[rec.Key][rec.index]);
}

How to sum a column id datagridview?

I apologize for not writing any code, I just put this example to explain what is required, all I want is to sum values from the column "Oil_Qt" for each "Fix_No" so that the result is as shown in the three lines below:
ID
Fix_No
Fix_Name
Car_Number
Oil_Qt
1
1
Change Engine Oil
77
20
2
2
Change Gearbox Oil
60
6
3
2
Change Gearbox Oil
30
4
4
3
Change Hydraulic Oil
80
32
5
1
Change Engine Oil
80
15
6
1
Change Engine Oil
50
18
7
3
Change Hydraulic Oil
35
10
8
1
Change Engine Oil
35
15
Sum of Engine Oil = 68
Sum of Gearbox Oil = 10
Sum of Hydraulic Oil = 42
I tried this code but it didn't work:
for (int i = 0; i < dgOils.Rows.Count; ++i)
{
sum += Convert.ToInt32(dgOils.Rows[i].Cells[4].Value);
}
txtEngineOils.Text = sum.ToString();```
var gridDataSource = (DataTable)[YourDataGridView].Datasource;
var sumEngineOil = gridDataSource.Compute("Sum[Oil_Qt]", "[Fix_No]=1");
var sumGearboxOil = gridDataSource.Compute("Sum[Oil_Qt]", "[Fix_No]=2");
var sumHydraulicOil = gridDataSource.Compute("Sum[Oil_Qt]", "[Fix_No]=3");

What kind of Algorithm am I looking for to combine quantities?

I have been stuck on this problem now for 8 weeks and I think that I almost have a solution however the last bit of math is racking my mind. I will try to explain a simple problem that requires a complex solution. I am programing in C#.net MVC Web Project. Here is the situation.
I have an unknown group of quantities incoming to look for like items. Those like items share a max level to make it a full box. Here is an example of this:
Revision******
This is the real world case
I have many, let say candy, orders coming in to a company.
Qty Item MaxFill Sold-To DeliverNumber
60 candy#14 26 Joe 1
1 candy#12 48 Jim 2
30 candy#11 48 Jo 3
60 candy#15 48 Tom 4
6 candy#8 48 Kat 5
30 candy#61 48 Kim 6
44 candy#12 48 Jan 7
10 candy#12 48 Yai 8
10 candy#91 48 Jun 9
55 candy#14 26 Qin 10
30 candy#14 26 Yo 11
40 candy#14 26 Moe 12
in this list I am looking for like candy items to combine to make all the full boxes of candy that I can based off the MaxFill number. Here we see the like items are:
Qty Item MaxFill Sold-To DeliverNumber
60 candy#14 26 Joe 1
55 candy#14 26 Qin 10
30 candy#14 26 Yo 11
40 candy#14 26 Moe 12
1 candy#12 48 Jim 2
44 candy#12 48 Jan 7
10 candy#12 48 Yai 8
Now lets take the first set of numbers for candy#14.
I know that the total of candy#14 is 185 and I can get 7 full boxes of 26 with one box having only 3 in the last box. So how do I do this with the values that I have without losing the information of the original order. So this is how I am working it out right now
See below
End of Revision******
Like candy#14 max fill level is 26.
Like candy#14 quantities:
60
55
30
40
Now I already have a recursive function to break these down to the 26 level and is working fine. I feel that I need another recursive function to deal with the remainders that come out of this. As you can see most of the time there will be remainders from any given list but those remainders could total up to another full box of 26.
60 = 26+26+8
55 = 26+26+3
30 = 26+4
40 = 26+14
The 8,3,4,14 = 29 so I can get another 26 out of this. But in the real unknown world I could have the remainders come up with a new set of remainders that could repeat the same situation. To make this even more complicated I have to save the data that is originality with the 60,55,30,40 that is carried with it such as who it was sold to and delivery number. This will also be helpful with knowing how the original amount was broken down and combined together.
from the 8,3,4,14 the best way that I was think to add to that value is to take the 8,4,14 this will give me the 26 that I am looking for and I would not have to split any value because 3 is the remainder and I could save all other data without issue. However this just works in this situation only. If I go in a linear motion 8+3+4=15 so I would have to take 11 from the next value 14 with a remainder of 3.
In reading about different algorithms I was thinking that this might fall into the NP,NP-Complete,NP-Hard category. But with all the situations it is very technical and not a lot of real world scenarios are to be found.
Any suggestions would help here if I should go through the list of number to find the best combinations to reach the 26 or if the linear progression and splitting of the next value is the best solution. I know that I can solve to get how many full boxes I could get from the remainders and what the left over amount would be such as 8+3+4+14=29 which would give me 1, 26 and 1, 3 but I have no idea about the math in a recursive way to solve this. I have this much done and I "feel" that this is on the right track but can't see how to adjust to make this work with the linear or "test every possible combination".
public static void Main(string[] args)
{
var numbers = new List<int>() { 8, 3, 4, 14 };
var target = 26;
sum_up(numbers, target);
}
private static void sum_up(List<int> numbers, int target)
{
sum_up_recursive(numbers, target, new List<int>());
}
private static void sum_up_recursive(List<int> numbers, int target, List<int> partial)
{
int s = 0;
foreach (int x in partial) s += x;
if (s == target)
{
var outputtext = "sum(" + string.Join(",", partial.ToArray()) + ")=" + target;
}
if (s >= target)
return;
for (int i = 0; i < numbers.Count; i++)
{
List<int> remaining = new List<int>();
int n = numbers[i];
for (int j = i + 1; j < numbers.Count; j++) remaining.Add(numbers[j]);
List<int> partial_rec = new List<int>(partial);
partial_rec.Add(n);
sum_up_recursive(remaining, target, partial_rec);
}
}
I wrote sample project in javascript.
Please check my repo.
https://github.com/panghea/packaging_sample

C# Translating a few smaller arrays to one big

Title may not explain fully what I want to do, so I made an image.
You can see there are 4 1D arrays(red numbers, black colored numbers are indexes), each of this array goes from 0 to 63. I want to somehow translate them, that for example, index 16 will point to first index of second array.
What I was thinking of, is a function where I give List of arrays and index that I want to get as input, and its returns me the index of array and exact index in this array as output.
I would like to have some hints or suggestions on how to proceed here to achieve this functionality.
Ok, your image shows an interleaved data of 16 elements, so you want to have (showing an example of only two matrices because of space :D)
Global index
0 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
-----------------------------------------------------------------------------------------------------
Array0 indexes - Array1 indexes
0 1 2 3 4 5 6 7 8 9 A B C D E F - 0 1 2 3 4 5 6 7 8 9 A B C D E F
-------------------------------------------------------------------------------------------------
Global index
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
-----------------------------------------------------------------------------------------------------
Array0 indexes - Array1 indexes
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F - 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
To get it you can do something like this:
public class CompositeIndex
{
public int ArrayNumber { get; set; }
public int ElementNumber { get; set; }
}
public static CompositeIndex GetIndex(int GlobalIndex, int ArrayCount, int ElementsToInterleave)
{
CompositeIndex index = new CompositeIndex();
int fullArrays = GlobalIndex / ElementsToInterleave; //In your example: 16 / 16 = 1;
index.ArrayNumber = fullArrays % ArrayCount; //In your example: 1 mod 4 = 1;
index.ElementNumber = GlobalIndex - (fullArrays * ElementsToInterleave); //In your example: 16 - (1 * 16) = 0;
return index;
}
Then, if you have 4 matrices and want to get the "global" index 16 you do:
var index = GetIndex(16, 4, 16);
This function allows you to use an indeterminated number of arrays and interleaved elements.
BTW, another time ask better your question, a lot more people will help you if they don't have to solve a puzzles to understand what you want...

Spliting into lines then into variables

i have this text file below:
001 Bulbasaur 45 49 49 65 65 45 Grass Poison
002 Ivysaur 60 62 63 80 80 60 Grass Poison
003 Venusaur 80 82 83 100 100 80 Grass Poison
004 Charmander 39 52 43 60 50 65 Fire
005 Charmeleon 58 64 58 80 65 80 Fire
I have written this piece of code to split it into lines then into variables but it refuses to work, my apology's if i am asking this in the wrong place. (unity C# question).
var lines = textFile.text.Split("\n"[0]);
allMonsters = new Monsters[lines.Length];
List<string> lineSplit = new List<string>();
for (int i = 0; i < lines.Length; i++) {
Debug.Log(lines[i]);
lineSplit.Clear();
lineSplit = lines[i].Split(' ').ToList ();
int ID = int.Parse(lineSplit[0]);
string Name = lineSplit[1].ToString();
float HP = float.Parse(lineSplit[2]);
float ATK = float.Parse(lineSplit[3]);
float DEF = float.Parse(lineSplit[4]);
float SPATK = float.Parse(lineSplit[5]);
float SpDEF = float.Parse(lineSplit[6]);
float speed = float.Parse(lineSplit[7]);
FirstType Ft = (FirstType)System.Enum.Parse(typeof(FirstType),lineSplit[8]);
SecondType ST = (SecondType)System.Enum.Parse(typeof(SecondType),lineSplit[9]); }
The code works for the first line but then on the second run of this code i get null reference to an object error, please help me.
Note, variables are assigned to so they aren't overwritten after code.
EDIT: LineSplit variable is over 1200 elements long, so i do not think unity is clearing the array properly could this be the issue?
I'm not sure about the second iteration, but on the 4th iteration line 004 Charmander 39 52 43 60 50 65 Fire have only 9 parameters (by split of space) and you using lineSplit[9], so there you will get NullPointerException.
When working with text files, linefeed often comes along with carriage return.
Try
var lines = textFile.text.Split(new string[]{"\r\n"}, System.StringSplitOptions.None);
in the first line.

Categories

Resources