diff options
author | alyx <alyx@aleteoryx.me> | 2023-11-20 17:12:15 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-11-20 17:12:15 -0500 |
commit | ccf1a5828fc26a82545c7accf1ce7916daa08a2d (patch) | |
tree | 2428c1374e8f9bcbfd00c8090606f4781d083401 /2015/rs/code/eight/src/main.rs | |
parent | 4810e24657a5931ea79be3fc3ee3e108fe40282e (diff) | |
download | adventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.tar.gz adventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.tar.bz2 adventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.zip |
Reorganize using workspaces
Diffstat (limited to '2015/rs/code/eight/src/main.rs')
-rw-r--r-- | 2015/rs/code/eight/src/main.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2015/rs/code/eight/src/main.rs b/2015/rs/code/eight/src/main.rs new file mode 100644 index 0000000..b51f436 --- /dev/null +++ b/2015/rs/code/eight/src/main.rs @@ -0,0 +1,55 @@ +static INPUT: &'static str = include_str!("input.txt"); + +#[derive(Copy, Clone)] +enum ParseState { + Verbatim, + Esc, + Hex1, + Hex2(u32) +} + +fn main() { + let valueified = INPUT.split('\n') + .filter(|s| !s.is_empty()); + + let (oglen, parsedlen) = valueified.clone() + .map(|s| { + let rs = s.strip_prefix('"').and_then(|s| s.strip_suffix('"')).unwrap(); + let mut buf = String::with_capacity(s.len()); + let mut state = ParseState::Verbatim; + for c in rs.chars() { + match (state, c) { + (ParseState::Verbatim, '\\') => { state = ParseState::Esc; }, + (ParseState::Verbatim, c) => { buf.push(c); }, + + (ParseState::Esc, 'x') => { state = ParseState::Hex1; }, + (ParseState::Esc, c) => { buf.push(c); state = ParseState::Verbatim; }, + + (ParseState::Hex1, c) => { state = ParseState::Hex2(c.to_digit(16).unwrap() * 16); }, + (ParseState::Hex2(n), c) => { buf.push(/*char::from_u32(n + c.to_digit(16).unwrap()).unwrap()*/'?'); state = ParseState::Verbatim; }, + } + } + (s, buf) + }) + .fold((0, 0), |(a1, b1), (a2, b2)| (a1 + a2.len(), b1 + b2.len())); + let lendiff = oglen - parsedlen; + println!("Literal Length - Parsed Length: {lendiff}"); + + let (oglen, enclen) = valueified + .map(|s| { + let mut buf = String::with_capacity(s.len() * 2); + buf.push('"'); + for c in s.chars() { + match c { + '\\' => { buf += "\\\\" }, + '"' => { buf += "\\\"" }, + c => { buf.push(c) }, + } + } + buf.push('"'); + (s, buf) + }) + .fold((0, 0), |(a1, b1), (a2, b2)| (a1 + a2.len(), b1 + b2.len())); + let lendiff = enclen - oglen; + println!("Encoded Length - Literal Length: {lendiff}"); +} |