diff options
author | June McEnroe <programble@gmail.com> | 2016-12-02 03:47:42 -0500 |
---|---|---|
committer | June McEnroe <programble@gmail.com> | 2016-12-02 03:47:42 -0500 |
commit | 28e544d2ba7017e84533886c6fd3b29c0955563a (patch) | |
tree | 850c3108b7502371143ffd4e9e5b8dd5e7b1a402 | |
parent | Day 1 (diff) | |
download | aoc-28e544d2ba7017e84533886c6fd3b29c0955563a.tar.gz aoc-28e544d2ba7017e84533886c6fd3b29c0955563a.zip |
Rewrite day 1 solution
-rw-r--r-- | day01.asm | 111 | ||||
-rw-r--r-- | input/day01.txt | 2 |
2 files changed, 55 insertions, 58 deletions
diff --git a/day01.asm b/day01.asm index 725c9b2..8ad4c66 100644 --- a/day01.asm +++ b/day01.asm @@ -1,72 +1,69 @@ %include "sys.asm" -global _start - -section .bss -input: resb 1 -section .data -position: - .x: dd 0 - .y: dd 0 -direction: - .x: dd 0 - .y: dd -1 +%define rPosX r8 +%define rPosY r9 +%define rDirX r10 +%define rDirY r11 -section .text +global _start _start: - .loopTurn: - syscall SYS_READ, FD_STDIN, input, 1 - test rax, rax - jz .breakTurn + sub rsp, 4096 + syscall SYS_READ, FD_STDIN, rsp, 4096 + mov rdi, rax ;; Length. + xor rcx, rcx ;; Index. - cmp byte [input], 'R' - jne .elseLeft - .thenRight: - rol qword [direction], 32 - neg dword [direction.x] - jmp .endLeft - .elseLeft: - neg dword [direction.x] - rol qword [direction], 32 - .endLeft: + xor rPosX, rPosX + xor rPosY, rPosY + xor rDirX, rDirX + mov rDirY, -1 ;; North. - xor r12, r12 - .loopDigit: - syscall SYS_READ, FD_STDIN, input, 1 - test rax, rax - jz .breakDigit - movzx rax, byte [input] + .loopTurn: + cmp byte [rsp + rcx], 'L' + jne .right + .left: ;; L(dx, dy) = (dy, -dx) + neg rDirX + jmp .swap + .right: ;; R(dx, dy) = (-dy, dx) + neg rDirY + .swap: + xchg rDirX, rDirY - cmp al, ',' - je .breakDigit + xor rax, rax + .loopDigit: + inc rcx + movzx rdx, byte [rsp + rcx] + cmp dl, '0' + jb .breakDigit - shl r12, 1 - lea r12, [r12 + r12 * 4 - '0'] - add r12, rax + ;; rax = rax * 10 + rdx - '0' + shl rax, 1 + lea rax, [rax + rax * 4 - '0'] + add rax, rdx jmp .loopDigit .breakDigit: + add rcx, 2 ;; Discard comma and space. + + mov rdx, rax + imul rdx, rDirX + add rPosX, rdx - syscall SYS_READ, FD_STDIN, input, 1 + mov rdx, rax + imul rdx, rDirY + add rPosY, rdx - mov eax, r12d - imul eax, [direction.x] - add [position.x], eax - imul r12d, [direction.y] - add [position.y], r12d - jmp .loopTurn - .breakTurn: + cmp rcx, rdi + jb .loopTurn - mov eax, [position.x] - sar eax, 31 - mov ecx, eax - xor ecx, [position.x] - sub ecx, eax + ;; abs(x) = (x ^ (x >> 63)) - (x >> 63) + mov rax, rPosX + sar rax, 63 + xor rPosX, rax + sub rPosX, rax - mov eax, [position.y] - sar eax, 31 - mov edx, eax - xor edx, [position.y] - sub edx, eax + mov rax, rPosY + sar rax, 63 + xor rPosY, rax + sub rPosY, rax - add ecx, edx -syscall SYS_EXIT, rcx + lea rax, [rPosX + rPosY] +syscall SYS_EXIT, rax diff --git a/input/day01.txt b/input/day01.txt index c08616b..711aa05 100644 --- a/input/day01.txt +++ b/input/day01.txt @@ -1 +1 @@ -R2, L5, L4, L5, R4, R1, L4, R5, R3, R1, L1, L1, R4, L4, L1, R4, L4, R4, L3, R5, R4, R1, R3, L1, L1, R1, L2, R5, L4, L3, R1, L2, L2, R192, L3, R5, R48, R5, L2, R76, R4, R2, R1, L1, L5, L1, R185, L5, L1, R5, L4, R1, R3, L4, L3, R1, L5, R4, L4, R4, R5, L3, L1, L2, L4, L3, L4, R2, R2, L3, L5, R2, R5, L1, R1, L3, L5, L3, R4, L4, R3, L1, R5, L3, R2, R4, R2, L1, R3, L1, L3, L5, R4, R5, R2, R2, L5, L3, L1, L1, L5, L2, L3, R3, R3, L3, L4, L5, R2, L1, R1, R3, R4, L2, R1, L1, R3, R3, L4, L2, R5, R5, L1, R4, L5, L5, R1, L5, R4, R2, L1, L4, R1, L1, L1, L5, R3, R4, L2, R1, R2, R1, R1, R3, L5, R1, R4 \ No newline at end of file +R2, L5, L4, L5, R4, R1, L4, R5, R3, R1, L1, L1, R4, L4, L1, R4, L4, R4, L3, R5, R4, R1, R3, L1, L1, R1, L2, R5, L4, L3, R1, L2, L2, R192, L3, R5, R48, R5, L2, R76, R4, R2, R1, L1, L5, L1, R185, L5, L1, R5, L4, R1, R3, L4, L3, R1, L5, R4, L4, R4, R5, L3, L1, L2, L4, L3, L4, R2, R2, L3, L5, R2, R5, L1, R1, L3, L5, L3, R4, L4, R3, L1, R5, L3, R2, R4, R2, L1, R3, L1, L3, L5, R4, R5, R2, R2, L5, L3, L1, L1, L5, L2, L3, R3, R3, L3, L4, L5, R2, L1, R1, R3, R4, L2, R1, L1, R3, R3, L4, L2, R5, R5, L1, R4, L5, L5, R1, L5, R4, R2, L1, L4, R1, L1, L1, L5, R3, R4, L2, R1, R2, R1, R1, R3, L5, R1, R4 |