add problem 05

This commit is contained in:
timeshifter
2026-07-12 19:59:04 +02:00
parent 518d68a1d3
commit 9ba67af68a
3 changed files with 364 additions and 0 deletions
+225
View File
@@ -0,0 +1,225 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anyhow"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
[[package]]
name = "bitflags"
version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8"
[[package]]
name = "bytes"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "errno"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "lock_api"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
"scopeguard",
]
[[package]]
name = "mio"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02bd0af71c67b473010cbbc60715ee815645a4dc942899111f494b4b737d6fda"
dependencies = [
"libc",
"wasi",
"windows-sys",
]
[[package]]
name = "parking_lot"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-link",
]
[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "problem05"
version = "0.1.0"
dependencies = [
"anyhow",
"tokio",
]
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
dependencies = [
"bitflags",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
"errno",
"libc",
]
[[package]]
name = "smallvec"
version = "1.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"
[[package]]
name = "socket2"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "syn"
version = "2.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tokio"
version = "1.52.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe"
dependencies = [
"bytes",
"libc",
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
]
[[package]]
name = "tokio-macros"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "problem05"
version = "0.1.0"
edition = "2024"
[dependencies]
tokio = { version = "1", features = ["full"] }
anyhow = "1"
+131
View File
@@ -0,0 +1,131 @@
use anyhow::bail;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt, Result},
net::TcpStream,
};
const TONYS_ADDRESS: &str = "7YWHMfk9JZe0LM0g1ZauHuiSxhI";
#[tokio::main]
async fn main() -> Result<()> {
let listener = tokio::net::TcpListener::bind("0.0.0.0:10000").await?;
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move {
match handle_connection(socket).await {
Ok(_) => (),
Err(err) => {
dbg!(err);
}
}
});
}
}
async fn handle_connection(socket: TcpStream) -> anyhow::Result<()> {
let (mut downstream_reader, mut downstream_writer) = tokio::io::split(socket);
let upstream = tokio::net::TcpStream::connect("chat.protohackers.com:16963").await?;
let (mut upstream_reader, mut upstream_writer) = tokio::io::split(upstream);
let mut downstream_read_buffer = Vec::with_capacity(1024);
let mut upstream_read_buffer = Vec::with_capacity(1024);
loop {
tokio::select! {
read_amount = read_to_buffer(&mut upstream_reader, &mut upstream_read_buffer) => {
match read_amount {
Ok(_) => (),
Err(_) => {
return Ok(());
}
};
let message = match handle_message(&mut upstream_read_buffer) {
Ok(s) => s,
Err(_) => continue,
};
downstream_writer.write_all(&message.into_bytes()).await?;
}
// receive from downstream, send to upstream
read_amount = read_to_buffer(&mut downstream_reader, &mut downstream_read_buffer) => {
match read_amount {
Ok(_) => (),
Err(_) => {
return Ok(());
}
};
let message = match handle_message(&mut downstream_read_buffer) {
Ok(s) => s,
Err(_) => continue,
};
upstream_writer.write_all(&message.into_bytes()).await?;
}
};
}
}
fn find_next_newline(read_buffer: &[u8]) -> Option<usize> {
read_buffer.iter().position(|c| *c == b'\n')
}
async fn read_to_buffer(
reader: &mut tokio::io::ReadHalf<TcpStream>,
read_buffer: &mut Vec<u8>,
) -> anyhow::Result<usize> {
let mut read_amount = 0;
while !read_buffer.contains(&b'\n') {
read_amount += reader.read_buf(read_buffer).await?;
if read_amount == 0 {
bail!("user disconnected");
}
}
Ok(read_amount)
}
fn replace_boguscoin_addresses(s: String) -> String {
let trimmed = s.trim_end_matches('\n');
let mut result = String::new();
let mut words = trimmed.split(' ').peekable();
while let Some(word) = words.next() {
if is_boguscoin_addres(word) {
result.push_str(TONYS_ADDRESS);
} else {
result.push_str(word);
}
if words.peek().is_some() {
result.push(' ');
}
}
result.push('\n');
result
}
fn is_boguscoin_addres(s: &str) -> bool {
s.starts_with('7')
&& (26..=35).contains(&s.len())
&& s.chars().all(|c| c.is_ascii_alphanumeric())
}
fn handle_message(read_buffer: &mut Vec<u8>) -> anyhow::Result<String> {
let newline = match find_next_newline(read_buffer) {
Some(idx) => idx,
None => bail!("continue"),
};
let before_newline = read_buffer.drain(0..=newline).collect();
let message = String::from_utf8(before_newline)?;
let message = replace_boguscoin_addresses(message);
Ok(message)
}