Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

A Java-based solution #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions mask.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/sh

# Call your program here instead of cat.
cat
java -cp solution/classes luhnybin.Filter
Binary file added solution/classes/luhnybin/Filter.class
Binary file not shown.
73 changes: 73 additions & 0 deletions solution/src/luhnybin/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package luhnybin;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Filter {

InputStream in;
OutputStream out;
List<Integer> buffer = new ArrayList<Integer>();
List<Integer> digits = new ArrayList<Integer>();

public static void main(String[] args) throws IOException {
new Filter(System.in, System.out).process();
}

public Filter(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}

public void process() throws IOException {
for (int b; (b = in.read()) != -1;) {
if (!isCreditCardSymbol(b)) {
flush();
out.write(b);
continue;
}
buffer.add(b);
if (isDigit(b)) digits.add(b);
if (digits.size() < 14) continue;
int count = findLongestTrailingCreditCardNumber();
maskTrailingDigitsInBuffer(count);
}
flush();
}

void flush() throws IOException {
for (Integer b : buffer) out.write(b);
buffer.clear();
digits.clear();
}

void maskTrailingDigitsInBuffer(int count) {
for (int i = buffer.size() - 1; i >= 0 && count > 0; i--)
if (isDigit(buffer.get(i))) {
buffer.set(i, (int)'X');
count--;
}
}

int findLongestTrailingCreditCardNumber() {
int result = 0, sum = 0, limit = Math.min(digits.size(), 16);
for (int i = 1; i <= limit; i++) {
int digit = digits.get(digits.size() - i) - 48;
if (i % 2 == 0) digit *= 2;
sum += digit / 10 + digit % 10;
if (i >= 14 && sum % 10 == 0) result = i;
}
return result;
}

boolean isCreditCardSymbol(int b) {
return isDigit(b) || b == 32 || b == 45;
}

boolean isDigit(int b) {
return b >= 48 && b <= 57;
}
}