diff --git a/.gitignore b/.gitignore index 9da658f..9eaf061 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ */input -*/part1 -*/part2 + diff --git a/03/part1/Cargo.lock b/03/part1/Cargo.lock new file mode 100644 index 0000000..ea3233f --- /dev/null +++ b/03/part1/Cargo.lock @@ -0,0 +1,104 @@ +[[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 = "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 = "part1" +version = "0.1.0" +dependencies = [ + "scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.2.11" +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.5.6 (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.5.6" +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 = "scan_fmt" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.2.11 (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 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" +"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" +"checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30" +"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" diff --git a/03/part1/Cargo.toml b/03/part1/Cargo.toml new file mode 100644 index 0000000..3cd8cd0 --- /dev/null +++ b/03/part1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "part1" +version = "0.1.0" +authors = ["Nick Thomas "] +edition = "2018" + +[dependencies] +scan_fmt = "0.1.3" diff --git a/03/part1/src/main.rs b/03/part1/src/main.rs new file mode 100644 index 0000000..1b0a11c --- /dev/null +++ b/03/part1/src/main.rs @@ -0,0 +1,98 @@ +#[macro_use] extern crate scan_fmt; + +use std::fs::File; +use std::io::BufReader; +use std::io::prelude::*; + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Point { + x: usize, + y: usize, +} + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Rect { + a: Point, + b: Point, +} + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Claim { + id: u32, + area: Rect, +} + +impl Claim { + fn new(from: &str) -> Claim { + let (id,x,y,w,h) = scan_fmt!( + from, + "#{} @ {},{}: {}x{}", + u32, usize, usize, usize, usize + ); + + return Claim{ + id: id.unwrap(), + area: Rect { + a: Point {x: x.unwrap(), y: y.unwrap()}, + b: Point {x: x.unwrap()+w.unwrap(), y: y.unwrap()+h.unwrap()}, + }, + } + } +} + +fn main() -> std::io::Result<()> { + let f = File::open("../input")?; + let reader = BufReader::new(f); + + let mut claims: Vec = Vec::new(); + let mut max_x = 0; + let mut max_y = 0; + + for line in reader.lines() { + let claim = Claim::new(&line.unwrap()); + + if claim.area.b.x > max_x { + max_x = claim.area.b.x; + } + + if claim.area.b.y > max_y { + max_y = claim.area.b.y; + } + + claims.push(claim); + } + + println!("{} {}", max_x, max_y); + + let mut field: Vec> = Vec::new(); + for i in 0..max_y { + let mut x_row: Vec = Vec::new(); + + for _ in 0..max_x { + x_row.push(0); + } + + field.push(x_row); + } + + for claim in claims { + for x in claim.area.a.x..claim.area.b.x { + for y in claim.area.a.y..claim.area.b.y { + field[y as usize][x as usize] += 1; + } + } + } + + let mut count = 0; + for y in field { + for val in y { + if val > 1 { + count += 1; + } + } + } + + println!("{}", count); + + Ok(()) +} diff --git a/03/part2/Cargo.lock b/03/part2/Cargo.lock new file mode 100644 index 0000000..80e202c --- /dev/null +++ b/03/part2/Cargo.lock @@ -0,0 +1,104 @@ +[[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 = "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 = "part2" +version = "0.1.0" +dependencies = [ + "scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.2.11" +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.5.6 (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.5.6" +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 = "scan_fmt" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.2.11 (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 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" +"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" +"checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30" +"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" diff --git a/03/part2/Cargo.toml b/03/part2/Cargo.toml new file mode 100644 index 0000000..a210fe8 --- /dev/null +++ b/03/part2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "part2" +version = "0.1.0" +authors = ["Nick Thomas "] +edition = "2018" + +[dependencies] +scan_fmt = "0.1.3" diff --git a/03/part2/src/main.rs b/03/part2/src/main.rs new file mode 100644 index 0000000..66d44b4 --- /dev/null +++ b/03/part2/src/main.rs @@ -0,0 +1,104 @@ +#[macro_use] extern crate scan_fmt; + +use std::collections::HashSet; +use std::fs::File; +use std::io::BufReader; +use std::io::prelude::*; + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Point { + x: usize, + y: usize, +} + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Rect { + a: Point, + b: Point, +} + +#[derive(Hash, Eq, PartialEq, Debug)] +struct Claim { + id: u32, + area: Rect, +} + +impl Claim { + fn new(from: &str) -> Claim { + let (id,x,y,w,h) = scan_fmt!( + from, + "#{} @ {},{}: {}x{}", + u32, usize, usize, usize, usize + ); + + return Claim{ + id: id.unwrap(), + area: Rect { + a: Point {x: x.unwrap(), y: y.unwrap()}, + b: Point {x: x.unwrap()+w.unwrap(), y: y.unwrap()+h.unwrap()}, + }, + } + } +} + +fn main() -> std::io::Result<()> { + let f = File::open("../input")?; + let reader = BufReader::new(f); + + let mut claims: Vec = Vec::new(); + let mut max_x = 0; + let mut max_y = 0; + + for line in reader.lines() { + let claim = Claim::new(&line.unwrap()); + + if claim.area.b.x > max_x { + max_x = claim.area.b.x; + } + + if claim.area.b.y > max_y { + max_y = claim.area.b.y; + } + + claims.push(claim); + } + + let mut field: Vec>> = Vec::new(); + for _ in 0..max_y { + let mut x_row: Vec> = Vec::new(); + + for _ in 0..max_x { + x_row.push(Vec::new()); + } + + field.push(x_row); + } + + for claim in &claims { + for x in claim.area.a.x..claim.area.b.x { + for y in claim.area.a.y..claim.area.b.y { + field[y][x].push(claim.id); + } + } + } + + let mut bad = HashSet::new(); + + for y in field { + for x in y { + if x.len() > 1 { + for id in x { + bad.insert(id); + } + } + } + } + + for claim in &claims { + if !bad.contains(&claim.id) { + println!("{} is good", claim.id) + } + } + + Ok(()) +}