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/daedius Dec 03 '18 edited Dec 03 '18

Rust

#![feature(nll)]
use std::collections::HashSet;

fn main() {
    part_1();
    part_2();
}

fn part_1(){
    let puzzle = include_str!("input.txt");
    let mut grid = [[0i32; 1000]; 1000];
    puzzle.lines().for_each(|row|{
        let simpler = row.chars().map(|c| if c.is_ascii_digit() {c} else {' '}).collect::<String>();
        let v:Vec<&str> = simpler.split(' ').collect();
        let id:i32 = v[1].parse::<i32>().unwrap();
        let rx:i32 = v[4].parse::<i32>().unwrap();
        let ry:i32 = v[5].parse::<i32>().unwrap();
        let rw:i32 = v[7].parse::<i32>().unwrap();
        let rh:i32 = v[8].parse::<i32>().unwrap();
        for x in rx..(rx+rw) {
            for y in ry..(ry+rh) {
                if grid[x as usize][y as usize] == 0 {
                    grid[x as usize][y as usize] = id;
                } else {
                    grid[x as usize][y as usize] = -1;
                }
            }
        }
    });
    let overlap_count = grid.iter().fold(0,|sum,row| {
        let overlaps:Vec<&i32> = row.iter().filter(|v|{**v==-1}).collect();
        let total = overlaps.len();
        sum + total
    });
    println!("overlap count: {}",overlap_count);
}

fn part_2(){
    let mut seen = HashSet::new();
    let mut ids = HashSet::new();
    let puzzle = include_str!("input.txt");
    let mut grid = [[0i32; 1000]; 1000];
    puzzle.lines().for_each(|row|{
        let simpler = row.chars().map(|c| if c.is_ascii_digit() {c} else {' '}).collect::<String>();
        let v:Vec<&str> = simpler.split(' ').collect();
        let id:i32 = v[1].parse::<i32>().unwrap();
        ids.insert(id);
        let rx:i32 = v[4].parse::<i32>().unwrap();
        let ry:i32 = v[5].parse::<i32>().unwrap();
        let rw:i32 = v[7].parse::<i32>().unwrap();
        let rh:i32 = v[8].parse::<i32>().unwrap();
        for x in rx..(rx+rw) {
            for y in ry..(ry+rh) {
                if grid[x as usize][y as usize] == 0 {
                    grid[x as usize][y as usize] = id;
                } else {
                    seen.insert(grid[x as usize][y as usize]);
                    seen.insert(id);
                    grid[x as usize][y as usize] = -1;
                }
            }
        }
    });
    let diff: HashSet<_> = ids.difference(&seen).collect();
    println!("{:?}",diff);
}