Commit cc36fb9d authored by Jonathan Klimt's avatar Jonathan Klimt

Solved day5

parent cb344df7
[[package]]
name = "day5"
version = "0.1.0"
dependencies = [
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_syscall"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
"checksum redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "a84bcd297b87a545980a2d25a0beb72a1f490c31f0a9fde52fca35bfbb1ceb70"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
[package]
name = "day5"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
edition = "2018"
[dependencies]
termion = "1.5"
This diff is collapsed.
extern crate termion;
use self::termion::color;
// use std::io::BufRead;
// use std::io::BufReader;
use std::io::Error;
use std::fs::File;
// use std::io;
use std::io::prelude::*;
#[derive(Default, Debug, Clone, PartialEq)]
struct Claim {
id: usize,
}
fn main() -> Result<(), Error> {
let mut f = File::open("input.txt")?;
let mut s : String = String::new();
f.read_to_string(&mut s).unwrap();
println!("last cars: {}", s.chars().skip(s.len() - 40).collect::<String>() );
while let Some(tmp) = react_once(&mut s) {
s = tmp;
}
println!("last cars: {}", s.chars().skip(s.len() - 20).collect::<String>() );
println!("Length of fully reacted polymer: {}", s.len());
let mut shortest_len = s.len();
for c in (0..26).map(|x| (x + 'a' as u8) as char){
let mut new_s : String = s.chars().filter(|x| x.to_lowercase().next().unwrap() != c ).collect();
while let Some(tmp) = react_once(&mut new_s) {
new_s = tmp;
// println!("new str: {} len: {}", s.chars().take(40).collect::<String>(), s.len());
}
if new_s.len() < shortest_len{
shortest_len = new_s.len();
}
println!("without {}: len: {}", c, new_s.len());
}
println!("shortest length: {}", shortest_len);
Ok(())
}
fn react_once (s: &String) -> Option<(String)>{
let mut new_s = String::new();
let mut changes = 0;
let mut c_iter = s.chars().peekable();
while let Some(c) = c_iter.next() {
if let Some(next) = c_iter.peek() {
if ! next.is_alphanumeric(){
new_s.push(c);
break; // probably eol
}
if (c.is_lowercase() && next.is_uppercase() && c == next.to_lowercase().next()?) ||
(c.is_uppercase() && next.is_lowercase() && c == next.to_uppercase().next()?){
c_iter.next();
changes += 1;
continue;
}
}
new_s.push(c);
}
// new_s.push(c_iter);
if changes == 0 {
None
} else {
Some(new_s)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn simple1(){
let mut s = "abcCde".to_string();
let correct = "abde".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple2(){
let mut s = "abcCcde".to_string();
let correct = "abcde".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple3(){
let mut s = "abcCcCde".to_string();
let correct = "abde".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple4(){
let mut s = "abcCxcCde".to_string();
let correct = "abxde".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple5(){
let mut s = "abcC".to_string();
let correct = "ab".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple6(){
let mut s = "abCc".to_string();
let correct = "ab".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple7(){
let mut s = "abCCc".to_string();
let correct = "abC".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn simple8(){
let mut s = "abCcB".to_string();
let correct = "abB".to_string();
assert_eq!(react_once(&mut s).unwrap() , correct);
}
#[test]
fn no_change1(){
let mut s = "abcc".to_string();
assert!(react_once(&mut s).is_none());
}
#[test]
fn no_change2(){
let mut s = "aBcc".to_string();
assert!(react_once(&mut s).is_none());
}
#[test]
fn loop1(){
let mut s = "abCcB".to_string();
let correct = "a".to_string();
while let Some(tmp) = react_once(&mut s) {
s = tmp;
println!("new str: {}", s);
}
assert_eq!(s , correct);
}
#[test]
fn loop2(){
let mut s = "abCcBAsdf".to_string();
let correct = "sdf".to_string();
while let Some(tmp) = react_once(&mut s) {
s = tmp;
println!("new str: {}", s);
}
assert_eq!(s , correct);
}
#[test]
fn real1(){
let mut s = "dabAcCaCBAcCcaDA".to_string();
let correct = "dabCBAcaDA".to_string();
while let Some(tmp) = react_once(&mut s) {
s = tmp;
println!("new str: {}", s);
}
assert_eq!(s , correct);
assert_eq!(s.len(), 10);
}
#[test]
fn real2(){
let mut s = "JzZlqQLvVjO".to_string();
let correct = "O".to_string();
while let Some(tmp) = react_once(&mut s) {
s = tmp;
println!("new str: {}", s);
}
assert_eq!(s , correct);
assert_eq!(s.len(), 1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment