aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2018-12-04 01:22:16 +1000
committerGravatar alecdwm 2018-12-04 01:22:16 +1000
commitc9d38ac98b9638efcee0428f17e4f5d2b50580a8 (patch)
treedca510facb55062d2834d4fd3a82f0bf472569d1
parentf5ebaad3dc2d4cb2f231c70fc6afd8f6bfc99d90 (diff)
added day3 part2 solution
-rw-r--r--src/day3.rs42
-rw-r--r--src/main.rs1
2 files changed, 43 insertions, 0 deletions
diff --git a/src/day3.rs b/src/day3.rs
index 8161c86..eb2ec85 100644
--- a/src/day3.rs
+++ b/src/day3.rs
@@ -68,6 +68,48 @@ pub fn part1() {
);
}
+/// Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
+///
+/// For example, in the claims above, only claim 3 is intact after all claims are made.
+///
+/// What is the ID of the only claim that doesn't overlap?
+pub fn part2() {
+ let input = ::common::read_stdin_to_string();
+
+ let mut fabric: BTreeMap<(i64, i64), u8> = BTreeMap::new();
+ let mut claims: Vec<FabricClaim> = Vec::new();
+
+ for line in input.lines() {
+ let claim: FabricClaim = line.parse().expect("Parsing fabric claim");
+ for w in 0..claim.width {
+ for h in 0..claim.height {
+ let index = (claim.pos_x + w, claim.pos_y + h);
+ *fabric.entry(index).or_insert(0) += 1;
+ }
+ }
+ claims.push(line.parse().expect("Parsing fabric claim"));
+ }
+
+ let mut free_claim_id = -1;
+
+ 'claim_loop: for claim in claims.iter() {
+ for w in 0..claim.width {
+ for h in 0..claim.height {
+ let index = (claim.pos_x + w, claim.pos_y + h);
+ if *fabric.get(&index).unwrap() > 1 {
+ continue 'claim_loop;
+ }
+ }
+ }
+ free_claim_id = claim.id;
+ }
+
+ println!(
+ "the ID of the only claim that doesn't overlap: {}",
+ free_claim_id
+ );
+}
+
#[derive(Debug)]
struct FabricClaim {
id: i64,
diff --git a/src/main.rs b/src/main.rs
index 846ad42..9af7e79 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,7 @@ fn main() {
puzzle_solution_map.insert("day2::part1", advent_of_code_2018::day2::part1);
puzzle_solution_map.insert("day2::part2", advent_of_code_2018::day2::part2);
puzzle_solution_map.insert("day3::part1", advent_of_code_2018::day3::part1);
+ puzzle_solution_map.insert("day3::part2", advent_of_code_2018::day3::part2);
let command = args[1].as_str();
if command == "list" {