Advertisement
VirKato

AoC 2022 day 8

Dec 8th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.31 KB | Source Code | 0 0
  1. package org.advent.of.code.y2022;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Line8 {
  8.  
  9.     public static void main(String[] args) {
  10.         String test = """
  11.                30373
  12.                25512
  13.                65332
  14.                33549
  15.                35390
  16.                """;
  17. //        data = test;
  18.  
  19.         solution();
  20.     }
  21.  
  22.  
  23.     private static void solution() {
  24.         int width = 0;
  25.  
  26.         Scanner in = new Scanner(data);
  27.         List<int[]> table = new ArrayList<>();
  28.         while (in.hasNextLine()) {
  29.             String line = in.nextLine();
  30.             char[] chars = line.toCharArray();
  31.             width = chars.length;
  32.             int[] nums = new int[width];
  33.             int index = 0;
  34.             for (char c : chars) {
  35.                 nums[index++] = Integer.parseInt(Character.toString(c));
  36.             }
  37.             table.add(nums);
  38.         }
  39.  
  40.  
  41.         int count = 0;
  42.         int bestScore = 0;
  43.  
  44.         for (int x = 0; x < width; x++) {
  45.             for (int y = 0; y < table.size(); y++) {
  46.                 if (x == 0 || y == 0 || x == width - 1 || y == table.size() - 1) {
  47.                     count++;
  48.                 } else {
  49.                     int xr = x, xl = x, yd = y, yu = y;
  50.                     boolean visXr = true;
  51.                     boolean visXl = true;
  52.                     boolean visYd = true;
  53.                     boolean visYu = true;
  54.                     boolean stopXr = false;
  55.                     boolean stopXl = false;
  56.                     boolean stopYd = false;
  57.                     boolean stopYu = false;
  58.                     int cXr = 0;
  59.                     int cXl = 0;
  60.                     int cYd = 0;
  61.                     int cYu = 0;
  62.  
  63.                     while (!(stopXr && stopXl && stopYd && stopYu)) {
  64.                         if (xr >= width - 1) stopXr = true;
  65.                         if (xl < 1) stopXl = true;
  66.                         if (yd >= table.size() - 1) stopYd = true;
  67.                         if (yu < 1) stopYu = true;
  68.  
  69.                         if (!stopXr) {
  70.                             if (visXr) cXr++;
  71.                             xr++;
  72.                         }
  73.                         if (!stopXl) {
  74.                             if (visXl) cXl++;
  75.                             xl--;
  76.                         }
  77.                         if (!stopYd) {
  78.                             if (visYd) cYd++;
  79.                             yd++;
  80.                         }
  81.                         if (!stopYu) {
  82.                             if (visYu) cYu++;
  83.                             yu--;
  84.                         }
  85.  
  86.                         if (table.get(y)[x] <= table.get(yd)[x]) visYd = false;
  87.                         if (table.get(y)[x] <= table.get(yu)[x]) visYu = false;
  88.                         if (table.get(y)[x] <= table.get(y)[xr]) visXr = false;
  89.                         if (table.get(y)[x] <= table.get(y)[xl]) visXl = false;
  90.                     }
  91.  
  92.                     if (visXr || visXl || visYd || visYu) count++;
  93.  
  94.                     int score = cXr * cXl * cYd * cYu;
  95.                     bestScore = Math.max(score, bestScore);
  96.                 }
  97.             }
  98.         }
  99.  
  100.  
  101.         System.out.println("First: " + count);
  102.         System.out.println("Second: " + bestScore);
  103.  
  104.     }
  105.  
  106.     private static String data = """
  107.            301201331333030024002151134115545441525510053410321024643223304253251423014345311431413433341211111
  108.            210313031411030140245452501003514242514125366210551353634546632015232323540022212401412031113310333
  109.            201223331222432221305443412445304401201165033624245311620346242360420521343514132520204104244200322
  110.            210003034413133345421153101053132524226234460546142402452466345321440520524311212410303224012242000
  111.            002232200024321241105051144044530503201446133504652500410463241436361361002024122340512441243222203
  112.            102023303032432341105353554225663326563653250523350302104003635340436625240235531151145343032212033
  113.            011432423042054254520503506222252050256315142254016650105015643542660231044302233035322122100032212
  114.            034421243321311550201305666136554451350322354144633556333022003664006612152610511132354312443134400
  115.            302214114340201552153246320504242003513464536535521715125521314654606653312124014223422135000032024
  116.            022203301311105514431426025115566414166144753435463146324664616502342356626446124204455213422314241
  117.            133202031100242023236362026113243573426315617627527543647213541517440126016042624554125530550304004
  118.            412414223443004100621020255403316757326244275247134627215636656313472021156626214003511121524211201
  119.            100020113553530111635403433310123371416566464531664371236763762654672716453255155611041431321301000
  120.            040442015403322353635250250156757514552341246114755653427321572644373333764015356451022003122252214
  121.            131335400000330152046666616363523347474566725475326875713565751313316433363251655012415235210153134
  122.            212205354412516232346215667365363413636143322285687428338237666271455444277511521334662020135104441
  123.            400134535100103465544240377332663352346366464426356585587388572154627365567576126334445412551245044
  124.            313125111102350042411627156361342341273445272266422466584446423554731131743433165124231111435223052
  125.            414211350002631116406722423146474385423846752263457355267246283862347153516722261451645234034250125
  126.            142350115212334322146515331713325337468443588225285768685574226644533724475335124115505531225141432
  127.            221420101060222256363211452511352288853828648746647256668823328327287747564123275261621500245133411
  128.            453113024555202631573132336658242736347325874252333535256677587837657678625214457745232452150220123
  129.            555034246226545644223655121622358344733858586769673974337826765554468355524344661464661052120504131
  130.            101440210460332606657775163743688753344448398636494466664973768385765272752651367426304666101123130
  131.            532232304344031236635141354227547676887398783998657559945898897482268637355733664642700640525354230
  132.            254425530336536647234251648333783825963356648666958849583777336977537428866623134526176025236412301
  133.            052420045556125743544677352453853565968669799673389736356646485675748435258353372737571166544644253
  134.            144212521432144457427473545763356688633446594997933764479494846866798222377556235341461400160564305
  135.            414546463341545336547365484645276357553584336774496855639397587938638557263543347554553131065014354
  136.            213110564326373147676363536832594994448537796457645847754798668996863976763583886251625241215423451
  137.            402441605154657151126587562648764444548399868584444984666447398399475652866688642677426256050111044
  138.            030505222044142675156675655568358889949479978645448697784665774359755689243645735162215642560304321
  139.            002364341155613641588426677659986477446964688986679878477959859546863555528383865312255533510006644
  140.            315603002442216142126278638843367593346876468657757444768587498693659647788784456647117426311553113
  141.            534026231247517653336254847934574378679647999777747989757856888763437583487626885465121722135401110
  142.            152663134317453637627862365337774534996567697946779555455989744649937645994653657567156245462533302
  143.            415120400426463528567443378965654344948544677795568678979865564545598768643876476547177613642314014
  144.            212134106151643463482274255887744459499685657799867587677998674896776889393348337484463153571460162
  145.            123265543627354585248835586978887467585449979975988897979955857994455783796697728544256417663200436
  146.            253156122444754335226888665898754494695989955588988885866899669459797535776733282376417141555433640
  147.            105125666421254638626347936375599547788669998799766755958796875664688687698846364536875113743306636
  148.            543154323554211424234263689757375946445859555669888666959896575566688549986398632678217421361116016
  149.            534446627674264324833537896437597864966965789556679958788689859997595593943745863828822163463613245
  150.            456422412772232364425647578537895646785958867897868976696886585987468565967685553265682333333642602
  151.            155305566576114247342276876599654498977865685987867897688785559595497686574858683675725765634063131
  152.            350133405277354723553294549397478955987876557987698878889985757654986568886863447837277147741536410
  153.            013062547146413742675348465764876557565879576696666889866579778749764578444847765768252647361443506
  154.            312465621677151875655585995745694878655956799697796697686885558978975449756474748887586542411256255
  155.            423204362353178745852254637935464664778869968879677697777878575777556878634856584473864213312622015
  156.            534661567344614365768387877864878845767598869677978779767787858784584759476648665773358427645311651
  157.            430011475352152575755679953749774454789788589769698678798856669687965675695986644657485514217351666
  158.            542102111432167877663467859536777594767895986867898886766676976869846495867599543374583332731266033
  159.            625000474732136887874855736749589476996659798967789898789776587976889996948384763734447643736220025
  160.            315666062115612845335864885945654758789765698688877769667668867576798855583978955268844425122514014
  161.            152600463716132562642488647595685976468985656996977998778955585795596495844759888838286175352560414
  162.            006166514645376457676454676698858546456775687876679968965697579748769964686887987774361312361623112
  163.            630201542341341448626638885984646765658869666987886976795798598788495887683399383777375346636211550
  164.            036646423755734847267336476354564466955596575589776859595968578788654499546534758266652277357366530
  165.            453243636357156364866489789657955759776966959655868798956766987954989947943863354645777345476202531
  166.            530105612477445332537878834449565659485976667656597697775688857778754844458355277846316112626121162
  167.            055455363734131537735589765579697845958456556556686566965687745949576967556763233558753142411453220
  168.            106052304147675524263254984449336844955757978879668668576768676787755787574533445667862426312335351
  169.            133545504422337378456858864594449878578885567885775668878958786485657683335482267532317234560541422
  170.            200255566345515714753376495455776859698895897588685996587586745857468367437854554745116413532363200
  171.            350510465672221255867873257334876685756745545667796896497688464848948768947422782828661471761064122
  172.            525006435255273146277854349566333878494888868995668459955986944485699959837552728523754132365504644
  173.            455465632474322677526353348496893746878784677485547847847759558643378358396575444475242721722332551
  174.            341264465466426473255828826967974346556896576764466849697759555548879644597626468762475471232114332
  175.            143015600661254143155723453874645479598897489897887869959899767545558694586565824554175726556330040
  176.            133114601616545361237744836629659733553959758846995589848895753645437876746537567247223752042644353
  177.            434245136445331567724385257642784846854365449776854954774496353883589792252488545733673623365531411
  178.            242355400240614257475587442835547333788435758749676988545464355973893552465258322436517445530103424
  179.            150522445522362651727687768785836854563533374767758439957544655684349342343244731321332645111541021
  180.            552400436500115714126552863645543555378537389683366874478896898884353658447572421457611323114255212
  181.            304202302265264467247666485428778363698453577569497785947535758344375242826567265416457044433163541
  182.            233353455013125235351275368356247267348486886475363374634485474876565735683724532261524142510630102
  183.            440535161152151631635373575637235623878689436669334497565478939626735535482665761716504623034023120
  184.            530315220006363246522576534738725655632837547375957696853688457354338383534653311344113001640515414
  185.            204313020634510554536253474458753547425233336944493739858844757374686252224513224773652513353305442
  186.            303341052510105201513731753724282223364752725537642288866686622588863837445543126664241453441235310
  187.            020424153032624323671363111463422353477742627478683844868633432636588536274451572264610340614104555
  188.            234413051424523314161146731763467724436346548732688655832363463687352214147256367321403115315443500
  189.            012242303516512442500774532142266284382765822428622346384737664242643615627661273552155526054233342
  190.            414111403514124460666037555342627216676675426728525487642273658483463277637667635142640425425132342
  191.            030230422403061322643446763675767367778368767234652457668385774272636771211474352411225452213412310
  192.            032113014010425161346051473351752565267374786865733735338367253243133431751566100563025504520403342
  193.            412304104125143243301236345151712624614211575411475821376435722564217622616521260005512052222004343
  194.            220120030303233310444012046257231472762352112415164732622531735165124331120326541545011444410002400
  195.            014433054545451316643032015010316146277161216552272145522146562717437536450604320052302441034404241
  196.            343434423453055414653253445655564632771244123251647272745743671776331230215004615545255055050133340
  197.            201000210435340452132116022411356245227731333555165752717373435156310453501204235431002250524221113
  198.            132100434242305201202434465065260455543457427521464157317741755225245256645305422324552252031144120
  199.            202441110432420500412256514006155406431013256445476676323751251451145064255351541053031130431201234
  200.            020212022220112430245130010061216066220264013366625475240550163452521001005224125240541123033121143
  201.            132244304123021525001211154544140012602120125510011436101445405650606625263223310430540142302200433
  202.            103142142201203130045124150512613540504163320002045004044613203062515015344450101154534004044231012
  203.            221234114210331020511522535110011636160005413424224560604634052506210501451443521502544433404042033
  204.            001233231400214434040102054514026055053336624361305231213331535242260044242320014551114014133012203
  205.            013221201313013142535111451534205341402225520043535003305340330410530234344250234251433044340323003
  206.            """;
  207. }
  208.  
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement