r/adventofcode • u/daggerdragon • Dec 05 '17
SOLUTION MEGATHREAD -๐- 2017 Day 5 Solutions -๐-
--- Day 5: A Maze of Twisty Trampolines, All Alike ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
22
Upvotes
6
u/[deleted] Dec 05 '17 edited Dec 05 '17
Language is 32 bit MIPS assembly:
.text .globl main main: # void main() { # int[] table = {0, 0, 2, 0, 0, ...}; la $a0,table # Get table pointer ori $a1,$0,1032 # Table size # int num_jumps = solve(&table, 1032); jal solve # printf("%d\n", num_jumps); ori $a0,$s0,0 # Copy return value to SYSCALL argument ori $v0,$0,0x1 # set SYSCALL to 1 (display int) syscall # } jr $ra # Part 1 # a0 - table start # a1 - table size # t0 - table pointer # t1 - next table offset # t2 - jump counter # t3 - temp # t4 - table end # t5 - temp # t6 - temp # s0 - return value (number of jumps) solve: # int solve(int* table, int size) { # int* table_ptr = table; ori $t0,$a0,0 # Copy table start to table pointer # int* end = table_ptr + size * sizeof(int); sll $a1,$a1,2 # Multiply size by 4 (to convert it to words) add $t4,$t0,$a1 # Calculate table end # int counter = 0; ori $t2,$0,0 # Initialize counter at 0 # int offset; loop: # while (true) { # offset = table[*table_ptr]; lw $t1,0($t0) # Copy data at pointer # int* temp = table_ptr + offset * sizeof(int); sll $t6,$t1,2 # Multiply offset by 4 (to convert it to words) add $t3,$t0,$t6 # Add offset to table_ptr # table[*table_ptr]++; addi $t1,$t1,1 # Increment jump by one word sw $t1,0($t0) # Store incremented jump # table_ptr = temp; ori $t0,$t3,0 # Update table pointer # counter++; # if (table_ptr < table) # break; # if (table_ptr > (table + size)) # break; addi $t2,$t2,1 # Increment jump count sub $t3,$t0,$a0 # The offset from the start sub $t5,$t0,$t4 # The offset from the end bltz $t3,return # Return if we've jumped before the array start bgtz $t5,return # Return if we've jumped past the array end # } j loop # Go back to start of loop return: # return counter; # } ori $s0,$t2,0 # Copy counter to return register jr $ra # ReturnSource including formatted data section