My favorite part of this is how the actions get selected. the get-action function actually returns the function used for handling the inputs (where 99 returns a function that, regardless of input, returns "stop"). Then, that function is used with the apply function inside calculate to actually pass the arguments to that function.
I'm sure there should be a better way of handling the brute force handling of part 2, but for now, it's just a poor man's nested for loop implemented using atoms.
(nsaoc2019.day2(:require[clojure.string:asst]))(defnget-action"Selects an action from the available opcodes"[action](condp=action1+2*99(constantly"stop")))(defnparse-input"Converts input into atom holding instruction integers."[input](map#(Integer/parseInt%)(st/split(st/trim-newlineinput)#",")))(defninitial-adjustments[instructionsnounverb](->instructions(assoc1noun)(assoc2verb)))(defncalculate"Read opcodes, insert required values, and read position 0 when complete."[inputnounverb](loop[instructions(initial-adjustments(vec(parse-inputinput))nounverb)cursor0](let[[actionpos1pos2result-pos](take4(dropcursorinstructions))result(apply(get-actionaction)[(getinstructionspos1)(getinstructionspos2)])](if(=result"stop")(firstinstructions)(recur(associnstructionsresult-posresult)(+4cursor))))))(defnp2019-02-part1[input](calculateinput122))(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(calculateinput@noun@verb))(swap!verbinc)(if(=@verb100)(do(swap!nouninc)(reset!verb0))))(+@verb(*100@noun))))(defnrun"Runs the Day 2 solutions."[](let[input(slurp"inputs/2019-02-input")](println(str"Part 1: "(p2019-02-part1input)))(println(str"Part 2: "(p2019-02-part2input)))))
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.
Back again with more Clojure.
My favorite part of this is how the actions get selected. the
get-action
function actually returns the function used for handling the inputs (where 99 returns a function that, regardless of input, returns "stop"). Then, that function is used with theapply
function insidecalculate
to actually pass the arguments to that function.I'm sure there should be a better way of handling the brute force handling of part 2, but for now, it's just a poor man's nested for loop implemented using atoms.