Day 3 2018

This commit is contained in:
Wesley Moore 2018-12-05 07:11:20 +11:00
parent f78231443c
commit 0ed1c5cec9
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE
4 changed files with 1544 additions and 0 deletions

92
2018/Cargo.lock generated
View file

@ -1,4 +1,96 @@
[[package]]
name = "advent-of-code"
version = "0.1.0"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[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 = "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"

View file

@ -4,3 +4,5 @@ version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
[dependencies]
lazy_static = "1.2.0"
regex = "1.1.0"

1331
2018/input/2018/day3.txt Normal file

File diff suppressed because it is too large Load diff

119
2018/src/bin/day3.rs Normal file
View file

@ -0,0 +1,119 @@
#[macro_use]
extern crate lazy_static;
extern crate regex;
use regex::Regex;
use std::fs;
const FABRIC_SIZE: usize = 1000;
#[derive(Clone)]
struct Rect {
x: u32,
y: u32,
width: u32,
height: u32,
}
#[derive(Clone)]
struct Claim {
id: u32,
rect: Rect,
}
fn main() {
let input = fs::read_to_string("input/2018/day3.txt").expect("input");
let claims = input
.lines()
.map(parse_claim)
.collect::<Option<Vec<_>>>()
.expect("input error");
let fabric = claim_counts(&claims);
part1(&fabric);
part2(&fabric);
}
fn part1<'a>(input: &[Vec<&'a Claim>]) {
let over_two_claims = input.into_iter().filter(|claims| claims.len() >= 2).count();
println!("{}", over_two_claims);
}
fn part2<'a>(input: &[Vec<&'a Claim>]) {
let non_overlapping_claim = input
.into_iter()
.filter_map(|claims| {
if claims.len() == 1 {
Some(claims[0])
} else {
None
}
}).find(|candidate| winner(input, candidate))
.expect("did not find non-overlapping claim");
println!("{}", non_overlapping_claim.id);
}
fn parse_claim(line: &str) -> Option<Claim> {
lazy_static! {
static ref RE: Regex = Regex::new(r#"\A#(\d+) @ (\d+),(\d+): (\d+)x(\d+)\z"#).unwrap();
}
let captures = RE.captures(line)?;
let id = captures[1].parse().ok()?;
let x = captures[2].parse().ok()?;
let y = captures[3].parse().ok()?;
let width = captures[4].parse().ok()?;
let height = captures[5].parse().ok()?;
Some(Claim {
id,
rect: Rect {
x,
y,
width,
height,
},
})
}
fn claim_counts<'a>(claims: &'a [Claim]) -> Vec<Vec<&'a Claim>> {
let mut claim_counts = vec![Vec::new(); FABRIC_SIZE * FABRIC_SIZE];
for claim in claims {
for y in claim.rect.y..claim.rect.max_y() {
for x in claim.rect.x..claim.rect.max_x() {
let existing = claim_counts
.get_mut(y as usize * FABRIC_SIZE + x as usize)
.unwrap();
existing.push(claim)
}
}
}
claim_counts
}
fn winner<'a>(claims: &[Vec<&'a Claim>], candidate: &Claim) -> bool {
for y in candidate.rect.y..candidate.rect.max_y() {
for x in candidate.rect.x..candidate.rect.max_x() {
if claims[y as usize * FABRIC_SIZE + x as usize].len() != 1 {
return false;
}
}
}
true
}
impl Rect {
fn max_x(&self) -> u32 {
self.x + self.width
}
fn max_y(&self) -> u32 {
self.y + self.height
}
}