Advertisement
Guest User

AOC2021 Day 1

a guest
Dec 1st, 2021
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .include "../framework/src/global.inc"
  2.  
  3. values:
  4. .word 199,200,208,210,200,207,240,269,260,263
  5. eof:
  6.  
  7. .export aoc_1a
  8. .proc aoc_1a
  9.   ;initalize count
  10.   lda #0
  11.   sta tempU16D
  12.   sta tempU16D+1
  13.  
  14.   ;set pointer to the word list
  15.   lda #<values
  16.   sta tempPtr
  17.   lda #>values
  18.   sta tempPtr+1
  19.  
  20. mainLoop:
  21.   ;if we're on the 2nd to last entry, we're done
  22.   lda tempPtr+1
  23.   cmp #>(eof-2)
  24.   bne :+
  25.   lda tempPtr
  26.   cmp #<(eof-2)
  27.   bne :+
  28.     jmp done
  29. :
  30.  
  31.   ;load current and next
  32.   lda (tempPtr),y
  33.   sta tempU16B
  34.   iny
  35.   lda (tempPtr),y
  36.   sta tempU16B+1
  37.   iny
  38.   lda (tempPtr),y
  39.   sta tempU16C
  40.   iny
  41.   lda (tempPtr),y
  42.   sta tempU16C+1
  43.  
  44.   ;compare next and current
  45.   lda tempU16C+1
  46.   cmp tempU16B+1
  47.   beq :+
  48.   bcs isHigher
  49.   bcc isNotHigher
  50. :
  51.   lda tempU16C
  52.   cmp tempU16B
  53.   beq isNotHigher
  54.   bcs isHigher
  55.   jmp isNotHigher
  56.  
  57. isHigher:
  58.   ;increase the count
  59.   clc
  60.   lda tempU16D
  61.   adc #1
  62.   sta tempU16D
  63.   lda tempU16D+1
  64.   adc #0
  65.   sta tempU16D+1
  66.  
  67. isNotHigher:
  68.  
  69.   ;go to next number
  70.   clc
  71.   lda tempPtr
  72.   adc #2
  73.   sta tempPtr
  74.   lda tempPtr+1
  75.   adc #0
  76.   sta tempPtr+1
  77.   jmp mainLoop
  78.  
  79. done:
  80.  
  81.   DISPLAY_ANSWER_16 tempU16D
  82.   rts
  83.  
  84. .endproc
  85.  
  86.  
  87.  
  88. .export aoc_1b
  89. .proc aoc_1b
  90.   ;initalize count
  91.   lda #0
  92.   sta tempU16D
  93.   sta tempU16D+1
  94.  
  95.   ;set pointer to the word list
  96.   lda #<values
  97.   sta tempPtr
  98.   lda #>values
  99.   sta tempPtr+1
  100.  
  101. mainLoop:
  102.   ;if we're on the 3rd to last entry, we're done
  103.   lda tempPtr+1
  104.   cmp #>(eof-(2 * 3))
  105.   bne :+
  106.   lda tempPtr
  107.   cmp #<(eof-(2 * 3))
  108.   bne :+
  109.     jmp done
  110. :
  111.  
  112.   ;load current and next
  113.   ldy #0
  114.   jsr getWindow
  115.   sta tempU16B
  116.   stx tempU16B+1
  117.  
  118.   ldy #2
  119.  
  120.   jsr getWindow
  121.   sta tempU16C
  122.   stx tempU16C+1
  123.  
  124.   ;compare next and current
  125.   lda tempU16C+1
  126.   cmp tempU16B+1
  127.   beq :+
  128.   bcs isHigher
  129.   bcc isNotHigher
  130. :
  131.   lda tempU16C
  132.   cmp tempU16B
  133.   beq isNotHigher
  134.   bcs isHigher
  135.   jmp isNotHigher
  136.  
  137. isHigher:
  138.   ;increase the count
  139.   clc
  140.   lda tempU16D
  141.   adc #1
  142.   sta tempU16D
  143.   lda tempU16D+1
  144.   adc #0
  145.   sta tempU16D+1
  146.  
  147. isNotHigher:
  148.  
  149.   ;go to next number
  150.   clc
  151.   lda tempPtr
  152.   adc #2
  153.   sta tempPtr
  154.   lda tempPtr+1
  155.   adc #0
  156.   sta tempPtr+1
  157.   jmp mainLoop
  158.  
  159. done:
  160.  
  161.   DISPLAY_ANSWER_16 tempU16D
  162.   rts
  163.  
  164. getWindow:
  165.   lda (tempPtr),y
  166.   sta 0
  167.   iny
  168.   lda (tempPtr),y
  169.   sta 1
  170.   iny
  171.  
  172.   ;pre-load these first because we might carry
  173.   ; while incrementing y
  174. .repeat 2
  175.   lda (tempPtr),y
  176.   sta 2
  177.   iny
  178.   lda (tempPtr),y
  179.   sta 3
  180.   iny
  181.  
  182.   lda 2
  183.   clc
  184.   adc 0
  185.   sta 0
  186.  
  187.   lda 3
  188.   adc 1
  189.   sta 1
  190. .endrepeat
  191.  
  192.   lda 0
  193.   ldx 1
  194.   rts
  195.  
  196. .endproc
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement