Commit 611c84a0 authored by Jonathan Klimt's avatar Jonathan Klimt

Solved day3

parent e2cd58a4
/target
**/*.rs.bk
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.1 (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 = "day3"
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.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.1"
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.44 (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.43"
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.43 (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.1 (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.44 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.43 (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.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
"checksum redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "679da7508e9a6390aeaf7fbd02a800fdc64b73fe2204dd2c8ae66d22d9d5ad5d"
"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 = "day3"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
[dependencies]
regex = "1"
termion = "1.5"
This diff is collapsed.
#1 @ 8,4: 2x4
#2 @ 6,7: 3x6
#3 @ 5,5: 7x5
extern crate regex;
extern crate termion;
use self::termion::color;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Error;
use regex::Regex;
#[derive(Default, Debug, Clone, PartialEq)]
struct Claim {
id: usize,
x: usize,
y: usize,
w: usize,
h: usize
}
type Fabric = [[usize ; 1000] ; 1000];
fn main() -> Result<(), Error> {
let file = File::open("input.txt")?;
let mut f : Fabric = [[0; 1000]; 1000];
let claim_vec = parse_claims(&file)?;
// println!("claim_vec: {:?}", claim_vec);
for c in claim_vec.clone() {
apply_claim(c, &mut f);
}
print_fabric(&f);
println!{"Overlapping parts: {}", count_overlap(&f)?};
let c = search_valid_claim(&f, &claim_vec)?;
println!("valid claim: {} at x: {} y: {}", c.id, c.x, c.y );
Ok(())
}
fn parse_claims(f: &File) -> Result<Vec<Claim>, Error> {
let bufread = BufReader::new(f);
let mut claim_vec : Vec<Claim> = Vec::new();
let re = Regex::new(r"#(\d+) @ (\d{1,4}),(\d{1,4}): (\d{1,4})x(\d{1,4})").unwrap();
for line in bufread.lines() {
let l = line?;
let caps = re.captures(&l).unwrap();
let c = Claim{
id: caps.get(1).unwrap().as_str().parse().unwrap(),
x: caps.get(2).unwrap().as_str().parse().unwrap(),
y: caps.get(3).unwrap().as_str().parse().unwrap(),
w: caps.get(4).unwrap().as_str().parse().unwrap(),
h: caps.get(5).unwrap().as_str().parse().unwrap()
};
// println!("line: {}, caps: {:?}", l, c);
claim_vec.push(c);
}
Ok(claim_vec)
}
fn apply_claim(c : Claim, f : &mut Fabric){
// println!("{:?}", c);
for i in c.x..(c.x+c.w) {
for j in c.y..(c.y+c.h) {
f[i][j] += 1;
}
}
}
fn claim_is_free(c : &Claim, f : &Fabric) -> bool {
for i in c.x..(c.x+c.w) {
for j in c.y..(c.y+c.h) {
if f[i][j] > 1 {
return false;
}
}
}
true
}
fn print_fabric(f: &Fabric) {
for l in 0..40 {
for c in 0..40 {
let x = f[l][c];
if x == 0 {
print!("{}.{} ", color::Fg(color::LightCyan), color::Fg(color::Reset));
}
else if x == 1 {
print!("# ");
}
else {
print!("{}{}{} ", color::Fg(color::Red), x, color::Fg(color::Reset));
}
}
println!("");
}
}
fn count_overlap(f: &Fabric) -> Result<usize, Error> {
let mut i : usize = 0;
for l in f.iter() {
for c in l.iter() {
if *c > 1 {
i += 1;
}
}
}
Ok(i)
}
fn search_valid_claim(f: &Fabric, v : &Vec<Claim>) -> Result<Claim, Error> {
for c in v {
if claim_is_free(c, f) {
return Ok(c.clone());
}
}
Err(std::io::Error::new(std::io::ErrorKind::Other, "No claim Found"))
}
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