From c9d38ac98b9638efcee0428f17e4f5d2b50580a8 Mon Sep 17 00:00:00 2001 From: alecdwm Date: Tue, 4 Dec 2018 01:22:16 +1000 Subject: added day3 part2 solution --- src/day3.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 43 insertions(+) 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 = 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" { -- cgit v1.2.3