Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //june-22 harder one
- //killing this brought back my confidence
- class CellularNetworkTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- String[] cells = scanner.nextLine().split("\\s+");
- String[] cellIds = new String[cells.length];
- int[] capacities = new int[cells.length];
- for (int i = 0; i < cells.length; ++i) {
- String[] parts = cells[i].split(":");
- cellIds[i] = parts[0];
- capacities[i] = Integer.parseInt(parts[1]);
- }
- CellularNetwork cellularNetwork = new CellularNetwork(name, cellIds, capacities);
- int n = scanner.nextInt();
- scanner.nextLine();
- System.out.println("----- Making calls -----");
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split("\\s+");
- try {
- cellularNetwork.makeCall(parts[0], parts[1]);
- } catch (CellFullException e) {
- System.out.println(e.getMessage());
- }
- }
- System.out.println();
- n = scanner.nextInt();
- scanner.nextLine();
- System.out.println("----- Making handovers -----");
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split("\\s+");
- try {
- cellularNetwork.handover(parts[0], parts[1], parts[2]);
- } catch (CellFullException e) {
- System.out.println(e.getMessage());
- }
- }
- System.out.println("===== Find numbers =====");
- n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String number = scanner.nextLine();
- cellularNetwork.findNumber(number);
- }
- System.out.println("===== Load =====");
- cellularNetwork.load();
- scanner.close();
- }
- }
- class CellFullException extends Exception{
- public CellFullException(String cellId) {
- super(String.format("CellFullException: {%s}",cellId));
- }
- }
- class Cell {
- String cellId;
- int capacity;
- List<String> numbers;
- public Cell(String cellId, int capacity) {
- this.cellId = cellId;
- this.capacity = capacity;
- this.numbers = new ArrayList<String>();
- }
- public String getCellId() {
- return cellId;
- }
- public void removeNumber(String number) {
- this.numbers.remove(number);
- }
- public void addNumber(String number) throws CellFullException {
- if(numbers.size()==capacity)
- throw new CellFullException(cellId);
- numbers.add(number);
- }
- public double getPercentOfFilledCapacity(){
- return numbers.size() * 100.0 / capacity;
- }
- public String getNumberOfStars(){
- String stars="";
- for(int i=0;i<(int)getPercentOfFilledCapacity()/10;i++){
- stars+="*";
- }
- return stars;
- }
- @Override
- public String toString() {
- return String.format("ID: %s\n" + "%s %.2f%%", cellId,getNumberOfStars(), getPercentOfFilledCapacity());
- }
- }
- class CellularNetwork {
- String name;
- Map<String,Cell> cells;
- Map<String,List<String>> cellsPerNumber;
- public CellularNetwork(String name, String[] cellIds, int[] capacities) {
- this.name = name;
- this.cells= new HashMap<>();
- for(int i=0;i<capacities.length;i++){
- cells.putIfAbsent(cellIds[i],new Cell(cellIds[i],capacities[i]));
- }
- this.cellsPerNumber= new HashMap<>();
- }
- public void makeCall(String cellId, String number) throws CellFullException {
- cells.get(cellId).addNumber(number);
- cellsPerNumber.putIfAbsent(number, new ArrayList<>());
- cellsPerNumber.get(number).add(cellId);
- }
- public void handover(String number, String fromCellId, String toCellId) throws CellFullException {
- cells.get(fromCellId).removeNumber(number);
- cells.get(toCellId).addNumber(number);
- if(cellsPerNumber.containsKey(number))
- cellsPerNumber.get(number).add(toCellId);
- }
- public void findNumber(String number) {
- if(!cellsPerNumber.containsKey(number)){
- System.out.println(String.format("Number '%s' not found",number));
- return ;
- }
- System.out.print(String.format("Number '%s' : ",number));
- System.out.println(String.join(" --> ",
- cellsPerNumber.get(number).stream()
- .map(i->i.toString())
- .toList()
- ));
- }
- public void load() {
- cells.values().stream().sorted(Comparator.comparing(Cell::getCellId)).forEach(i-> System.out.println(i));
- }
- }
- //**
- Telefonica
- cell-0:41 cell-1:39 cell-2:93 cell-3:91 cell-4:62 cell-5:26 cell-6:15 cell-7:49 cell-8:99 cell-9:30 cell-10:60 cell-11:57 cell-12:96
- 330
- cell-9 077155515
- cell-6 070744086
- cell-1 070609890
- cell-11 076057080
- cell-12 077497349
- cell-0 071320920
- cell-5 075227471
- cell-9 078393089
- cell-6 077967341
- cell-0 077937849
- cell-5 071420410
- cell-10 071105206
- cell-6 075405765
- cell-1 070432921
- cell-2 075361572
- cell-5 075957288
- cell-4 071350470
- cell-10 070101894
- cell-6 076756301
- cell-11 070166441
- cell-0 076784732
- cell-4 078252256
- cell-12 078050893
- cell-2 075265495
- cell-6 077599636
- cell-2 077088456
- cell-11 075132077
- cell-9 071787188
- cell-6 078349455
- cell-9 076308844
- cell-4 075788786
- cell-6 077511714
- cell-1 071206729
- cell-1 077225406
- cell-0 078995064
- cell-8 076518717
- cell-11 075475784
- cell-11 078112403
- cell-7 076885126
- cell-8 077835995
- cell-3 077863009
- cell-12 078440649
- cell-5 070311600
- cell-11 071602445
- cell-0 075897919
- cell-11 078779310
- cell-2 070085002
- cell-0 077841966
- cell-3 076981213
- cell-0 078222571
- cell-8 070892766
- cell-5 070631652
- cell-1 076234814
- cell-1 075789230
- cell-7 070608330
- cell-6 075358940
- cell-2 077834717
- cell-1 071058947
- cell-9 075892863
- cell-9 077459874
- cell-4 077823816
- cell-12 071882640
- cell-9 071587652
- cell-6 070123300
- cell-12 070405057
- cell-11 076707709
- cell-11 075703343
- cell-11 071901151
- cell-1 071715302
- cell-3 070669243
- cell-3 071804647
- cell-12 071003966
- cell-2 076700085
- cell-1 070058434
- cell-0 076125303
- cell-0 078306973
- cell-10 070582459
- cell-6 077659477
- cell-8 071089086
- cell-9 077194356
- cell-10 075301384
- cell-0 071135213
- cell-11 077139026
- cell-3 077077456
- cell-6 075948033
- cell-7 076155912
- cell-1 075432243
- cell-4 077667155
- cell-10 076977582
- cell-6 070807120
- cell-7 071251065
- cell-12 077565705
- cell-3 075229803
- cell-11 070197268
- cell-7 071491512
- cell-5 075054032
- cell-3 078270554
- cell-11 078318865
- cell-9 077744153
- cell-4 070747703
- cell-3 075694804
- cell-9 075628531
- cell-7 076070930
- cell-0 071289586
- cell-12 077617091
- cell-9 070808663
- cell-5 076551671
- cell-5 070849521
- cell-3 071745033
- cell-9 076628770
- cell-7 070449804
- cell-4 070134300
- cell-4 078403144
- cell-1 075398340
- cell-11 077389994
- cell-0 077173029
- cell-1 071103496
- cell-4 077757368
- cell-2 071803357
- cell-12 071690066
- cell-8 076020624
- cell-12 071074644
- cell-9 078888494
- cell-0 078160840
- cell-4 071815847
- cell-4 078744181
- cell-6 075792835
- cell-9 070759142
- cell-3 071569811
- cell-0 070405154
- cell-5 075407763
- cell-3 078180219
- cell-8 077785601
- cell-11 075266823
- cell-12 076110915
- cell-7 078482517
- cell-2 077486472
- cell-5 078910455
- cell-12 078534640
- cell-12 078017267
- cell-8 076885275
- cell-3 075486180
- cell-7 076296393
- cell-1 070686864
- cell-3 078698077
- cell-3 077837389
- cell-11 078473523
- cell-8 078139851
- cell-4 070947963
- cell-10 070195799
- cell-0 071253651
- cell-7 077094909
- cell-2 071724602
- cell-4 070007528
- cell-3 070828915
- cell-6 077263014
- cell-10 077276449
- cell-11 076403671
- cell-8 071005558
- cell-6 076781768
- cell-3 078248736
- cell-3 078881943
- cell-9 078992082
- cell-8 077146081
- cell-7 070796697
- cell-4 071922530
- cell-7 071639310
- cell-3 076415059
- cell-10 076532259
- cell-7 070316676
- cell-8 070458009
- cell-9 070004311
- cell-10 071483014
- cell-0 075752481
- cell-2 071388271
- cell-1 076241700
- cell-8 077305991
- cell-5 070575116
- cell-1 071913324
- cell-4 077114475
- cell-5 077643997
- cell-2 076206471
- cell-8 075981654
- cell-8 078597302
- cell-1 076848901
- cell-2 075992107
- cell-1 070150773
- cell-4 076748611
- cell-0 070818687
- cell-8 078289636
- cell-4 070350947
- cell-5 075343488
- cell-7 078027919
- cell-6 070040376
- cell-3 071976213
- cell-11 070849566
- cell-9 075392512
- cell-6 071281219
- cell-8 075716273
- cell-5 070323353
- cell-8 071024444
- cell-7 077755786
- cell-11 075710684
- cell-3 075163085
- cell-7 077428338
- cell-3 070424763
- cell-5 070694886
- cell-3 077193061
- cell-0 077500132
- cell-9 075202837
- cell-12 071284142
- cell-10 076234076
- cell-6 075873978
- cell-3 070146385
- cell-4 070036091
- cell-0 078283135
- cell-8 070885727
- cell-0 070898394
- cell-8 077448814
- cell-0 070374409
- cell-12 070152703
- cell-6 070168340
- cell-3 071362213
- cell-10 071105419
- cell-6 077362619
- cell-1 077878760
- cell-5 071114646
- cell-10 075561325
- cell-12 075467046
- cell-8 078615866
- cell-12 076047326
- cell-5 078153795
- cell-0 070183074
- cell-5 078733420
- cell-5 077597983
- cell-9 075071132
- cell-0 077584310
- cell-6 071640772
- cell-11 075889025
- cell-1 078575495
- cell-4 075990971
- cell-3 070837311
- cell-5 075690895
- cell-11 070385578
- cell-10 070144571
- cell-12 071518753
- cell-3 078440202
- cell-0 070019101
- cell-7 077365910
- cell-6 078894198
- cell-11 077594916
- cell-1 078275090
- cell-5 076254701
- cell-6 077954062
- cell-4 075027042
- cell-12 075645658
- cell-4 071775154
- cell-9 071164722
- cell-1 077500385
- cell-10 078243710
- cell-11 076639513
- cell-9 070141797
- cell-7 075685644
- cell-9 071388976
- cell-11 070458071
- cell-11 070969271
- cell-9 076055723
- cell-10 071863138
- cell-1 076860630
- cell-2 071422609
- cell-12 076237828
- cell-2 070345532
- cell-5 075637310
- cell-10 076210785
- cell-2 076734410
- cell-0 078452918
- cell-10 077120357
- cell-12 071464421
- cell-11 078391251
- cell-7 070565965
- cell-5 070328685
- cell-11 078422606
- cell-0 071078986
- cell-6 076658075
- cell-10 070653035
- cell-4 077117554
- cell-11 078138676
- cell-9 071308316
- cell-11 071354288
- cell-3 078422797
- cell-1 076416262
- cell-8 078996760
- cell-10 076215870
- cell-4 078286907
- cell-4 075409879
- cell-10 070978111
- cell-5 075919824
- cell-11 071098132
- cell-12 071586869
- cell-10 070313696
- cell-0 075679501
- cell-3 071185119
- cell-4 075656867
- cell-3 077772570
- cell-7 077695010
- cell-8 070797365
- cell-4 070983023
- cell-5 076358621
- cell-1 076899300
- cell-12 076523362
- cell-8 070829120
- cell-12 076284725
- cell-3 076063172
- cell-5 070717538
- cell-3 071190981
- cell-5 070693868
- cell-12 071630052
- cell-3 076718376
- cell-7 077387602
- cell-7 071162466
- cell-4 070409325
- cell-6 078103726
- cell-12 071333194
- cell-1 078612077
- cell-6 077873663
- cell-2 078575011
- cell-1 076689806
- cell-4 077093233
- cell-0 071078155
- cell-2 070136103
- 62
- 071640772 cell-6 cell-7
- 075685644 cell-7 cell-8
- 076899300 cell-1 cell-2
- 071639310 cell-7 cell-8
- 077193061 cell-3 cell-4
- 071320920 cell-0 cell-1
- 076707709 cell-11 cell-12
- 070007528 cell-4 cell-5
- 076981213 cell-3 cell-4
- 075358940 cell-6 cell-7
- 078888494 cell-9 cell-10
- 075628531 cell-9 cell-10
- 076308844 cell-9 cell-10
- 077954062 cell-6 cell-7
- 075628531 cell-10 cell-11
- 075637310 cell-5 cell-6
- 070818687 cell-0 cell-1
- 078575495 cell-1 cell-2
- 071354288 cell-11 cell-12
- 075990971 cell-4 cell-5
- 071190981 cell-3 cell-4
- 070004311 cell-9 cell-10
- 070316676 cell-7 cell-8
- 078779310 cell-11 cell-12
- 076784732 cell-0 cell-1
- 075266823 cell-11 cell-12
- 070797365 cell-8 cell-9
- 070759142 cell-9 cell-10
- 070058434 cell-1 cell-2
- 070892766 cell-8 cell-9
- 077146081 cell-8 cell-9
- 070796697 cell-7 cell-8
- 077659477 cell-6 cell-7
- 077835995 cell-8 cell-9
- 076210785 cell-10 cell-11
- 078615866 cell-8 cell-9
- 075266823 cell-12 cell-0
- 070759142 cell-10 cell-11
- 070892766 cell-9 cell-10
- 071098132 cell-11 cell-12
- 070686864 cell-1 cell-2
- 070146385 cell-3 cell-4
- 078243710 cell-10 cell-11
- 077093233 cell-4 cell-5
- 078222571 cell-0 cell-1
- 071388271 cell-2 cell-3
- 076718376 cell-3 cell-4
- 075229803 cell-3 cell-4
- 070582459 cell-10 cell-11
- 075054032 cell-5 cell-6
- 076628770 cell-9 cell-10
- 075710684 cell-11 cell-12
- 076047326 cell-12 cell-0
- 070166441 cell-11 cell-12
- 078286907 cell-4 cell-5
- 078160840 cell-0 cell-1
- 070717538 cell-5 cell-6
- 071602445 cell-11 cell-12
- 070019101 cell-0 cell-1
- 077834717 cell-2 cell-3
- 075637310 cell-6 cell-7
- 077155515 cell-9 cell-10
- 18
- 071690066
- 077263014
- 070978111
- 070759142
- 070686864
- 078306973
- 076237828
- 076781768
- 078248736
- 076977582
- 076403671
- 075637310
- 078612077
- 076063172
- 071005558
- 075992107
- 070101894
- 075710684
- **//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement