Day 19: Aplenty

I found this quite difficult although have solved part one after quite a while. The problem seemed simple enough but once I got into the details of the code it became quite confusing and muddled, with naming as well. After a long time looking deeply at an aspect of the code it is easy to get hypnotised and confused. It might also be because it is Christmas and people are talking around me. It is a bit difficult to focus, but in general I need to get better at not getting confused with this kind of problem, and start learning about automated testing in the new year.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 const inputToUse = realInput; const inputLines = inputToUse.split(/\n/); // console.log('inputLines',inputLines); var workflowsLogged = false; const workflows = []; const parts = []; inputLines.forEach(function(line){ const cleanLine = line.trim(); // console.log('cleanLine',cleanLine); if( !cleanLine.length ) { workflowsLogged = true; } else { if( workflowsLogged ) { // ! - it's a part const trimLine = cleanLine.slice(1,cleanLine.length-1); // console.log('trimLine',trimLine); // partLines.push(trimLine); const partStatLines = trimLine.split(','); // console.log('partStatLines',partStatLines); let statObj = {}; partStatLines.forEach(function(statLine) { const statArr = statLine.split('='); // console.log('statArr',statArr); statObj = { ...statObj, [statArr[0]]:statArr[1] } }); // console.log('statObj',statObj); parts.push(statObj); } else { // ! - it's a workflow // workflowLines.push(cleanLine); const workflowArr = cleanLine.split('{'); const name = workflowArr[0]; const wfLine = workflowArr[1].slice(0, workflowArr[1].length-1); // console.log('wfLine',wfLine); const rulesArr = wfLine.split(','); const wfRules = []; // console.log('rules',rules); rulesArr.forEach(function(rule){ // console.log('rule',rule); var wf = {}; if( rule.includes(':') ) { const ruleArr = rule.split(':'); // console.log('ruleArr',ruleArr); const condition = ruleArr[0]; var condObj = {}; if( condition.includes('>') || condition.includes('<') ) { let conditionName = ''; let conditionType = ''; let conditionValue = ''; if( condition.includes('>') ) { conditionType = '>'; const arr = condition.split('>'); conditionName = arr[0]; conditionValue = arr[1]; } else { conditionType = '<'; const arr = condition.split('<'); conditionName = arr[0]; conditionValue = arr[1]; } condObj = { conditionName: conditionName, type: conditionType, value: conditionValue } } // console.log('condObj',condObj); const outcome = ruleArr[1]; // console.log('outcome',outcome); wf = { condition: condObj, outcome: outcome } } else { wf = { outcome:rule } } wfRules.push(wf); }); const wfObj = { name: name, rules: wfRules } workflows.push(wfObj); } } }); console.log('workflows',workflows); console.log('parts',parts); const acceptedParts = []; parts.forEach(function(part){ // console.log('---- NEW PART'); console.log(part); var inLimbo = true; var currentWorkflowName = 'in'; const passedThru = []; while( inLimbo ) { // console.log('currentWorkflowName',currentWorkflowName); passedThru.push(currentWorkflowName); // find workflow with current name const currentWorkflow = Object.values(workflows).find((obj) => { return obj.name === currentWorkflowName }); // console.log('currentWorkflow',currentWorkflow); const rules = currentWorkflow.rules; for( var i=0; i<rules.length; i++ ) { const rule = rules[i]; console.log('NEW rule',rule); const condition = rule.condition; // console.log('condition',condition); if( condition ) { const conditionName = condition.conditionName; const ruleType = condition.type; console.log('ruleType',ruleType); const ruleVal = parseFloat(condition.value); console.log('ruleVal',ruleVal); const partValue = parseFloat(part[conditionName]); console.log('partValue',partValue); if( ruleType === '>' ) { if( partValue > ruleVal ) { const outcome = rule.outcome; if( outcome === 'R' ) { console.log('REJECTED'); inLimbo = false; break; } else if( outcome === 'A' ) { console.log('ACCEPTED'); acceptedParts.push(part); inLimbo = false; break; } else { currentWorkflowName = outcome; break; } } } else if( ruleType === '<' ) { if( partValue < ruleVal ) { const outcome = rule.outcome; if( outcome === 'R' ) { console.log('REJECTED'); inLimbo = false; break; } else if( outcome === 'A' ) { console.log('ACCEPTED'); acceptedParts.push(part); inLimbo = false; break; } else { currentWorkflowName = outcome; break; } } } } else { const outcome = rule.outcome; // console.log('outcome', outcome); if( outcome === 'R' ) { inLimbo = false; break; } else if( outcome === 'A' ) { acceptedParts.push(part); inLimbo = false; break; } else { currentWorkflowName = outcome; break; } } } } console.log('passedThru',passedThru); }); console.log('acceptedParts',acceptedParts); var ratingTotal = 0; acceptedParts.forEach(function(part) { var partTotal = 0; for( const [key, value] of Object.entries(part) ) { const val = parseFloat(value); partTotal = partTotal + val; } // console.log('partTotal', partTotal); ratingTotal = ratingTotal + partTotal; }); console.log('ratingTotal',ratingTotal);
All days