Add c64 day 1 part 2

This commit is contained in:
Sijmen 2020-12-09 16:17:02 +01:00
parent 4d3b79b474
commit 4959a34dcb
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
1 changed files with 101 additions and 22 deletions

View File

@ -29,6 +29,9 @@ day01:
move_16_imm(input_pointer, day01_input) move_16_imm(input_pointer, day01_input)
ldy #0 ldy #0
lda #0
sta zp_temp
!line: !line:
// Create a temporary 16-bit integer to store the result in // Create a temporary 16-bit integer to store the result in
// in between digits. // in between digits.
@ -73,9 +76,9 @@ day01:
lda (input_pointer), y lda (input_pointer), y
bne !line- bne !line-
move_16_imm($03, str_parse_done) //
jsr write_string // Part 1
//
ldy #0 ldy #0
!loop: !loop:
// Subtract the number from 2020 to find the needed value. // Subtract the number from 2020 to find the needed value.
@ -102,9 +105,6 @@ day01:
sec sec
jsr multiply_16bit_unsigned jsr multiply_16bit_unsigned
move_16_imm($03, str_calc_done)
jsr write_string
// Print the 32-bit result as decimal. // Print the 32-bit result as decimal.
lda $25 lda $25
sta udivmod32_dividend + 3 sta udivmod32_dividend + 3
@ -116,27 +116,106 @@ day01:
sta udivmod32_dividend + 0 sta udivmod32_dividend + 0
jsr print_decimal_u32 jsr print_decimal_u32
rts move_16_imm($03, str_part2)
!error:
move_16_imm($03, str_no_result)
jsr write_string jsr write_string
.const val_a = $26
.const val_b = $fd
.const val_c = $44
.const val_temp = $46
.const outer_i = $1f
//
// Part 2
//
ldx #0
!outer_loop:
ldy #0
lda tree_values_lo, x
sta val_a + 0
lda tree_values_hi, x
sta val_a + 1
stx outer_i
!inner_loop:
// Subtract the numbers from 2020 to find the needed value.
lda tree_values_lo, y
sta val_b + 0
bne !not_zero+
lda tree_values_hi, y
bne !not_zero+
ldx outer_i
inx
bne !outer_loop-
!not_zero:
lda tree_values_hi, y
sta val_b + 1
lda val_a + 0
clc
adc val_b + 0
sta val_temp + 0
lda val_a + 1
adc val_b + 1
sta val_temp + 1
lda val_temp + 0
eor #$ff
sec
adc #<2020
sta $03
sta val_c + 0
lda val_temp + 1
eor #$ff
adc #>2020
sta $04
sta val_c + 1
iny
jsr tree_contains
bne !inner_loop-
// Multiply the three numbers together
sec
jsr multiply_16bit_unsigned
u16_u16_move($30, $22)
u16_u16_move($32, $24)
u16_u16_move($34, val_c)
move_16_imm($36, 0)
jsr multiply_32bit_unsigned
// Print the 32-bit result as decimal.
u16_u16_move(udivmod32_dividend, $38)
u16_u16_move(udivmod32_dividend + 2, $3a)
u16_u16_move(udivmod32_dividend + 4, $3c)
u16_u16_move(udivmod32_dividend + 6, $3e)
jsr print_decimal_u32
move_16_imm($03, str_done)
jsr write_string
rts rts
str_title: str_title:
.text "# Day 01, part 1:" .text "# Day 01"
.byte '\n', 0 .byte '\n', '\n'
.text "Part 1: "
.byte 0
str_parse_done: str_part2:
.text "* Finished parsing"
.byte '\n', 0
str_calc_done:
.text "* Finished calculating"
.byte '\n', 0
str_no_result:
.byte '\n' .byte '\n'
.text "! Could not find a result" .text "Part 2: "
.byte 0
str_done:
.byte '\n', '\n'
.text "* Done"
.byte '\n', 0 .byte '\n', 0