2018/03
This commit is contained in:
95
2018/03/Cargo.lock
generated
Normal file
95
2018/03/Cargo.lock
generated
Normal file
@@ -0,0 +1,95 @@
|
||||
[[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 = "day-03"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex 1.1.0 (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 = "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 = "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 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 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"
|
||||
8
2018/03/Cargo.toml
Normal file
8
2018/03/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day-03"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.1"
|
||||
1229
2018/03/input.txt
Normal file
1229
2018/03/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
112
2018/03/src/main.rs
Normal file
112
2018/03/src/main.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use std::collections::HashSet;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
struct Claim {
|
||||
id: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let re = Regex::new(r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)").expect("failed to build regex");
|
||||
|
||||
let claims = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.map(|line| {
|
||||
let caps = re
|
||||
.captures(&line)
|
||||
.expect("faild to capture input from line");
|
||||
|
||||
Claim {
|
||||
id: caps
|
||||
.get(1)
|
||||
.expect("faild to find id")
|
||||
.as_str()
|
||||
.parse::<usize>()
|
||||
.expect("faild to parse id"),
|
||||
x: caps
|
||||
.get(2)
|
||||
.expect("failed to find x")
|
||||
.as_str()
|
||||
.parse::<usize>()
|
||||
.expect("faild to parse x"),
|
||||
y: caps
|
||||
.get(3)
|
||||
.expect("failed to find y")
|
||||
.as_str()
|
||||
.parse::<usize>()
|
||||
.expect("faild to parse y"),
|
||||
width: caps
|
||||
.get(4)
|
||||
.expect("failed to find width")
|
||||
.as_str()
|
||||
.parse::<usize>()
|
||||
.expect("faild to parse width"),
|
||||
height: caps
|
||||
.get(5)
|
||||
.expect("failed to find height")
|
||||
.as_str()
|
||||
.parse::<usize>()
|
||||
.expect("faild to parse height"),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let width = claims
|
||||
.iter()
|
||||
.map(|claim| claim.x + claim.width)
|
||||
.max()
|
||||
.expect("failed to find fabric width");
|
||||
|
||||
let height = claims
|
||||
.iter()
|
||||
.map(|claim| claim.y + claim.height)
|
||||
.max()
|
||||
.expect("failed to find fabric height");
|
||||
|
||||
let mut fabric = vec![HashSet::new(); width * height].into_boxed_slice();
|
||||
let mut not_overlapping = HashSet::new();
|
||||
|
||||
for claim in &claims {
|
||||
let mut success = true;
|
||||
|
||||
for d_y in claim.y..(claim.y + claim.height) {
|
||||
let y = d_y * width;
|
||||
|
||||
for x in claim.x..(claim.x + claim.width) {
|
||||
let patch = &mut fabric[y + x];
|
||||
|
||||
if !patch.is_empty() {
|
||||
success = false;
|
||||
|
||||
for id in patch.iter() {
|
||||
not_overlapping.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
patch.insert(claim.id);
|
||||
}
|
||||
}
|
||||
|
||||
if success {
|
||||
not_overlapping.insert(claim.id);
|
||||
}
|
||||
}
|
||||
|
||||
let overlaping = fabric.iter().filter(|patch| patch.len() > 1).count();
|
||||
|
||||
println!("part.one={}", overlaping);
|
||||
|
||||
let not_overlapping = not_overlapping
|
||||
.into_iter()
|
||||
.next()
|
||||
.expect("failed to get first non overlapping claim");
|
||||
|
||||
println!("part.two={}", not_overlapping);
|
||||
}
|
||||
Reference in New Issue
Block a user