Je fatigue , il est temps que la saison se termine. J'ai encore perdu du temps sur des tests bête aux borne
Environ ~3h00 au total.
Encore obligé d'en arriver à écrire T.U. pour débuguer. Demain, je TDD, je vais probablement gagner du temps.
Pour la partie 1, j'ai utilisé un evaluator JEXL pour les expressions.
Pour la partie 2, je n'avais pas le choix. J'ai écris le "parseur" (En fait, c'est plutôt un split de l'expression) car il faut plus travailler sur des lots de pieces.
Mon objet Piece a maintenant une propriété Range que je vais split à chaque expression en 0, 1 ou 2.
Temps d'éxecution:123ms
packageaoc2023;importjava.math.BigInteger;importjava.util.ArrayList;importjava.util.Collection;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.Random;importjava.util.Scanner;importjava.util.Stack;importorg.apache.commons.jexl3.JexlBuilder;importorg.apache.commons.jexl3.JexlContext;importorg.apache.commons.jexl3.JexlEngine;importorg.apache.commons.jexl3.JexlExpression;importorg.apache.commons.jexl3.MapContext;publicclassAoc2023s19v2{publicstaticRandomRANDOM=newRandom(0L);publicstaticJexlEnginejexl=newJexlBuilder().create();publicstaticrecordRange(intstart,intend){publiclongcount(){returnend-start;}publicRange[]split(intto){if(start>=to||to>=end){returnnull;}returnnewRange[]{newRange(start,to),newRange(to,end)};}publicRangerandom(){inti=start+RANDOM.nextInt((int)count());returnnewRange(i,i+1);}}publicstaticclassPiece{publicRangex;publicRangem;publicRangea;publicRanges;Piece(Rangex,Rangem,Rangea,Ranges){this.x=x;this.m=m;this.a=a;this.s=s;}publiclongprod(){returnx.count()*m.count()*a.count()*s.count();}@OverridepublicStringtoString(){return"Piece [x="+x+", m="+m+", a="+a+", s="+s+"]";}publicRangeget(Stringkey){returnswitch(key){case"x"->x;case"m"->m;case"a"->a;case"s"->s;default->thrownewRuntimeException();};}publicPiecesubstitute(Stringkey,Ranger){returnswitch(key){case"x"->newPiece(r,m,a,s);case"m"->newPiece(x,r,a,s);case"a"->newPiece(x,m,r,s);case"s"->newPiece(x,m,a,r);default->thrownewRuntimeException();};}}staticclassRule{Stringcondition;Stringdestination;StringattributTest;booleanattributeGreaterThan;intvalueTest;Rule(Stringcondition,Stringdestination){this.condition=condition;this.destination=destination;if(!this.condition.equals("true")){attributTest=""+this.condition.charAt(0);attributeGreaterThan=this.condition.charAt(1)=='>';valueTest=Integer.parseInt(this.condition.substring(2));}}publicbooleanmatch(Piecepiece){// Create a JEXL expression objectJexlExpressionjexlExpression=jexl.createExpression(condition);// Create a JexlContext and add the 'part' variable to itJexlContextcontext=newMapContext();context.set("x",piece.x.start);context.set("m",piece.m.start);context.set("s",piece.s.start);context.set("a",piece.a.start);// Evaluate the expressionbooleanresult=(boolean)jexlExpression.evaluate(context);//System.out.println("Evaluation result: " + result);returnresult;}publicStringtoString(){returncondition+","+destination;}publicbooleanprocess(Piecepiece,Workflowworkflow,Map<String,Workflow>workflows,List<Piece>accepts){if(condition.equals("true")){System.out.println("Default move to "+destination);getDestination(workflows,accepts).add(piece);returntrue;}Rangerange=piece.get(attributTest);System.out.println(this.condition);System.out.println(range);Rangesplit[]=range.split(attributeGreaterThan?valueTest+1:valueTest);if(split!=null){System.out.println("Splited to "+workflow.name+" & "+destination);System.out.println(split[0]+" <-> "+split[1]);if(attributeGreaterThan){getDestination(workflows,accepts).add(piece.substitute(attributTest,split[1]));workflow.pieces.add(piece.substitute(attributTest,split[0]));}else{getDestination(workflows,accepts).add(piece.substitute(attributTest,split[0]));workflow.pieces.add(piece.substitute(attributTest,split[1]));}returntrue;}else{if(attributeGreaterThan){if(range.start>valueTest){System.out.println("Greater move to ");getDestination(workflows,accepts).add(piece.substitute(attributTest,range));returntrue;}}else{if((range.end-1)<valueTest){System.out.println("Lower move to ");getDestination(workflows,accepts).add(piece.substitute(attributTest,range));returntrue;}}}returnfalse;}privateCollection<Piece>getDestination(Map<String,Workflow>workflows,List<Piece>accepts){if("A".equals(destination)){returnaccepts;}if("R".equals(destination)){returnnewArrayList();}returnworkflows.get(destination).pieces;}}staticclassWorkflow{Stringname;List<Rule>rules;Stack<Piece>pieces=newStack();Workflow(Stringname){this.name=name;this.rules=newArrayList<>();}voidaddRule(Rulerule){this.rules.add(rule);}publicStringtoString(){StringBuildersb=newStringBuilder();for(Rulerule:this.rules){sb.append(rule.toString());}returnsb.toString();}}staticMap<String,Workflow>parseInput(Scannerin){Map<String,Workflow>workflows=newHashMap<>();while(in.hasNext()){Stringline=in.nextLine();line=line.trim();if(line.isEmpty()){break;}Stringname=line.substring(0,line.indexOf('{')).trim();Workflowcurrent=newWorkflow(name);workflows.put(name,current);String[]parts=line.substring(line.indexOf('{')+1,line.length()-1).split(",");for(Stringpart:parts){String[]s=part.split(":");if(s.length==1){current.addRule(newRule("true",s[0]));}else{current.addRule(newRule(s[0],s[1]));}}}Rangerange=newRange(1,4001);Piecebase=newPiece(range,range,range,range);workflows.get("in").pieces.add(base);List<Piece>accepts=newArrayList<>();computeAccepts(workflows,accepts);BigIntegersum=BigInteger.ZERO;for(Piecepiece:accepts){// uncomment to checkIndividual(piece, workflows);sum=sum.add(BigInteger.valueOf(piece.prod()));}System.out.println(sum);returnworkflows;}privatestaticvoidcheckIndividual(PiecerangePiece,Map<String,Workflow>workflows){for(inti=0;i<1000;i++){StringcurrentName="in";Piecepiece=newPiece(rangePiece.x.random(),rangePiece.m.random(),rangePiece.a.random(),rangePiece.s.random());//System.out.println("Piece:" + piece); while(!currentName.equals("R")&&!currentName.equals("A")){//System.out.println("Current workflow:" + currentName);Workflowworkflow=workflows.get(currentName);for(Rulerule:workflow.rules){//System.out.println("Match:" + rule.condition);if(rule.match(piece)){currentName=rule.destination;//System.out.println("=>:" + rule.destination);break;}}}if(!currentName.equals("A")){System.err.println(rangePiece);thrownewRuntimeException(piece.toString());}}}privatestaticvoidcomputeAccepts(Map<String,Workflow>workflows,List<Piece>accepts){while(workflows.values().stream().filter(w->!w.pieces.isEmpty()).count()>0){Workflowworkflow=workflows.values().stream().filter(w->!w.pieces.isEmpty()).findFirst().orElse(null);System.out.println("Workflow:"+workflow.name);Piecepiece=workflow.pieces.pop();for(Rulerule:workflow.rules){if(rule.process(piece,workflow,workflows,accepts)){break;}}}}publicstaticvoidmain(String[]args){longs=System.currentTimeMillis();try(Scannerin=newScanner(Aoc2023s18v2.class.getResourceAsStream("res/t19.txt"))){parseInput(in);}System.out.println("Durée:"+(System.currentTimeMillis()-s)+"ms");}}
# Encore des problèmes de tests au borne
Posté par syj . En réponse au message Advent of Code, jour 19. Évalué à 1.
Je fatigue , il est temps que la saison se termine. J'ai encore perdu du temps sur des tests bête aux borne
Environ ~3h00 au total.
Encore obligé d'en arriver à écrire T.U. pour débuguer. Demain, je TDD, je vais probablement gagner du temps.
Pour la partie 1, j'ai utilisé un evaluator JEXL pour les expressions.
Pour la partie 2, je n'avais pas le choix. J'ai écris le "parseur" (En fait, c'est plutôt un split de l'expression) car il faut plus travailler sur des lots de pieces.
Mon objet Piece a maintenant une propriété Range que je vais split à chaque expression en 0, 1 ou 2.
Temps d'éxecution:123ms