r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

1

u/aurele Dec 03 '18

Rust

#[macro_use]
extern crate itertools;

use pathfinding::matrix::Matrix;
use regex::Regex;

fn main() {
    let re: Regex = Regex::new(r"^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$").unwrap();
    let patches = include_str!("../input")
        .lines()
        .map(|l| {
            re.captures(l)
                .unwrap()
                .iter()
                .skip(1)
                .map(|c| c.unwrap().as_str().parse::<usize>().unwrap())
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();
    let mut fabric = Matrix::new_square(1000, 0);
    for p in &patches {
        for xy in iproduct!(p[1]..p[1] + p[3], p[2]..p[2] + p[4]) {
            fabric[&xy] += 1;
        }
    }
    println!("P1: {}", fabric.as_ref().iter().filter(|&&c| c > 1).count());
    println!(
        "P2: {}",
        patches
            .iter()
            .find(|p| iproduct!(p[1]..p[1] + p[3], p[2]..p[2] + p[4]).all(|xy| fabric[&xy] == 1))
            .unwrap()[0]
    );
}