2020/c64/day05.asm

196 lines
3.6 KiB
NASM

// vim: filetype=kickass
* = * "Day 05 Input"
!input:
.import text "../inputs/05"
.byte 0
* = * "Day 05 Code"
//
// day05
//
// Destroys: $03..04, $08..09, $0a..0d
//
.const column_min = $0a
.const column_max = $0b
.const row_min = $0c
.const row_max = $0d
.const input_ptr = $08
.const max_value = $0e
.const min_value = $2c
// Conflicts with tree_values_lo and _hi
.const buffer = $c800 // $c800..cfff
day05:
move_16_imm($03, !str_title+)
jsr write_string
lda #0
tay
!loop:
.for (var i = $0000; i < $0800; i += $0100)
sta buffer + i, y
iny
bne !loop-
move_16_imm(input_ptr, !input-)
move_16_imm(max_value, 0)
move_16_imm(min_value, $ffff)
ldy #0
!init_vars:
lda #128
sta row_max
lda #8
sta column_max
lda #0
sta row_min
sta column_min
!loop:
lda ($08), y
// TODO could be optimized easily
cmp #'F'
bne !else+
// lower half
lda row_min
adc row_max
lsr
sta row_max
jmp !next+
!else:
cmp #'B'
bne !else+
// upper half
lda row_min
adc row_max
lsr
sta row_min
jmp !next+
!else:
cmp #'L'
bne !else+
// lower half column
lda column_min
adc column_max
lsr
sta column_max
jmp !next+
!else:
cmp #'R'
bne !else+
// upper half column
lda column_min
adc column_max
lsr
sta column_min
jmp !next+
!else:
cmp #'\n'
bne !else+
lda #0; sta row_min + 1
// row_min *= 8
lda row_min
asl; rol row_min + 1
asl; rol row_min + 1
asl; rol row_min + 1
// row_min |= column_min
ora column_min
sta row_min
lda row_min + 1
cmp max_value + 1
bcc !not_max+
bne !max+
lda row_min
cmp max_value
bcc !not_max+
!max:
u16_u16_move(max_value, row_min)
!not_max:
lda min_value + 1
cmp row_min + 1
bcc !not_max+
bne !max+
lda min_value
cmp row_min
bcc !not_max+
!max:
u16_u16_move(min_value, row_min)
!not_max:
lda row_min + 1
clc
adc #>buffer
sta row_min + 1
ldx #0
lda #1
sta (row_min, x)
iny
bne !+
//.break
inc $09
!:
jmp !init_vars-
!else:
jmp !done+
!next:
iny
bne !+
//.break
inc $09
!:
jmp !loop-
!done:
u16_u16_move(udivmod32_dividend, max_value)
move_16_imm(udivmod32_dividend + 2, 0)
jsr print_decimal_u32
move_16_imm($03, str_part2)
jsr write_string
lda #>buffer
clc
adc min_value + 1
sta column_min + 1
// Find the first free seat
ldy min_value
!loop:
y_buf_inc(column_min)
lda (column_min), y
bne !loop-
sty udivmod32_dividend
lda column_min + 1
sec
sbc #>buffer
sta udivmod32_dividend + 1
move_16_imm(udivmod32_dividend + 2, 0)
jsr print_decimal_u32
rts
!newline:
rts
!str_title:
.text "# Day 05"
.byte '\n', '\n'
.text "Part 1: "
.byte 0