From 92a75560fbf45608c7686af1c0f70f000860bafc Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 11 Dec 2023 16:47:52 +0100 Subject: [PATCH] add decimal output to brainfuck programs --- bf/day_01.bf | 107 +++++++++++++++++++++++++++++++++++++++++++ bf/day_04.bf | 110 +++++++++++++++++++++++++++++++++++++++++++-- day_04/src/main.rs | 11 ++--- 3 files changed, 220 insertions(+), 8 deletions(-) diff --git a/bf/day_01.bf b/bf/day_01.bf index f7a3765..595d30e 100644 --- a/bf/day_01.bf +++ b/bf/day_01.bf @@ -90,3 +90,110 @@ marker at t7 , next char ] + +>>>> high byte of output +>>[-]>> >>>>- new c8 + +Convert to decimal and print output + underflow carry from high byte + [c1 + did not underflow + <+ undo underflow carry on c0 + >c1 + >> exit on c3 + ] c1 or c3 + >>>> c5 or c7 + +[->+]- marker c8 + + <<<< << c2 + loop c2 + ] (subtract 10 complete) + + + stop division (c6) if trying to high byte underflowed + >>>>- unmark c6 + <<<< << c0 + +[-c0 is not 255 + >>>> >>+ mark c6; subtractions can continue + <<< exit on c3 + ] c0 or c3 + >>>> >>>> c8 or c11 + +[-<+]- marker c8 + + + increase quotient + <<<+ addition carry c5 + <+ add c4 + [did not overflow + >- undo carry c5 + << exit on c3 + ] c4 or c3 + >>+[->+]- marker c8 + << + loop c6 + ] divided by 10 + store remainder: + <<<< < c1 + ++++ ++++ ++ add 10 to compensate overcounting + + >>>> >>>> c9 + [>]find next available spot + >++++++++[-<++++++>]< mark the end with a 48 (base for ascii digits) + <+[-<+]- marker c8 + <<<< <<< c1 + [- move c1 to end + >>>> >>>> c9 + [>]< end of array + + place value + <+[-<+]- c8 + <<<< <<< c1 + ] + >>>> >>>> + [>] find end of array + < + <+[-<+]- marker c8 + + move quotient to input: + <<<<- c4 + [-<<<+>>>] move c4 to c1 + >[-<<<< <+> >>>>] move c5 to c0 + + clear c7 if input is empty: + >>- mark quit + <<<< << c1 + [ + >>>> >>+ mark that more decimals exist + <<<< exit on c3 + ]c1 or c3 + >>>>> c6 or c8 + +[->+]- c8 + <<<< <<<< c0 + [ + >>>> >>>+ mark that more decimals exist + <<<< exit on c3 + ]c0 or c3 + >>>>> c5 or c8 + +[->+]- c8 + +[->+]- c8 + < c7 + loop c7 +] +>c8 ++ set marker to 0 +>[>]< go to end of array +[.<] print in reverse until start + + diff --git a/bf/day_04.bf b/bf/day_04.bf index 95b18f8..01b1fd2 100644 --- a/bf/day_04.bf +++ b/bf/day_04.bf @@ -7,9 +7,6 @@ Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 - - #### 297693052 - out0 out1 score0 score1 255 t0 t1 t2 t3 t4 255 win_0 win_1 etc @@ -254,3 +251,110 @@ out0 out1 score0 score1 ] ,loop on first character of the line (exits if all input is consumed) ] +<+ remove first marker +>>>>>> + remove array marker +<<- marker at c8 + +Convert to decimal and print output + underflow carry from high byte + [c1 + did not underflow + <+ undo underflow carry on c0 + >c1 + >> exit on c3 + ] c1 or c3 + >>>> c5 or c7 + +[->+]- marker c8 + + <<<< << c2 + loop c2 + ] (subtract 10 complete) + + + stop division (c6) if trying to high byte underflowed + >>>>- unmark c6 + <<<< << c0 + +[-c0 is not 255 + >>>> >>+ mark c6; subtractions can continue + <<< exit on c3 + ] c0 or c3 + >>>> >>>> c8 or c11 + +[-<+]- marker c8 + + + increase quotient + <<<+ addition carry c5 + <+ add c4 + [did not overflow + >- undo carry c5 + << exit on c3 + ] c4 or c3 + >>+[->+]- marker c8 + << + loop c6 + ] divided by 10 + store remainder: + <<<< < c1 + ++++ ++++ ++ add 10 to compensate overcounting + + >>>> >>>> c9 + [>]find next available spot + >++++++++[-<++++++>]< mark the end with a 48 (base for ascii digits) + <+[-<+]- marker c8 + <<<< <<< c1 + [- move c1 to end + >>>> >>>> c9 + [>]< end of array + + place value + <+[-<+]- c8 + <<<< <<< c1 + ] + >>>> >>>> + [>] find end of array + < + <+[-<+]- marker c8 + + move quotient to input: + <<<<- c4 + [-<<<+>>>] move c4 to c1 + >[-<<<< <+> >>>>] move c5 to c0 + + clear c7 if input is empty: + >>- mark quit + <<<< << c1 + [ + >>>> >>+ mark that more decimals exist + <<<< exit on c3 + ]c1 or c3 + >>>>> c6 or c8 + +[->+]- c8 + <<<< <<<< c0 + [ + >>>> >>>+ mark that more decimals exist + <<<< exit on c3 + ]c0 or c3 + >>>>> c5 or c8 + +[->+]- c8 + +[->+]- c8 + < c7 + loop c7 +] +>c8 ++ set marker to 0 +>[>]< go to end of array +[.<] print in reverse until start + + diff --git a/day_04/src/main.rs b/day_04/src/main.rs index 1afacf3..309b761 100644 --- a/day_04/src/main.rs +++ b/day_04/src/main.rs @@ -1,9 +1,10 @@ use std::fs; -fn main () { - for _ in 0..1000{ - a(); - b();} +fn main() { + for _ in 0..1000 { + a(); + b(); + } } fn a() { @@ -72,7 +73,7 @@ fn b() { cards.push(1); } let this_card = cards[card_id]; - for i in 1..(score+1) { + for i in 1..(score + 1) { if card_id + i >= cards.len() { cards.push(1 + this_card); } else {