[2018][rust][3.0]

sorbet
Alpha Chen 6 years ago
parent af6a02c6a2
commit cf72591813

91
2018/rust/Cargo.lock generated

@ -1,4 +1,95 @@
[[package]] [[package]]
name = "advent_of_code_2018" name = "advent_of_code_2018"
version = "0.1.0" version = "0.1.0"
dependencies = [
"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"

@ -5,3 +5,4 @@ authors = ["Alpha Chen <alpha.chen@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
regex = "1"

@ -0,0 +1,120 @@
use std::collections::HashMap;
use std::error::Error;
use std::io::{self, Read};
use std::str::FromStr;
use regex::Regex;
fn main() -> Result<(), Box<Error>> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let output = solve(&input)?;
println!("{}", output);
Ok(())
}
fn solve(input: &str) -> Result<String, Box<Error>> {
let claims: Vec<_> = input
.lines()
.map(str::trim)
.map(Claim::from_str)
.collect::<Result<_, _>>()?;
let fabric = claims.iter().fold(HashMap::new(), |mut fabric, claim| {
for square_inch in claim.square_inches() {
fabric
.entry(square_inch)
.and_modify(|count| *count += 1)
.or_insert(1);
}
fabric
});
let output = fabric.values().filter(|&&count| count > 1).count();
Ok(output.to_string())
}
#[test]
fn test_solve() {
let input = r"
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2
"
.trim();
assert_eq!(&solve(input).unwrap(), "4");
}
#[derive(Debug, PartialEq, Eq)]
struct Claim {
id: usize,
x: usize,
y: usize,
width: usize,
height: usize,
}
impl Claim {
fn square_inches(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
(self.x..self.x + self.width)
.flat_map(move |x| (self.y..self.y + self.height).map(move |y| (x, y)))
}
}
#[test]
fn test_claim_square_inches() {
let claim = Claim {
id: 1,
x: 2,
y: 3,
width: 4,
height: 4,
};
let square_inches: Vec<_> = claim.square_inches().collect();
assert_eq!(square_inches.len(), 16);
assert!(square_inches.contains(&(2, 3)));
assert!(square_inches.contains(&(5, 3)));
assert!(square_inches.contains(&(5, 6)));
assert!(square_inches.contains(&(2, 6)));
}
impl FromStr for Claim {
type Err = Box<Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let re =
Regex::new(r"^#(?P<id>\d+) @ (?P<x>\d+),(?P<y>\d+): (?P<width>\d+)x(?P<height>\d+)$")
.unwrap();
let captures = re.captures(s).unwrap();
let id = captures.name("id").unwrap().as_str().parse()?;
let x = captures.name("x").unwrap().as_str().parse()?;
let y = captures.name("y").unwrap().as_str().parse()?;
let width = captures.name("width").unwrap().as_str().parse()?;
let height = captures.name("height").unwrap().as_str().parse()?;
Ok(Claim {
id,
x,
y,
width,
height,
})
}
}
#[test]
fn test_claim_from_str() {
let claim = Claim::from_str("#1 @ 1,3: 4x4").unwrap();
assert_eq!(
claim,
Claim {
id: 1,
x: 1,
y: 3,
width: 4,
height: 4
}
);
}
Loading…
Cancel
Save