Skip to content
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
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so

# Virtual environments
.venv/
venv/
env/

# Testing & Coverage
.pytest_cache/
.coverage
htmlcov/
.tox/
.cache

# Distribution / packaging
build/
dist/
*.egg-info/

# IDE & Editors
.vscode/
!.vscode/launch.json
.idea/
*.sublime-project
*.sublime-workspace

# OS generated files
.DS_Store
Thumbs.db
*~

# Project-specific
kattis_problems/
*.kattisrc
.kattisrc
testing

# Logs and temporary files
*.log
*.tmp
*.temp
*.swp
*.swo

# Backup files
*.bak
*.backup
*.old
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Chris MacDonald
Copyright (c) 2025 Christopher MacDonald

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
290 changes: 229 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,230 @@
# Kattis Grind
A small easy-to-use package of Python 3 scripts which can help speed up the process for competitive programming on [Kattis](https://open.kattis.com). The scripts will download a local copy of the question (html file), and can automate testing inputs / outputs.

# Requirements
Kattis Grind uses Python 3. To check if you have it:
```console
foo@bar:~$ python3 --version
Python 3.7.3
```
If you don't have Python 3, you can install it from the [Python Website](https://www.python.org/downloads/).

The Kattis Grind setup requires two modules. To install them:
```console
foo@bar:~$ pip3 install bs4
foo@bar:~$ pip3 install fake-useragent
```
# How do I use it?
## Fetch a question!
Simple! If you wanted to fetch a question, you can simply run
```console
foo@bar:~/kattis-grind$ ./fetch.py
Enter ID:
```
If you prefer to use command line arguments instead, you can! For example, if I wanted to get question 'hello':
```console
foo@bar:~/kattis-grind$ ./fetch.py --id hello
```

## Test your solution!
The useful feature for Kattis Grind is that you can easily test your solutions! To do so:
```console
foo@bar:~/kattis-grind$ ./test.py
Enter ID:
```
Similarly to fetching a question, you can pass in an optional ID argument! Using 'hello' as an example again:
```console
foo@bar:~/kattis-grind$ ./test.py --id hello
```
## Generating random questions!
The best feature of this program is that you can fetch random questions from Kattis too! If I was interested in five questions between a range of 1.4 to 1.6, I can run the script like this:
```console
foo@bar:~/kattis-grind$ ./rand.py
Enter lower bound: 1.4
Enter upper bound: 1.6
How many questions: 5
```
It's as simple as that! But as you may have guessed, there are optional command line arguments for these too! Although they're less useful, you can use them like this:
```console
foo@bar:~/kattis-grind$ ./rand.py --lobound 1.4 --upbound 1.6 --qamount 5
```
Depending on how many questions you want, and what range they're between, this operation *may* take several seconds (sometimes a good 10 - 40 seconds!!). Keep in mind that while it takes long, it definitely works!

# Current Bugs
Currently, Windows seems to have some trouble with encoding characters and Python crashes as a result of trying to save HTML files without proper encoding. This does not occur on Linux. Be wary of questions with foreign characters in them, for example: ő

# License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details

# Acknowledgments
* Thanks to [JarateKing](https://github.com/JarateKing) and [Ben Boyle](https://github.com/benbdevd) for the original grinding idea.
* Thanks to [Will Taylor](https://github.com/wtaylor17) for fixing test.py to work on Windows machines.
* Thanks to the UPEI SMCSS for all the motivation!!!

A modern CLI tool for competitive programming on [Kattis](https://open.kattis.com). This tool helps streamline your competitive programming workflow by automating problem fetching, solution testing, and submission processes.

## Features

- **Fetch Problems**: Download problem statements and sample test cases
- **Submit Solutions**: Submit your code directly from the command line with real-time feedback
- **Random Problem Discovery**: Find random problems within specified difficulty ranges
- **Multi-language Support**: Supports C++, Python, Java, C#, Go, Rust, and many more
- **Template System**: Automatic boilerplate code generation for your preferred languages
- **Problem Hints**: Optional integration with Steve Halim's cpbook.net for problem type hints and solving strategies

## Installation

### Option 1: Install directly from GitHub

```bash
pip install git+https://github.com/chrismacdonaldw/kattis-grind.git
```

This installs the `kattis-grind` command globally, allowing you to use it from anywhere.

### Option 2: Local development setup

1. Clone the repository:
```bash
git clone https://github.com/chrismacdonaldw/kattis-grind.git
cd kattis-grind
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

## Quick Start

### If installed via pip (Option 1):

1. **Initialize Configuration**:
```bash
kattis-grind config init
```

2. **Edit your configuration** with your Kattis credentials:
- Download your `.kattisrc` file from [Kattis](https://open.kattis.com/download/kattisrc)
- Place it in your home directory or edit the generated one

3. **Fetch a problem**:
```bash
kattis-grind fetch hello
```

4. **Submit a solution**:
```bash
kattis-grind submit solution.py
```

### If running locally (Option 2):

1. **Initialize Configuration**:
```bash
python main.py config init
```

2. **Edit your configuration** with your Kattis credentials:
- Download your `.kattisrc` file from [Kattis](https://open.kattis.com/download/kattisrc)
- Place it in your home directory or edit the generated one

3. **Fetch a problem**:
```bash
python main.py fetch hello
```

4. **Submit a solution**:
```bash
python main.py submit solution.py
```

## Commands

*Note: Replace `kattis-grind` with `python main.py` if running locally instead of installing via pip.*

### `fetch <problem_id>`
Downloads a problem and sets up a directory with:
- Problem description (HTML file)
- Sample input/output files
- Boilerplate code templates for configured languages

```bash
kattis-grind fetch hello
kattis-grind fetch twostones
```

### `submit <files...>`
Submits your solution to Kattis with real-time feedback.

```bash
# Submit a single file
kattis-grind submit solution.py

# Submit multiple files
kattis-grind submit main.cpp helper.h

# Specify problem and language explicitly
kattis-grind submit solution.java --problem hello --language Java
```

**Options:**
- `--problem, -p`: Specify the problem ID (auto-detected from filename if not provided)
- `--language, -l`: Specify the programming language (auto-detected from extension if not provided)
- `--mainclass, -m`: Specify the main class name (for languages that require it)
- `--force, -f`: Skip confirmation prompt

### `rand`
Finds a random problem within your configured difficulty range.

```bash
# Find a random problem
kattis-grind rand

# Specify difficulty range
kattis-grind rand --min 1.5 --max 2.0

# Fetch immediately
kattis-grind rand --fetch

# Output only the problem ID
kattis-grind rand --id-only
```

**Options:**
- `--min`: Minimum difficulty (uses config default if not specified)
- `--max`: Maximum difficulty (uses config default if not specified)
- `--fetch, -f`: Automatically fetch the problem
- `--id-only, -i`: Output only the problem ID (useful for scripting)

### `config init`
Creates a default configuration file in your home directory.

```bash
kattis-grind config init
```

## Configuration

The tool uses a `.kattisrc` configuration file, which should be placed in your home directory. You should use the `templates/default_kattisrc` which can be generated with the `config init` command shown above.

### Configuration File Structure

```ini
[user]
# Found at https://<hostname>/downloads/kattisrc
username = YOUR_USERNAME_HERE
token = YOUR_TOKEN_HERE

[kattis]
hostname = open.kattis.com
loginurl = https://open.kattis.com/login
submissionurl = https://open.kattis.com/submit

[settings]
# Preferred programming languages (comma-separated)
languages = python,cpp
# Difficulty range for random problem selection (min-max)
difficulty = 1.5-3.0
# Directory where problems will be saved (supports ~ for home directory)
problems_directory = kattis_problems
# Whether to fetch hints from Steve Halim's cpbook.net site
include_hints = false

[templates]
# Template Examples:
# cpp = ~/.kattis-grind/templates/cpp_template.cpp
# py = ~/.kattis-grind/templates/py_template.py
```

### Configuration Options

#### Custom Problems Directory
You can configure where problems are saved using the `problems_directory` setting:

```ini
[settings]
problems_directory = kattis_problems

# Or using ~ as a root:
problems_directory = ~/competitive_programming/kattis
```

#### Problem Hints
Enable hints from Steve Halim's [cpbook.net](https://cpbook.net) to get problem type information and solving strategies:

```ini
[settings]
include_hints = true
```

When enabled, the tool will display hints like:
- **Problem Type**: Classification (e.g., "1.4a, One-Liner I/O")
- **Hint Text**: Solving strategy (e.g., "just print 'Hello World!'")

### Environment Variables

- `KATTIS_GRIND_HOME`: Override the default configuration directory

## Project Structure

After fetching problems, your directory structure will look like:

```
kattis_problems/
├── hello/
│ ├── hello.html # Problem description
│ ├── sample-1.in # Sample input
│ ├── sample-1.ans # Expected output
│ ├── hello.py # Python template
│ └── hello.cpp # C++ template
└── twostones/
├── twostones.html
├── sample-1.in
├── sample-1.ans
├── twostones.py
└── twostones.cpp
```

## Acknowledgments

This project uses the [official Kattis CLI](https://github.com/Kattis/kattis-cli) as a starting point. The original Kattis CLI provided the foundation for understanding the Kattis API and submission process.
Loading