...
 
Commits (2)
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")?;
......@@ -25,14 +14,12 @@ fn main() -> Result<(), Error> {
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();
......@@ -62,7 +49,6 @@ fn react_once (s: &String) -> Option<(String)>{
}
new_s.push(c);
}
// new_s.push(c_iter);
if changes == 0 {
None
} else {
......
dashboard
s
run
b 45
break main
run
break 55
break line55
s
s
s
s
s
s
q
break main
r
s
s
n
n
n
n
n
n
list
b grow_field
b grow_field
dashboard
break d
break day6::grow_field
r
ls
b main
b grow_field
b day6::grow_field
help rbreak
rb grow_field
r
s
s
c
s
list
n
n
n
n
q
rb main
r
info locals
n
info locals
n
n
n
info locals
n
b 56
c
c
break 56
n
n
n
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
n
info locals
n
info x y
info x
c
show x
print x
print y
c
c
c
c
c
c
print y
20 n
20n
n
print y
n
print y
c
c
c
c
c
c
c
c
c
c
c
c
c
c
print n
print x
print x
n
print x
print x y
print x; print y
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout source !assembly stack
dashboard -layout default
dashboard -layout !assembly
n
dashboard -layout stack break
dashboard -layout stack history source
dashboard -layout stack history source expressions
n
n
c
n
b 57
delete 56
dashboard -layout stack history source expressions breakpoints
b
break panic
rb panic
r
list
info break
info stack
b
b
b
b
r
b 56
info break
del
b 58
del
b main
r
b 56
del
b main
r
list
rb main
del
file
b main
r
c
b main.rs:56
info b
r
r
c
dashboard -layout source expressions stack threads history
c
b 58
d 2
c
c
r
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day6"
version = "0.1.0"
dependencies = [
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "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 = "memchr"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (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 = "regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (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)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
"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 regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
"checksum regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a2ed29da7a9e1960e1639e7a982e6edc6d49be308a3b02daf511504a16d1"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[package]
name = "day6"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
edition = "2018"
[dependencies]
regex = "1"
termion = "1.5.1"
305, 349
315, 193
154, 62
246, 310
145, 283
260, 324
342, 79
321, 353
40, 242
351, 353
337, 297
174, 194
251, 160
314, 195
114, 81
204, 246
203, 169
203, 296
60, 276
201, 47
206, 96
243, 46
295, 304
319, 80
213, 330
337, 255
40, 262
302, 150
147, 349
317, 240
96, 315
133, 305
320, 348
210, 300
266, 216
223, 319
207, 152
127, 214
312, 245
49, 329
211, 84
129, 276
247, 143
208, 235
271, 126
124, 211
144, 184
54, 88
354, 300
148, 85
15, 3
05, 49
54, 2
46, 15
45, 3
6, 24
42, 9
21, 5
30, 30
44, 52
extern crate regex;
use regex::Regex;
extern crate termion;
use termion::color;
use termion::clear;
use std::io::Error;
use std::fs::File;
// use std::io::BufRead;
use std::io::BufReader;
use std::thread;
use std::time::Duration;
use std::io::prelude::*;
use std::collections::HashMap;
fn main() -> Result<(), Error> {
let f = File::open("input.txt")?;
let bufread = BufReader::new(f);
let re = Regex::new(r"(\d+), (\d+)").unwrap();
let mut points : Vec<(usize, usize)> = Vec::new();
for line in bufread.lines() {
let l = line?;
if let Some(caps) = re.captures(&l) {
let p : (usize, usize) = (caps.get(1).unwrap().as_str().parse::<usize>().unwrap(),
caps.get(2).unwrap().as_str().parse::<usize>().unwrap());
points.push(p.clone());
}
}
let max_x : usize = points.clone().into_iter().map(|(x,_)| x).max().unwrap();
let max_y : usize = points.clone().into_iter().map(|(_,y)| y).max().unwrap();
println!("max x {} max y {}", max_x, max_y);
// let mut field : [[Option<usize>; max_x]; max_y] = [[None; max_x]; max_y];
// let mut field : Vec<Vec<Option<usize>>> = Vec::new();
let mut field : Vec<Vec<Option<usize>>> = vec![vec![None; max_y + 1]; max_x +1];
for i in points.into_iter().enumerate(){
println!("point: {} {:?}", i.0, i.1);
field[(i.1).0][(i.1).1] = Some(i.0);
}
print_field(&field);
// let new_field = grow_field(&field).unwrap();
while let Some(tmp) = grow_field(&field){
field = tmp;
print_field(&field);
thread::sleep(Duration::from_millis(100));
}
println!{""};
print_field(&field);
let mut remove_vec : Vec<usize> = Vec::new();
for x in 0..max_x {
if let Some(i) = field[x][0] {
if !remove_vec.contains(&i) {
remove_vec.push(i);
}
}
if let Some(i) = field[x][field[0].len()-1] {
if !remove_vec.contains(&i) {
remove_vec.push(i);
}
}
}
for y in 0..max_y {
if let Some(i) = field[0][y] {
if !remove_vec.contains(&i) {
remove_vec.push(i);
}
}
if let Some(i) = field[field.len()-1][y] {
if !remove_vec.contains(&i) {
remove_vec.push(i);
}
}
}
println!("remove_vec: {:?}", remove_vec);
let mut area_count : HashMap<usize, usize> = HashMap::new();
for x in 0..max_x {
for y in 0..max_y {
if let Some(i) = field[x][y] {
if ! remove_vec.contains(&i){
if let Some(count) = area_count.get_mut(&i) {
*count = *count + 1;
} else {
area_count.insert(i, 1);
}
}
}
}
}
println!("count vec: {:?}, \nMaximum: {}", area_count, area_count.iter().map(|(k, v)| v).max().unwrap());
Ok(())
}
fn grow_field (f: &Vec<Vec<Option<usize>>>) -> Option<Vec<Vec<Option<usize>>>> {
let (max_x, max_y) = (f.len(), f[0].len());
let mut new_field : Vec<Vec<Option<usize>>> = vec![vec![None; max_y]; max_x];
let mut changes : usize = 0;
for x in 0..max_x {
for y in 0..max_y {
if let Some(i) = f[x][y]{
new_field[x][y] = Some(i);
continue;
}
let mut cand : Vec<usize> = Vec::new();
if x > 0 {
if let Some(c) = f[x-1][y] {
cand.push(c);
}
}
if x < max_x -1 {
if let Some(c) = f[x+1][y] {
if !cand.contains(&c) {
cand.push(c);
}
}
}
if y > 0 {
if let Some(c) = f[x][y-1] {
if !cand.contains(&c) {
cand.push(c);
}
}
}
if y < max_y -1 {
if let Some(c) = f[x][y+1] {
if !cand.contains(&c) {
cand.push(c);
}
}
}
if cand.len() == 1 {
new_field[x][y] = cand.pop();
changes = changes + 1;
}
}
}
if changes > 0 {
Some(new_field)
}
else {
None
}
}
fn print_field(f: &Vec<Vec<Option<usize>>>) {
print!("{}", clear::All);
let mut max_x = 120;
if f.len() < max_x {
max_x = f.len()
}
let mut max_y = 70;
if f[0].len() < max_y {
max_y = f[0].len()
}
for l in 0..max_y {
for c in 0..max_x {
if let Some(val) = f[c][l]{
match val % 8 {
0 => print!("{}", color::Fg(color::Red)),
1 => print!("{}", color::Fg(color::Blue)),
2 => print!("{}", color::Fg(color::Cyan)),
3 => print!("{}", color::Fg(color::Green)),
4 => print!("{}", color::Fg(color::Magenta)),
5 => print!("{}", color::Fg(color::White)),
6 => print!("{}", color::Fg(color::Yellow)),
7 => print!("{}", color::Fg(color::Black)),
_ => print!("{}", color::Fg(color::Black)),
}
print!("{}{} ", val%10, color::Fg(color::Reset));
}
else {
print!("{}.{} ", color::Fg(color::LightCyan), color::Fg(color::Reset));
}
}
println!("");
}
}
#[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);
}
}