AB22's blog

By AB22, history, 10 months ago, In English

Hey Codeforces! (^_^)

I've been working on a Rust library that makes competitive programming in Rust much cleaner and less verbose. If you've ever tried Rust for CP, you know the pain of writing the same input parsing boilerplate over and over again.

The Problem

Standard Rust CP code looks like this:

use std::{
    io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write},
    iter::repeat,
};

fn solve<R: Read, W: Write>(input: R, output: W) {
    let mut reader = BufReader::new(input);
    let mut writer = BufWriter::new(output);
    let mut buf = String::new();

    reader.read_line(&mut buf).unwrap();
    let n: usize = buf.trim().parse().unwrap();
    buf.clear();

    reader.read_line(&mut buf).unwrap();
    let a: Vec<i64> = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();

    // rest of the logic here

    writeln!(writer, "{}", ans).unwrap();
}

fn main() {
    let stdin = stdin();
    let stdout = stdout();
    solve(stdin.lock(), stdout.lock());
}

That's way too much boilerplate! (>_<#)

The Solution

With this library, the same code becomes:

sol! {
    fn solution(n: usize, arr: [i64]) -> i64 {
        // Your actual solution logic here
        arr.iter().sum()
    }
}

That's it! Clean, readable, and focuses on the algorithm rather than I/O.

Features

Simple Macros

  • sol! for single test case problems
  • sol_n! for multiple test case problems (reads N, then N test cases)

Smart Input Parsing

  • The read_value! macro handles various input patterns:
sol! {
    fn solution(
        n: usize,                    // Single integer
        arr: [i32],                  // Space-separated integers
        matrix: [[i32]; n],          // n lines of space-separated integers
        s: [char],                   // String as char array
        binary: [01],                // Binary string as Vec<u8>
        fixed: [i32; 5],            // Exactly 5 integers
        lines: [String]; n           // n lines as strings
    ) -> String {
        format!("Parsed {} elements", arr.len())
    }
}

Flexible Output

// Automatic Yes/No for booleans
sol! {
    fn solution(n: i32) -> bool {
        n % 2 == 0  // Prints "Yes" or "No"
    }
}

// Lists with custom separators
use cp_lib::{Words, Lines, words_of, lines_of};

sol! {
    fn solution(arr: [i32]) -> Words<i32> {
        words_of(arr)  // Space-separated output
    }
}

Debug Support

sol! {
    fn solution(n: usize, arr: [i32]) -> i32 {
        dbg_cp!("Processing array of size: {}", n);  // Only prints in debug mode
        arr.iter().max().unwrap_or(&0).clone()
    }
}

Real Examples

Typical CF Problem

sol! {
    fn solution(n: usize, a: [i32]) -> i32 {
        let mut max_val = 0;
        let mut current = 0;
        
        for &x in &a {
            current = (current + x).max(x);
            max_val = max_val.max(current);
        }
        
        max_val
    }
}

Multiple Test Cases

sol_n! {
    fn solution(n: usize, s: [char]) -> bool {
        let count_a = s.iter().filter(|&&c| c == 'a').count();
        count_a > n - count_a
    }
}

Grid Problems

sol! {
    fn solution(n: usize, m: usize, grid: [[char]; n]) -> i32 {
        let mut count = 0;
        for i in 0..n {
            for j in 0..m {
                if grid[i][j] == '#' {
                    count += 1;
                }
            }
        }
        count
    }
}

Performance

The library is designed for competitive programming:

  • Buffered I/O for fast input/output
  • Pre-allocated string buffers
  • Zero-copy parsing where possible
  • Minimal runtime overhead

How to Use

  • Copy the library code to your solution file (or use as a dependency)
  • Replace your main function with sol! or sol_n! macro
  • Focus on solving the problem instead of parsing input!

Source Code

The complete library is available as a single file that you can copy-paste into your solutions. It's designed to be self-contained and dependency-free (except for std).

lib

template

Why Rust for CP?

  • Memory Safety: No segfaults or buffer overflows
  • Performance: As fast as C++ when optimized
  • Expressiveness: Powerful iterators and functional programming
  • Type Safety: Catch bugs at compile time
  • Great Tooling: Excellent error messages and debugging

The main barrier was always the verbose I/O, but this library solves that!

Feedback Welcome!

I'd love to hear your thoughts and suggestions! Have you tried Rust for competitive programming? What other features would make this library more useful? Let me know in the comments if you try it out!

Full text and comments »

  • Vote: I like it
  • +36
  • Vote: I do not like it

By AB22, history, 12 months ago, In English

Hello Codeforces community!

I'm excited to share a project I've been working on: CP-Assist, a desktop application designed to address the limitations of existing competitive programming tools.

Why I Built This

Unlike many existing tools that either support too few languages or are tightly coupled with specific text editors, I wanted to create something more flexible. As a programmer who values versatility, I found most existing CP tools too restrictive, so I built CP-Assist to work across different environments and with various languages.

Key Features

Editor Independence:

  • Works with any text editor or IDE of your choice

  • No lock-in to a specific development environment

  • Flexible workflow that adapts to your preferences

Extensive Language Support:

  • Wide range of programming languages supported

  • Customizable compiler flags and execution parameters

Pre-submission File Modification:

  • Unique Feature: Modify files before submission to include library files

  • Perfect for competitors who use personal algorithm libraries

  • Streamlines the process of preparing code for submission

Test Case Management:

  • Automatic import of problem statements and test cases via Competitive Companion

  • Organized interface for managing multiple test cases

  • Real-time verdict status for each test

  • Can add custom test cases

Code Execution:

  • Run solutions against test cases directly in the application

  • Track execution time and memory usage

  • Automatic comparison of expected vs. actual outputs

Tech Stack

I built CP-Assist using:

  • Rust with Tauri for performance-critical backend operations

  • React with TypeScript for a responsive UI

  • Mantine and Tailwind CSS for styling

Available For Download

You can check out CP-Assist at: https://github.com/tsych0/cp-assist

Available for: - Linux (AppImage, .deb, .rpm) - Windows (Installer)

Looking for Feedback

I'd appreciate hearing your thoughts on CP-Assist:

  • How does the flexibility compare to other tools you've used?

  • Which languages are you using with it?

  • Is the library inclusion feature helping with your workflow?

  • What other features would make this more useful for you?

I built this tool to address the limitations I experienced with other CP assistants, and I hope it can help improve your competitive programming experience as well!

GitHub: https://github.com/tsych0/cp-assist

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it