Created
December 11, 2019 08:44
-
-
Save Rustywolf/f44fccb46d2c0d7506a8b9476a443081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define registers | |
$A: @end+0 | |
$B: $A+1 | |
$C: $B+1 | |
$D: $C+1 | |
$E: $D+1 | |
$F: $E+1 | |
$G: $F+1 | |
$H: $G+1 | |
$RET: $H+1 | |
$RESULT: $RET+1 | |
$START: $RESULT+1 | |
$END: $START+6 | |
$TOTAL: $END+6 | |
# from | |
IN $A | |
ADD $A 0 $START+0 | |
IN $A | |
ADD $A 0 $START+1 | |
IN $A | |
ADD $A 0 $START+2 | |
IN $A | |
ADD $A 0 $START+3 | |
IN $A | |
ADD $A 0 $START+4 | |
IN $A | |
ADD $A 0 $START+5 | |
# to | |
IN $A | |
ADD $A 0 $END+0 | |
IN $A | |
ADD $A 0 $END+1 | |
IN $A | |
ADD $A 0 $END+2 | |
IN $A | |
ADD $A 0 $END+3 | |
IN $A | |
ADD $A 0 $END+4 | |
IN $A | |
ADD $A 0 $END+5 | |
# SUBROUTINE - MAIN - entry point | |
main: | |
# reduce $start by one to check | |
ADD 0 this+5 $RET | |
JEZ 0 subtract_one | |
# compare $start to $end, halt if equal | |
ADD 0 this+5 $RET | |
JEZ 0 check_equal | |
JNZ $RESULT halt | |
# check if current $start is valid, inc $total if true | |
ADD 0 this+5 $RET | |
JEZ 0 check_valid | |
ADD $TOTAL $RESULT $TOTAL | |
# loop | |
JEZ 0 main | |
# SUBROUTINE - SUBTRACT ONE - subtract one from $start | |
subtract_one: | |
# Remove one from 6th digit, return if >=0 | |
ADD $START+5 -1 $START+5 | |
IFL $START+5 0 $A | |
JEZ $A $RET | |
# if <0 set to 9 | |
ADD 0 9 $START+5 | |
# Remove one from 5th, return if >=0 | |
ADD $START+4 -1 $START+4 | |
IFL $START+4 0 $A | |
JEZ $A $RET | |
# if <0 set to 9 | |
ADD 0 9 $START+4 | |
# Remove one from 4th, return if >=0 | |
ADD $START+3 -1 $START+3 | |
IFL $START+3 0 $A | |
JEZ $A $RET | |
# if <0 set to 9 | |
ADD 0 9 $START+3 | |
# Remove one from 3rd, return if >=0 | |
ADD $START+2 -1 $START+2 | |
IFL $START+2 0 $A | |
JEZ $A $RET | |
# if <0 set to 9 | |
ADD 0 9 $START+2 | |
# Remove one from 2nd, return if >=0 | |
ADD $START+1 -1 $START+1 | |
IFL $START+1 0 $A | |
JEZ $A $RET | |
# if <0 set to 9 | |
ADD 0 9 $START+1 | |
# Remove one from 1st, return if >=0 | |
ADD $START+0 -1 $START+0 | |
JEZ 0 $RET | |
# SUBROUTINE - CHECK VALID - check if $start is a valid password | |
check_valid: | |
ADD 0 $START+0 $A | |
ADD 0 1 $F | |
ADD 0 0 $G | |
second: | |
# compare 2nd to 1st, inc $F if equal | |
IFE $START+1 $A $B | |
ADD $F $B $F | |
# jump to next op if equal | |
JNZ $B third | |
# else if less, failure | |
IFL $START+1 $A $C | |
JNZ $C check_valid_failure | |
# else check if has created a valid pair, if so inc $G, set $A to current value and $F to 1 | |
IFE $F 2 $D | |
ADD $G $D $G | |
ADD 0 $START+1 $A | |
ADD 0 1 $F | |
third: | |
# compare 3rd to 2nd, inc $F if equal | |
IFE $START+2 $A $B | |
ADD $F $B $F | |
# jump to next op if equal | |
JNZ $B fourth | |
# else if less, failure | |
IFL $START+2 $A $C | |
JNZ $C check_valid_failure | |
# else check if has created a valid pair, if so inc $G, set $A to current value and $F to 1 | |
IFE $F 2 $D | |
ADD $G $D $G | |
ADD 0 $START+2 $A | |
ADD 0 1 $F | |
fourth: | |
# compare 4th to 3rd, inc $F if equal | |
IFE $START+3 $A $B | |
ADD $F $B $F | |
# jump to next op if equal | |
JNZ $B fifth | |
# else if less, failure | |
IFL $START+3 $A $C | |
JNZ $C check_valid_failure | |
# else check if has created a valid pair, if so inc $G, set $A to current value and $F to 1 | |
IFE $F 2 $D | |
ADD $G $D $G | |
ADD 0 $START+3 $A | |
ADD 0 1 $F | |
fifth: | |
# compare 5th to 4th, inc $F if equal | |
IFE $START+4 $A $B | |
ADD $F $B $F | |
# jump to next op if equal | |
JNZ $B sixth | |
# else if less, failure | |
IFL $START+4 $A $C | |
JNZ $C check_valid_failure | |
# else check if has created a valid pair, if so inc $G, set $A to current value and $F to 1 | |
IFE $F 2 $D | |
ADD $G $D $G | |
ADD 0 $START+4 $A | |
ADD 0 1 $F | |
sixth: | |
# compare 6th to 5th, inc $F if equal | |
IFE $START+5 $A $B | |
ADD $F $B $F | |
# jump to final if equal | |
JNZ $B final | |
# else if less, failure | |
IFL $START+5 $A $C | |
JNZ $C check_valid_failure | |
# else check if has created a valid pair, if so inc $G, set $A to current value and $F to 1 | |
IFE $F 2 $D | |
ADD $G $D $G | |
ADD 0 $START+5 $A | |
ADD 0 1 $F | |
final: | |
# check if 6th created valid pair | |
IFE $F 2 $D | |
ADD $G $D $G | |
# if $G > 0 then valid | |
JEZ $G check_valid_failure | |
# success | |
check_valid_success: | |
ADD 0 1 $RESULT | |
JEZ 0 $RET | |
# failure | |
check_valid_failure: | |
ADD 0 0 $RESULT | |
JEZ 0 $RET | |
# SUBROUTINE - CHECK EQUAL - check if $start equals $end | |
check_equal: | |
# Step 1 - compare all digits | |
IFE $START+0 $END+0 $A | |
IFE $START+1 $END+1 $B | |
IFE $START+2 $END+2 $C | |
IFE $START+3 $END+3 $D | |
IFE $START+4 $END+4 $E | |
IFE $START+5 $END+5 $F | |
# Step 2 - sum comparisons | |
ADD $A $B $A | |
ADD $A $C $A | |
ADD $A $D $A | |
ADD $A $E $A | |
ADD $A $F $A | |
# Step 3 - compare to 6 | |
ADD $A -6 $A | |
JEZ $A check_equal_success | |
# failure | |
check_equal_failure: | |
ADD 0 0 $RESULT | |
JEZ 0 $RET | |
# success | |
check_equal_success: | |
ADD 0 1 $RESULT | |
JEZ 0 $RET | |
# SUBROUTINE - HALT - process output and halt | |
halt: | |
OUT $TOTAL | |
HALT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment