2020-12-06 17:19:02 +00:00
|
|
|
// vim: filetype=kickass
|
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
*=* "Day 01"
|
2020-12-06 17:19:02 +00:00
|
|
|
day01:
|
2020-12-09 01:08:57 +00:00
|
|
|
move_16_imm($03, $04, str_title)
|
2020-12-06 17:19:02 +00:00
|
|
|
jsr write_string
|
|
|
|
|
|
|
|
move_16_imm($03, $04, day01_input)
|
2020-12-09 01:08:57 +00:00
|
|
|
move_16_imm($25, $26, buffer)
|
2020-12-06 17:19:02 +00:00
|
|
|
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
|
2020-12-09 01:08:57 +00:00
|
|
|
bne !digit-
|
|
|
|
|
|
|
|
inc $04
|
2020-12-06 17:19:02 +00:00
|
|
|
jmp !digit-
|
|
|
|
|
|
|
|
!break:
|
|
|
|
sty $24
|
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
ldy #0
|
2020-12-06 17:19:02 +00:00
|
|
|
lda $22
|
2020-12-09 01:08:57 +00:00
|
|
|
sta ($25), y
|
|
|
|
iny
|
|
|
|
lda $23
|
|
|
|
sta ($25), y
|
2020-12-06 17:19:02 +00:00
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
lda #2
|
|
|
|
i16_i8_add_a($25, $26)
|
2020-12-06 17:19:02 +00:00
|
|
|
|
|
|
|
ldy $24
|
|
|
|
iny
|
2020-12-09 01:08:57 +00:00
|
|
|
bne !+
|
|
|
|
inc $04
|
|
|
|
!:
|
2020-12-06 17:19:02 +00:00
|
|
|
lda ($03), y
|
|
|
|
bne !line-
|
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
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-)
|
|
|
|
|
2020-12-06 17:19:02 +00:00
|
|
|
!done:
|
2020-12-09 01:08:57 +00:00
|
|
|
// 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
|
2020-12-06 17:19:02 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
str_title:
|
|
|
|
.text "# Day 01, part 1:"
|
|
|
|
.byte '\n', 0
|
|
|
|
|
|
|
|
str_parse_done:
|
|
|
|
.text "* Finished parsing"
|
|
|
|
.byte '\n', 0
|
2020-12-06 17:19:02 +00:00
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
str_calc_done:
|
|
|
|
.text "* Finished calculating"
|
2020-12-06 17:19:02 +00:00
|
|
|
.byte '\n', 0
|
|
|
|
|
2020-12-09 01:08:57 +00:00
|
|
|
str_no_result:
|
2020-12-06 17:19:02 +00:00
|
|
|
.byte '\n'
|
2020-12-09 01:08:57 +00:00
|
|
|
.text "! Could not find a result"
|
2020-12-06 17:19:02 +00:00
|
|
|
.byte '\n', 0
|