Day 3 part 1
This commit is contained in:
parent
8fe3e93bd6
commit
e54d9297ff
91
Cargo.lock
generated
91
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.2 (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.45"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.1.2"
|
||||||
|
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.45 (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.2 (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.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
|
||||||
|
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
|
||||||
|
"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 = ["Tyler Hallada <tyler@hallada.net>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1"
|
||||||
|
1401
inputs/3.txt
Normal file
1401
inputs/3.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
inputs/3_test.txt
Normal file
3
inputs/3_test.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#1 @ 1,3: 4x4
|
||||||
|
#2 @ 3,1: 4x4
|
||||||
|
#3 @ 5,5: 2x2
|
3
inputs/3_test_malformed.txt
Normal file
3
inputs/3_test_malformed.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#1 @ 1,3: 4x4
|
||||||
|
#2 @ 3,1: 4x4
|
||||||
|
#3 @ 5,5: 2x2
|
164
src/day3.rs
Normal file
164
src/day3.rs
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
extern crate regex;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::fmt;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use regex::{Regex, Captures};
|
||||||
|
|
||||||
|
const INPUT: &str = "inputs/3.txt";
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct Claim {
|
||||||
|
id: u32,
|
||||||
|
left: u32,
|
||||||
|
top: u32,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
|
struct Point {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
struct MalformedClaim {
|
||||||
|
details: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MalformedClaim {
|
||||||
|
fn new(msg: &str) -> MalformedClaim {
|
||||||
|
MalformedClaim{ details: msg.to_string() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for MalformedClaim {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.details)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for MalformedClaim {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
&self.details
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve_part1() -> Result<u32, Box<Error>> {
|
||||||
|
Ok(count_overlapping_claimed_points(read_claims(INPUT)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_overlapping_claimed_points(claims: Vec<Claim>) -> u32 {
|
||||||
|
let mut claimed_points: HashMap<Point, u32> = HashMap::new();
|
||||||
|
for claim in claims {
|
||||||
|
for point in list_points_in_claim(claim) {
|
||||||
|
let current_point = claimed_points.get(&point).unwrap_or(&0);
|
||||||
|
claimed_points.insert(point, current_point + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return claimed_points.values().fold(0, |acc, claims| {
|
||||||
|
if claims > &1 {
|
||||||
|
return acc + 1
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_points_in_claim(claim: Claim) -> Vec<Point> {
|
||||||
|
let mut points = Vec::new();
|
||||||
|
for x in 0..claim.width {
|
||||||
|
for y in 0..claim.height {
|
||||||
|
points.push(Point { x: claim.left + x, y: claim.top + y });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
points
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_claims(filename: &str) -> Result<Vec<Claim>, Box<Error>> {
|
||||||
|
let mut claims: Vec<Claim> = Vec::new();
|
||||||
|
let claim_regex =
|
||||||
|
Regex::new(r"#(?P<id>\d+) @ (?P<left>\d+),(?P<top>\d+): (?P<width>\d+)x(?P<height>\d+)")?;
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
for line in BufReader::new(file).lines() {
|
||||||
|
match claim_regex.captures(&line?) {
|
||||||
|
Some(captures) => {
|
||||||
|
let claim = Claim {
|
||||||
|
id: get_captured_field(&captures, "id")?,
|
||||||
|
left: get_captured_field(&captures, "left")?,
|
||||||
|
top: get_captured_field(&captures, "top")?,
|
||||||
|
width: get_captured_field(&captures, "width")?,
|
||||||
|
height: get_captured_field(&captures, "height")?,
|
||||||
|
};
|
||||||
|
claims.push(claim);
|
||||||
|
},
|
||||||
|
None => return Err(Box::new(MalformedClaim {
|
||||||
|
details: "Malformed claim line, no fields could be found".to_string()
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(claims)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_captured_field(captures: &Captures, field: &str) -> Result<u32, Box<Error>> {
|
||||||
|
match captures.name(field) {
|
||||||
|
Some(capture) => Ok(capture.as_str().parse()?),
|
||||||
|
None => return Err(Box::new(MalformedClaim {
|
||||||
|
details: format!("Malformed claim line, field {} could not be found", field)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const TEST_INPUT: &str = "inputs/3_test.txt";
|
||||||
|
const TEST_INPUT_MALFORMED: &str = "inputs/3_test_malformed.txt";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reads_claims_file() {
|
||||||
|
assert_eq!(read_claims(TEST_INPUT).unwrap(), vec![
|
||||||
|
Claim { id: 1, left: 1, top: 3, width: 4, height: 4 },
|
||||||
|
Claim { id: 2, left: 3, top: 1, width: 4, height: 4 },
|
||||||
|
Claim { id: 3, left: 5, top: 5, width: 2, height: 2 },
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn errors_on_malformed_claims_file() {
|
||||||
|
match read_claims(TEST_INPUT_MALFORMED) {
|
||||||
|
Ok(_) => assert!(false, "read_claims should have returned an error"),
|
||||||
|
Err(err) => assert_eq!(
|
||||||
|
(*err).description(),
|
||||||
|
"Malformed claim line, no fields could be found".to_string(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lists_points_in_claim() {
|
||||||
|
assert_eq!(list_points_in_claim(Claim { id: 1, left: 0, top: 0, width: 2, height: 2 }),
|
||||||
|
vec![
|
||||||
|
Point { x: 0, y: 0 },
|
||||||
|
Point { x: 0, y: 1 },
|
||||||
|
Point { x: 1, y: 0 },
|
||||||
|
Point { x: 1, y: 1 },
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn counts_overlapping_claimed_points() {
|
||||||
|
let test_claims = vec![
|
||||||
|
Claim { id: 1, left: 1, top: 3, width: 4, height: 4 },
|
||||||
|
Claim { id: 2, left: 3, top: 1, width: 4, height: 4 },
|
||||||
|
Claim { id: 3, left: 5, top: 5, width: 2, height: 2 },
|
||||||
|
];
|
||||||
|
assert_eq!(count_overlapping_claimed_points(test_claims), 4);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
mod day1;
|
mod day1;
|
||||||
mod day2;
|
mod day2;
|
||||||
|
mod day3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Day 1:");
|
println!("Day 1:");
|
||||||
@ -8,4 +9,6 @@ fn main() {
|
|||||||
println!("Day 2:");
|
println!("Day 2:");
|
||||||
println!("{}", day2::solve_part1().unwrap());
|
println!("{}", day2::solve_part1().unwrap());
|
||||||
println!("{}", day2::solve_part2().unwrap().unwrap());
|
println!("{}", day2::solve_part2().unwrap().unwrap());
|
||||||
|
println!("Day 3:");
|
||||||
|
println!("{}", day3::solve_part1().unwrap());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user