did day2 and day3

This commit is contained in:
attila 2018-12-03 20:25:55 +01:00
parent 5b6516f839
commit f5ab8bba97
6 changed files with 1398 additions and 6 deletions

@ -247,4 +247,4 @@ megsdweulhrfnkatfjoybxcbvq
mrgsdhpulmrinkatfjoyzxcbvq
megsdwkukhrinkatftoyzxcbvq
megsqwpulhrinfatfjoyzxebvq
megsdwpulhriskanfjoyzxctvq
megsdwpulhriskanfjoyzxctvq

@ -11,14 +11,23 @@ fn main() {
let lines: Vec<&str> = contents.split("\n").collect();
println!("{}", count_doubles_and_triples(lines));
println!("{}", count_doubles_and_triples(&lines));
let common_letters : String;
match find_common_letters(&lines){
Ok(s) => common_letters = s.into_iter().collect(),
Err(s) => common_letters = s.to_string(),
}
println!("{}", common_letters);
}
fn count_doubles_and_triples(lines: Vec<&str>) -> i32 {
fn count_doubles_and_triples(lines: &Vec<&str>) -> i32 {
let mut doubles = 0;
let mut triples = 0;
for line in lines{
for line in lines {
let mut counts = HashMap::new();
for character in line.chars() {
@ -34,9 +43,33 @@ fn count_doubles_and_triples(lines: Vec<&str>) -> i32 {
}
}
triples*doubles
triples * doubles
}
fn find_common_letters(lines: Vec<&str>) -> Vec<char>{
fn find_common_letters(lines: &Vec<&str>) -> Result<Vec<char> , std::io::Error> {
for line in lines {
for line2 in lines {
let mismatches = line.chars().zip(line2.chars()).filter(|&(a, b)| a != b).count();
if mismatches == 1 {
let result: Vec<(char,char)> = line.chars().zip(line2.chars()).filter(|&(a, b)| a == b).collect();
let candidate1: Vec<char> = line.chars().collect();
let candidate2: Vec<char> = line2.chars().collect();
let mut result: Vec<char> = Vec::new();
for x in 0..candidate1.len() {
if candidate1[x] == candidate2[x]{
result.push(candidate1[x]);
}
}
return Ok(test);
}
}
}
Err(std::io::Error::new(std::io::ErrorKind::Other, "No match found"))
}

4
day3/Cargo.lock generated Normal file

@ -0,0 +1,4 @@
[[package]]
name = "day3"
version = "0.1.0"

6
day3/Cargo.toml Normal file

@ -0,0 +1,6 @@
[package]
name = "day3"
version = "0.1.0"
authors = ["attila <daf276@gmx.de>"]
[dependencies]

1265
day3/input Normal file

File diff suppressed because it is too large Load Diff

84
day3/src/main.rs Normal file

@ -0,0 +1,84 @@
use std::fs::File;
use std::io::Read;
use std::cmp;
use std::collections::HashMap;
struct Rectangle {
id: i32,
x: i32,
y: i32,
width: i32,
height: i32,
}
fn main() {
let mut f = File::open("input").expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents).expect("something went wrong reading the file");
let lines = contents.lines().collect::<Vec<_>>();
let mut rectangles: Vec<Rectangle> = Vec::new();
for line in lines {
let r = line.split(|c| c == ' ' || c == '@' || c == ',' || c == ':' || c == 'x' || c == '#').filter_map(|c| c.parse::<i32>().ok()).collect::<Vec<_>>();
rectangles.push(Rectangle { id: r[0], x: r[1], y: r[2], width: r[3], height: r[4] });
}
println!("{}", intersecting_area(&rectangles));
let claim;
match not_intersecting_claim(&rectangles){
Ok(s) => claim = s.to_string(),
Err(s) => claim = s.to_string(),
}
println!("{}", claim);
}
fn intersecting_area(rectangles: &Vec<Rectangle>) -> i32 {
let mut intersects = HashMap::new();
for rectangle in rectangles {
for x in rectangle.x..rectangle.x + rectangle.width {
for y in rectangle.y..rectangle.y + rectangle.height {
*intersects.entry((x, y)).or_insert(0) += 1;
}
}
}
return intersects.values().filter(|i| **i > 1).count() as i32;
}
fn not_intersecting_claim(rectangles: &Vec<Rectangle>) -> Result<i32, std::io::Error>
{
for i in 0..rectangles.len() {
let mut found = true;
for j in 0..rectangles.len() {
if rectangles_intersecting(&rectangles[i], &rectangles[j]) && i != j {
found = false;
break;
}
}
if found {
return Ok(rectangles[i].id.clone());
}
}
return Err(std::io::Error::new(std::io::ErrorKind::Other, "No not intersecting claim found"))
}
fn rectangles_intersecting(r1: &Rectangle, r2: &Rectangle) -> bool {
let si = cmp::max(0, cmp::min(r1.x + r1.width, r2.x + r2.width) - cmp::max(r1.x, r2.x)) * cmp::max(0, cmp::min(r1.y + r1.height, r2.y + r2.height) - cmp::max(r1.y, r2.y));
if si > 0{
return true;
} else {
return false;
}
}