2020/c64/day01.asm
2020-12-09 03:17:56 +01:00

153 lines
2.2 KiB
NASM

// vim: filetype=kickass
*=* "Day 01"
day01:
move_16_imm($03, $04, str_title)
jsr write_string
move_16_imm($03, $04, day01_input)
move_16_imm($25, $26, buffer)
ldy #0
!line:
lda #0
sta $22
sta $23
!digit:
lda ($03), y
cmp #'\n'
beq !break+
i16_mul10($22, $23)
lda ($03), y
sec
sbc #'0'
i16_i8_add_a($22, $23)
iny
bne !digit-
inc $04
jmp !digit-
!break:
sty $24
ldy #0
lda $22
sta ($25), y
iny
lda $23
sta ($25), y
lda #2
i16_i8_add_a($25, $26)
ldy $24
iny
bne !+
inc $04
!:
lda ($03), y
bne !line-
move_16_imm($25, $26, buffer)
move_16_imm($03, $04, str_parse_done)
jsr write_string
// Add every number to every other number
!outer:
move_16_imm($27, $28, buffer)
// $03..$04 = buffer[i]
ldy #0
lda ($25), y
sta $03
iny
lda ($25), y
sta $04
dey
lda ($25), y
bne !not_zero+
iny
lda ($25), y
bne !not_zero+
.break
jmp !error+
!not_zero:
// Subtract from 2020 to see what number we need
i16_imm_i16_sub($fb, $fc, 2020, $03, $04)
!inner:
// $fd..$fe = buffer[j]
ldy #0
lda ($27), y
sta $fd
iny
lda ($27), y
sta $fe
dey
lda ($27), y
bne !not_zero+
iny
lda ($27), y
bne !not_zero+
i16_inc($25, $26)
jmp !outer-
!not_zero:
// j++
lda #2
i16_i8_add_a($27, $28)
i16_i16_cmp_bne($fb, $fc, $fd, $fe, !inner-)
!done:
// Multiply the results together
sec
jsr multiply_16bit_unsigned
move_16_imm($03, $04, str_calc_done)
jsr write_string
lda $25
sta udivmod32_dividend + 3
lda $24
sta udivmod32_dividend + 2
lda $23
sta udivmod32_dividend + 1
lda $22
sta udivmod32_dividend + 0
jsr print_decimal_u32
rts
!error:
move_16_imm($03, $04, str_no_result)
jsr write_string
rts
str_title:
.text "# Day 01, part 1:"
.byte '\n', 0
str_parse_done:
.text "* Finished parsing"
.byte '\n', 0
str_calc_done:
.text "* Finished calculating"
.byte '\n', 0
str_no_result:
.byte '\n'
.text "! Could not find a result"
.byte '\n', 0