Learn Java Easy. Chapter 15. Bypassing the Timer System

Chapter 15. Bypassing the Time Counter System

Chapter 15. Bypassing the Time Counter System

The corridor leading to the prison exit is the most heavily guarded. You might think it’s because of fear that prisoners might escape through there, but that’s not the case. What happens is that everyone enters and exits through the same place. Both visitors and officials, as well as prisoners, always follow the same path.

Every time someone accesses the prison, they are searched and images are recorded. Additionally, there are three registration points, divided into several rooms. At each stop, there is a guard who doesn’t leave their post until a relief arrives.

Bud believes that at most, there will be two people. It’s even possible that at some point they might need to perform some task during their shift, and there might be a short period when we could be alone.

The plan is to identify, measure, and analyze the activities of the prison guards to know exactly when we should attempt the escape. To do this, the first step will be to analyze when the doors open and close. That way, we can identify when there is movement. We’ll create a program that calculates the downtime of the door leading to the waiting room. The door to the cells we can control visually, and there’s no need to worry about the street door since no one uses it at night.

We have a small advantage: with one of the keys obtained in Vicente’s office, Pedro has managed to access the seven activity logs of the door from last week, one for each day. With this, we can already get an idea of which day is the quietest and the hours when the guards come and go. The first step will be to determine which day we choose. For this, we need to create a program that reads the door activity periods and sums them up. We’ll choose the day to escape that has the least total activity time.

The file divides the day into minutes, and each line informs us of how many seconds the door was active during that minute. To give you an idea, it has this format:

0
0
0
0
60
60
12
0
0

Exercise 1. Register Activity Time. We need to register the door activity time to choose a day for our escape. For this, we need to write a program that reads the downtime from a file and records the total downtime in another file.

Instructions:

  1. We use the activity.txt file obtained by Pedro, with activity times in seconds (one per line).
  2. Read the activity times from the file.
  3. Calculate the total downtime.
  4. Write the total downtime to a file total_activity.txt

Solution:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        String inputFile = "activity.txt";
        String outputFile = "total_activity.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {

            String line;
            int totalActivity = 0;

            while ((line = br.readLine()) != null) {
                totalActivity += Integer.parseInt(line);
            }

            bw.write("Total activity time: " + totalActivity + " seconds");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Monday is the day with the least activity, so now we know when we should start the escape. According to the log, from minute 1322 to 1438, that is, from 10:02 PM to 11:58 PM, the door is inactive. Then, it approximately reactivates around 2:00 AM and subsequently every hour until 6:00 AM, when the main door opens and fills with police. Each time, the doors are active and open for less than two minutes.

Once we know when the doors open, we need to determine if they opened to let someone in or perhaps for someone to exit. Officials must perform a series of tasks during their shift. Pedro has gained access to the log of these nightly tasks and the times guards mark them as completed. We need to create a small program that records these activities and returns them in a text format. We know that their main activities are: regular patrol, inspection and maintenance, and cleaning and disinfection.

The log file looks like this:

"iRKJi2KpMW", "ixpFHX0v8s", "rMxNXbeUxC", "bjO9IUsFGc", "LpTB2ffO8w", "S4WwZffjqB", "HuuSb3RH6l", "O8OmH1ApJx", "LpFKbFT7hU", "TZ9BmcroA7", "dyLWJCvbU6", "qNlWgK2wzD", "s6vEOgmfsY", "ccDtnysyec", "waITKPmWbY", "HH9N0Ufih4", "cGIet9lE4y", "MCwCJ6LNQP", "OfecGXWhqY", "SOd3Tco6mI", "r8QwQDbHyp", "4V6bJhJ20x", "nzkiWhmxVj", "0tT2nDToVa", "67NEMfhs2x", "W3HXrDI7SJ", "KgGoiFJfMN", "2WK7UZVmgX", "3KNx0fDu54", "Rn20v6baaz", "t8jChEgFSz", "489dXWEFi6", "oNSsEfNwaP", "V4zx2r6XR8", "2dPXcKbZNQ", "WpsxQvEbVw", "7f6floF3VU", "W1joPNEiK1", "pXpGUMRsZq", "1M1jDBpiGu", "xjZBVieX1Q", "y3dVBuhTrJ", "s0YB6CVSFV", "vZQgDge2HJ", "EsWhUsnhLa", "hevhsUt0ux", "bYwU4Qy6Gl", "sCW32VXSfD", "mwCdwQ809e", "zyRwiYkfDk", "ko4o26MErP", "Lk1rspAJQK", "d5Y9IPuMk5", "yKj992kdnn", "Q1XsuVqUGr", "fHMDmXiAcp", "dql0nmQJcm", "qRqSHARwSA", "CgV8ne6fRG", "30DYrnKWoA", "r1QrdR0es2", "HXTV8XvYoP", "GQAWwyhMSL", "4yMmh7e5NW", "yT2kIJvDqN", "AbIqpbKj5h", "haAriF42gR", "O6MciwEwQ4", "hW8Pf9IkX7", "kUaN7Cnv96", "tAROxtCUiP", "aZEZynwwpj", "cqzR5Y3Ipi", "M69Qr3BWJj", "ySTegpfDxw", "f47AKb3Bsn", "XOsmCGvcSo", "wA7iC3Fq1k", "NqTC2OpWGr", "IvvkjHR0Q2", "JyT5lu4QPo", "2whhr0ixDg", "xcYX1ELvDi", "zTS8EJE0hh", "7gYXpaaPIs", "UiZ8tMlvfl", "uuSBzI6IfZ", "BdcQ3ci06N", "VNrQV6uZvU", "gZILASO2gT", "iOIRRKFxhc", "jkSGZFtIpw", "2gkQKh6J8L", "jnTyMxcG8I", "X07Mj6M0Rj", "SqjfH3cyLO", "GfKiA3DZ4w", "RBIquXXt0k", "udPC3Q0PXo", "CM44vkuwyT", "20n5Uwv6s8", "5ST85ixStb", "uliTvpFukE", "ErQPLyDuRw", "XX5eaAXr1a", "nLenQQgqoP", "ThjBwzns36", "8RghH8Vneq", "FWFaHu0c2a", "HqGai14Ao6", "nFEi87Bafj", "rotkzVlY3k", "Tah5BZU49N", "YILUBnOTTh", "kyhPp7TWZp", "q1kiVAMsHv", "OvKuo3mvSX", "htDbhBg7vs", "O8ZBRQB5cO", "TlUDfrzdUi", "7yIIltvZSI", "HkAkB7qO3c", "j4DvSZL3Ry", "ONmRHuWdGc", "a7LmXNTfap", "MlHEWHZhrz", "ADKHiZeBAS", "0KQ6aXlsZY", "OKMfZsps6e", "vJpOxdfzEd", "BKs9UTKTQJ", "DBNrFCM5d7", "hse8XLelZK", "3jz3bLreNc", "ZZSHQ35FpT", "jOmaC6iMOW", "iXvhHmq2pn", "gqnfV11and", "DDfLkv3uBg", "UQjsb1ixF0", "08zSNrhvxs", "LSWyFeFyYv", "Regular patrol", "4Bpd1idsum", "vCgKs8GFTI", "uhUIpi1VLd", "AQn63M3ANF", "Y39NIKxIgJ", "YcpNNmT3GX", "C8Di70GqS7", "Ybg4P3BOij", "6Tb4opfNeC", "aTmzLU9opz", "QSHg6ZRmyH", "lVUz2kHAOn", "Inspection and maintenance", "wUxaxSm9lC", "DVTb4JEZQ9", "sjahqvnVsk", "TfvPongU7w", "gQ6g0rJ7ax", "bejbKLd6ZR", "SMj8MjDEWY", "fanVKMvsmZ", "Regular patrol", "gmqX2oBxvm", "Mb9B4SHLvM", "64rh8hh6Li", "SjalZaHsOA", "yxDLOhr4Od", "qfN1GbZNFA", "C4q5ridJ7I", "aQLm7SJeEo", "LNDWA1UHzG", "QpYvFfSY6S", "I7ySXRZkQH", "WwtkFmM7pP", "gl794m6lyl", "6fvgHjprUx", "SVlNByrKBN", "DNEmWPvl51", "Mm6NxxNkdF", "gyLw8TP8kH", "vcqjXfWTYz", "bpuMR5EVFB", "sMMP5HpxJg", "9hU32sjepp", "RsYnv80Hx2", "SilPvzI3gD", "L7pspbXgSw", "RvAtxqkYF2", "9gOPHDbw31", "uUDPlKRjU3", "NkPKW7zkZK", "dBOiqar26k", "GXPsNzuULq", "KwwpvvPqQ2", "Tif0qfs2eq", "3xTc242fPW", "c5jJz3DVrV", "zrK0VW0AQH", "zPJt2lGxKU", "C3ssWKqU5s", "XsfI0o5asg", "QiR4pDd4N0", "q4iY9xGn3V", "msQISGxfMJ", "yRyD1ec8fb", "dBfcrywOUD", "O9cMzNXF5t", "lNuV5TSkbO", "YEgbviK7AV", "HKrrLQBtt2", "ZU0hqtXuNa"

Exercise 2. Generate an Activity Log. Bud needs to generate an activity log to analyze the behavior of the prison guards. Write a program that records the different system activities in a file.

Instructions:

  1. Create an array with different activities («Regular patrol», «Inspection and maintenance», » Cleaning and disinfection»).
  2. Write these activities to a file activity_log.txt, one per line.

Solution:

import java.io.*;

public class Main {
    public static void main(String[] args) {

        String[] activities = {
            "iRKJi2KpMW", "ixpFHX0v8s", "rMxNXbeUxC", "bjO9IUsFGc", "LpTB2ffO8w", "S4WwZffjqB", "HuuSb3RH6l", "O8OmH1ApJx", "LpFKbFT7hU",
            "TZ9BmcroA7", "dyLWJCvbU6", "qNlWgK2wzD", "s6vEOgmfsY", "ccDtnysyec", "waITKPmWbY", "HH9N0Ufih4", "cGIet9lE4y", "MCwCJ6LNQP", "OfecGXWhqY",
            "SOd3Tco6mI", "r8QwQDbHyp", "4V6bJhJ20x", "nzkiWhmxVj", "0tT2nDToVa", "67NEMfhs2x", "W3HXrDI7SJ", "KgGoiFJfMN", "2WK7UZVmgX", "3KNx0fDu54",
            "Rn20v6baaz", "t8jChEgFSz", "489dXWEFi6", "oNSsEfNwaP", "V4zx2r6XR8", "2dPXcKbZNQ", "WpsxQvEbVw", "7f6floF3VU", "W1joPNEiK1", "pXpGUMRsZq",
            "1M1jDBpiGu", "xjZBVieX1Q", "y3dVBuhTrJ", "s0YB6CVSFV", "vZQgDge2HJ", "EsWhUsnhLa", "hevhsUt0ux", "bYwU4Qy6Gl", "sCW32VXSfD", "mwCdwQ809e",
            "zyRwiYkfDk", "ko4o26MErP", "Lk1rspAJQK", "d5Y9IPuMk5", "yKj992kdnn", "Q1XsuVqUGr", "fHMDmXiAcp", "dql0nmQJcm", "qRqSHARwSA", "CgV8ne6fRG",
            "30DYrnKWoA", "r1QrdR0es2", "HXTV8XvYoP", "GQAWwyhMSL", "4yMmh7e5NW", "yT2kIJvDqN", "AbIqpbKj5h", "haAriF42gR", "O6MciwEwQ4", "hW8Pf9IkX7",
            "kUaN7Cnv96", "tAROxtCUiP", "aZEZynwwpj", "cqzR5Y3Ipi", "M69Qr3BWJj", "ySTegpfDxw", "f47AKb3Bsn", "XOsmCGvcSo", "wA7iC3Fq1k", "NqTC2OpWGr",
            "IvvkjHR0Q2", "JyT5lu4QPo", "2whhr0ixDg", "xcYX1ELvDi", "zTS8EJE0hh", "7gYXpaaPIs", "UiZ8tMlvfl", "uuSBzI6IfZ", "BdcQ3ci06N", "VNrQV6uZvU",
            "gZILASO2gT", "iOIRRKFxhc", "jkSGZFtIpw", "2gkQKh6J8L", "jnTyMxcG8I", "X07Mj6M0Rj", "SqjfH3cyLO", "GfKiA3DZ4w", "RBIquXXt0k", "udPC3Q0PXo",
            "CM44vkuwyT", "20n5Uwv6s8", "5ST85ixStb", "uliTvpFukE", "ErQPLyDuRw", "XX5eaAXr1a", "nLenQQgqoP", "ThjBwzns36", "8RghH8Vneq", "FWFaHu0c2a",
            "HqGai14Ao6", "nFEi87Bafj", "rotkzVlY3k", "Tah5BZU49N", "YILUBnOTTh", "kyhPp7TWZp", "q1kiVAMsHv", "OvKuo3mvSX", "htDbhBg7vs", "O8ZBRQB5cO",
            "TlUDfrzdUi", "7yIIltvZSI", "HkAkB7qO3c", "j4DvSZL3Ry", "ONmRHuWdGc", "a7LmXNTfap", "MlHEWHZhrz", "ADKHiZeBAS", "0KQ6aXlsZY", "OKMfZsps6e",
            "vJpOxdfzEd", "BKs9UTKTQJ", "DBNrFCM5d7", "hse8XLelZK", "3jz3bLreNc", "ZZSHQ35FpT", "jOmaC6iMOW", "iXvhHmq2pn", "gqnfV11and", "DDfLkv3uBg",
            "UQjsb1ixF0", "08zSNrhvxs", "LSWyFeFyYv", "Regular patrol", "4Bpd1idsum", "vCgKs8GFTI", "uhUIpi1VLd", "AQn63M3ANF", "Y39NIKxIgJ",
            "YcpNNmT3GX", "C8Di70GqS7", "Ybg4P3BOij", "6Tb4opfNeC", "aTmzLU9opz", "QSHg6ZRmyH", "lVUz2kHAOn", "Inspection and maintenance", "wUxaxSm9lC",
            "DVTb4JEZQ9", "sjahqvnVsk", "TfvPongU7w", "gQ6g0rJ7ax", "bejbKLd6ZR", "SMj8MjDEWY", "fanVKMvsmZ", "Regular patrol", "gmqX2oBxvm",
            "Mb9B4SHLvM", "64rh8hh6Li", "SjalZaHsOA", "yxDLOhr4Od", "qfN1GbZNFA", "C4q5ridJ7I", "aQLm7SJeEo", "LNDWA1UHzG", "QpYvFfSY6S", "I7ySXRZkQH",
            "WwtkFmM7pP", "gl794m6lyl", "6fvgHjprUx", "SVlNByrKBN", "DNEmWPvl51", "Mm6NxxNkdF", "gyLw8TP8kH", "vcqjXfWTYz", "bpuMR5EVFB", "sMMP5HpxJg",
            "9hU32sjepp", "RsYnv80Hx2", "SilPvzI3gD", "L7pspbXgSw", "RvAtxqkYF2", "9gOPHDbw31", "uUDPlKRjU3", "NkPKW7zkZK", "dBOiqar26k", "GXPsNzuULq", "KwwpvvPqQ2", "Tif0qfs2eq",
            "3xTc242fPW", "c5jJz3DVrV", "zrK0VW0AQH", "zPJt2lGxKU", "C3ssWKqU5s", "XsfI0o5asg", "QiR4pDd4N0", "q4iY9xGn3V", "msQISGxfMJ", "yRyD1ec8fb", "dBfcrywOUD", "O9cMzNXF5t", "lNuV5TSkbO", "YEgbviK7AV", "HKrrLQBtt2", "ZU0hqtXuNa"
        };

        String outputFile = "activity_log.txt";

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
            for (String activity : activities) {
                bw.write(activity);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Exercise 3. We need to analyze the activity log to identify repetitive patterns during the night. If any of them do not repeat during the night we intend to escape, it means the corridor might be empty.

Instructions:

  1. Read the file activity_log.txt.
  2. Count the number of times each activity occurs.
  3. Write the results to a file activity_summary.txt.

Solution:

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {

        String inputFile = "activity_log.txt";
        String outputFile = "activity_summary.txt";

        Map<String, Integer> activityCounter = new HashMap<>();

        try (
            BufferedReader br = new BufferedReader(new FileReader(inputFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))
        ) {
            String line;

            while ((line = br.readLine()) != null) {
                activityCounter.put(line, activityCounter.getOrDefault(line, 0) + 1);
            }

            for (Map.Entry<String, Integer> entry : activityCounter.entrySet()) {
                bw.write(entry.getKey() + ": " + entry.getValue());
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Exercise 4. Update the Activity Log. We need to update the activity log in real-time to reflect changes in the system. Write a program that adds new activities to the activity log file.

Instructions:

  1. Use an array to simulate new activities added to the system: «Camera monitoring», «Communication with the control post».
  2. Add these activities to the end of the file activity_log.txt.

Solution:

import java.io.*;

public class Main {
    public static void main(String[] args) {

        String[] newActivities = {
            "6UX5w1vjQJ", "KAB1GksspV", "ziuAVNowpG", "uG0BEclHuq", "0xgydgyaK7", "IkSjUcqTbg", "7ZzjJYTCID",
            "7oGiZxDCIP", "9BRePDOokd", "SLR5QqI1gm", "dzHGx84mQH", "tTP235qyws", "Fk7qbJXHUN", "b82VQ4j1R5",
            "B2I4iZ096c", "yI6ntrP2cz", "y4rC1OQMJg", "WIjc2dUrde", "OYukZtwS1L", "qkwGHmjPao", "HycTVJeLB4",
            "ZaUckOVqbT", "qODG1FAhVE", "erLTxdRe61", "qZ1NgPNFt9", "bUifZB8w4s", "Camera monitoring",
            "zCrzN1ZDUg", "gzCzIGm23u", "cDFm0hkrFw", "IDI0VBjizw", "klhN9ZnUyu", "GuscO6H64v", "DJrhWpPTqr",
            "zM3MWg5rSj", "DMYzD7vkHF", "BNghDoM55o", "yr89K38y7l", "H2qEcM6elW", "iw6OVfFJDh", "yj16xbky6M",
            "lFcJ6bQs54", "CNSJhoARCw", "jOxs8ry2fx", "INZj1DoLqA", "GSEGuaChrR", "jrE68aipWX", "h8doJ65vcr",
            "2EoBD02CKL", "C1av3xn5eG", "R8w7lp6uKQ", "sr1E5Nf6cz", "6C5Eff5czg", "WHVPxvyciZ", "KlO6H5Vduk",
            "x6NIpTkJBK", "3YvHL9eQQ0", "qYYtIPWjpv", "UZKlgaZ4TD", "msiexEsxj9", "RzoCEriGyZ", "vLuY3gwLJO",
            "d45CH4k7cs", "rGdyVlst2Q", "3XRVIfo9mc", "eWUlCZ8UOo", "Camera monitoring", "ic7SdX8di8",
            "C4qAoBxA3D", "Communication with the control post", "VBxKrQngfS", "fICz9PFnDX", "dk2ncCGc8x",
            "QG1MBYgT9i", "HAJkKvCVwT", "gYoMT0TLOu", "vaNsJoXLnd", "TpBnt4Ieav", "TduiTd43zP", "Qm2zhvHZXB",
            "n7BOCacWYA", "VY4N9FrFPB", "TACDi2dNQY", "VW4PHE8ZDb", "SDYMTOCNJc", "Jnt9v9dgKy", "pY0yYILavE",
            "G9ERpaXMNl", "qDkqFSu4Sr", "O3VyiIOh6E", "5qB9sTCmKH", "g5MyR0lXeu", "egzksA1lg2", "h6uLmm8tbp",
            "Fx7ay5mNkD", "neDWml2spW", "IwkPB704pi", "7WqwiNEBrv", "9ed11mNwh7", "kf3qNbMBkS", "MYFOZ3hHC8",
            "t4I0RK0M8O", "tkvxwGzdjg", "8YsZ8xVw0m", "EdFd9zOWF5", "Te6y3Oa5XP", "9sg2XIQxbQ", "kJhouzOXkS",
            "128laUqDWt", "nJDSEaneBg", "2rGSBCuzTd", "a5GbJeDTDh", "WVvQGSEkjL", "Q26giaMbeM", "ArdXbDImAh",
            "Ll3nyJ5PRS", "WjZL4th5V5", "fTtNnPheOS", "73rwDzESwL", "8eXQ7OW9gv", "SMgcsFvILx", "wRKnDVc55v",
            "UWC1QxcVZc"
        };

        String outputFile = "activity_log.txt";

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile, true))) {
            for (String activity : newActivities) {
                bw.write(activity);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Exercise 5. Manipulate Door Activity Times. Write a program that reads activity times from a file and modifies them.

Instructions:

  1. Create a file registration_times.txt with registration times in milliseconds (one per line).
  2. Read the registration times from the file.
  3. Increase each time by a fixed amount (1 second).
  4. Write the modified times to a file modified_times.txt.

Solution:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        String inputFile = "activity.txt";
        String outputFile = "modified_times.txt";
        int increment = 1;

        try (
            BufferedReader br = new BufferedReader(new FileReader(inputFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                long originalTime = Long.parseLong(line);
                long modifiedTime = originalTime + increment;
                bw.write(Long.toString(modifiedTime));
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

As expected, the program quickly detected the error and proceeded to identify and repair it. It simply restarted the system, and the logs returned to normal.

We achieved our goal: during that maintenance and error-searching period, a temporal window opened that allowed modifying all files. Pedro took advantage of that moment to change the list of scheduled tasks for Monday to the list we needed, the one that leaves the corridor free for an hour.

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏