I built my original solution to this on top of my Day 2 solution, but was really unhappy about how that solution had turned out. So, I spent time over the weekend and into this morning building out a more clean solution around the Intcode Computer (knowing that it's going to likely be used more later).
The implementation is below (and also accessible here), along with the new execution calls for Day 2:
;; incode_computer.clj(nsaoc2019.intcode-computer)(defoperations{1{:params3,:action(fn[p1p2_]{:output(+p1p2)})}2{:params3,:action(fn[p1p2_]{:output(*p1p2)})}3{:params1,:action(fn[_]{:output(Integer/parseInt(read-line))})}4{:params1,:action(fn[p1](printlnp1){})}5{:params2,:action(fn[p1p2](if(not=0p1){:jumpp2}{}))}6{:params2,:action(fn[p1p2](if(=0p1){:jumpp2}{}))}7{:params3,:action(fn[p1p2_](if(<p1p2){:output1}{:output0}))}8{:params3,:action(fn[p1p2_](if(=p1p2){:output1}{:output0}))}99{:params0}})(defnfetch"Retrieves the opcode, and the number of arguments for that opcode"[machine](let[opcode(get(:instructionsmachine)(:program-countermachine))action(modopcode100)]{:opcodeopcode:actionaction:params(:params(getoperationsaction))}))(defnopcode-to-parameters[opcode](let[modes(->>opcode(format"%05d")(into[])(take3)(reverse))](map#(condp=%\0{:mode:direct}\1{:mode:immediate})modes)))(defnretrieve-args"Retrieves the parameters of the instruction from the code list, and
returns a map of their positions, values, and mode"[machinefetched-instruction](let[in-params(take(:paramsfetched-instruction)(drop(inc(:program-countermachine))(:instructionsmachine)))op-params(opcode-to-parameters(:opcodefetched-instruction))joiner(fn[in-paramop-param]{:mode(:modeop-param):param-posin-param:param-val(get(:instructionsmachine)in-param)})](assocfetched-instruction:params(mapjoinerin-paramsop-params))))(defnexecute"Executes the instruction provided, and returns any results of that
execution."[instruction](let[params(map#(condp=(:mode%):direct(:param-val%):immediate(:param-pos%))(:paramsinstruction))action(:action(getoperations(:actioninstruction)))](ifaction(associnstruction:result(applyactionparams))(associnstruction:result:stop))))(defnapply-changes"Applies any changes based on the result of the instruction. Returns a
potentially modified instruction list."[machineinstruction-with-results](let[result(:resultinstruction-with-results)inc-program-counter(fn[](+1(:program-countermachine)(count(:paramsinstruction-with-results))))](if(:jumpresult)(assocmachine:program-counter(:jumpresult))(if(=result:stop)(assocmachine:stoptrue)(if(:outputresult)(let[adjusted-instructions(assoc(:instructionsmachine)(:param-pos(last(:paramsinstruction-with-results)))(:outputresult))](assocmachine:instructionsadjusted-instructions:program-counter(inc-program-counter)))(assocmachine:program-counter(inc-program-counter)))))));; day2.clj(nsaoc2019.day2(:require[clojure.string:asst][aoc2019.intcode-computer:asic]))(defnparse-input"Converts input into atom holding instruction integers."[input](map#(Integer/parseInt%)(st/split(st/trim-newlineinput)#",")))(defnnew-calculate[input](let[instructions(vec(parse-inputinput))](loop[machine{:instructionsinstructions:program-counter0:stopfalse}](if(:stopmachine)(first(:instructionsmachine))(recur(->>(ic/fetchmachine)(ic/retrieve-argsmachine)(ic/execute)(ic/apply-changesmachine)))))))(defnpreprocess-input([input](preprocess-inputinput122))([inputnounverb](let[parsed-input(vec(parse-inputinput))](st/join","(->parsed-input(assoc1noun)(assoc2verb))))))(defnp2019-02-part1[input](new-calculate(preprocess-inputinput)))(defnp2019-02-part2"Using the calculator from part 1, determine the proper inputs for our expected value."[input](let[expected-result19690720noun(atom0)verb(atom0)](while(not=expected-result(new-calculate(preprocess-inputinput@noun@verb)))(swap!verbinc)(if(=@verb100)(do(swap!nouninc)(reset!verb0))))(+@verb(*100@noun))))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I built my original solution to this on top of my Day 2 solution, but was really unhappy about how that solution had turned out. So, I spent time over the weekend and into this morning building out a more clean solution around the Intcode Computer (knowing that it's going to likely be used more later).
The implementation is below (and also accessible here), along with the new execution calls for Day 2: