Finds networks of resistors to approximate resistances.
Revisão | d0bdb9a227d150d2c81511cb61e1ff7ce501a5ab (tree) |
---|---|
Hora | 2020-03-11 16:12:16 |
Autor | AlaskanEmily <emily@alas...> |
Commiter | AlaskanEmily |
Sort results by resistor count, then by how close it is to the target
@@ -443,7 +443,19 @@ main(!IO) :- | ||
443 | 443 | % Read from stdin |
444 | 444 | read_resistors_from_stdin(rbtree.init, Resistors, !IO), |
445 | 445 | Sort = (pred(A::in, B::in, Cmp::out) is det :- |
446 | - builtin.compare(Cmp, length(A), length(B)) ), | |
446 | + builtin.compare(LenCmp, length(A), length(B)), | |
447 | + % If the lengths are identical, sort by how close the | |
448 | + % resistance is to our target. | |
449 | + ( if | |
450 | + LenCmp = (=) | |
451 | + then | |
452 | + % Not entirely sure why the type hinting is needed... | |
453 | + builtin.compare(Cmp, | |
454 | + abs(value(A) - Target):int, | |
455 | + abs(value(B) - Target)) | |
456 | + else | |
457 | + LenCmp = Cmp | |
458 | + ) ), | |
447 | 459 | |
448 | 460 | % Try a simple network. |
449 | 461 | promise_equivalent_solutions [SimpleResults] ( |