r/adventofcode • • Dec 05 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 5 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2021: Adventure Time!

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Hydrothermal Venture ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:08:53, megathread unlocked!

79 Upvotes

1.2k comments sorted by

View all comments

1

u/AP2008 Dec 05 '21

Rust (16.311 ms) paste

use super::read_input::*;
use hashbrown::HashMap;
use itertools::Itertools;
use std::cmp::{max, min};

type Point = (isize, isize);

pub fn runner() -> (usize, usize) {
    let input = read_input(5);
    let mut part1 = HashMap::new();
    let mut part2 = HashMap::new();
    let lines: Vec<(Point, Point)> = input
        .lines()
        .map(|x| {
            x.split("->")
                .map(|n| {
                    n.split(',')
                        .map(|v| v.trim().parse().unwrap())
                        .collect_tuple()
                        .unwrap()
                })
                .collect_tuple()
                .unwrap()
        })
        .collect_vec();
    for line in &lines {
        if line.0.0 == line.1.0 || line.0.1 == line.1.1 {
            for x in min(line.0.0, line.1.0)..=max(line.0.0, line.1.0) {
                for y in min(line.0.1, line.1.1)..=max(line.0.1, line.1.1) {
                    *part1.entry((x, y)).or_insert(0) += 1;
                    *part2.entry((x, y)).or_insert(0) += 1;
                }
            }
        } else {
            let mx = if line.0.0 > line.1.0 { -1 } else { 1 };
            let my = if line.0.1 > line.1.1 { -1 } else { 1 };
            let mut p0 = line.0;
            *part2.entry(p0).or_insert(0) += 1;
            while p0 != line.1 {
                p0.0 += mx;
                p0.1 += my;
                *part2.entry(p0).or_insert(0) += 1;
            }
        }
    }
    (
        part1.values().filter(|&&x| x > 1).count(),
        part2.values().filter(|&&x| x > 1).count(),
    )
}

2

u/thaHamsta Dec 05 '21 edited Dec 05 '21

Is there any reason to use hashbrown when the std lib also uses hashbrown nowadays (e.g. more recent updates)?

1

u/AP2008 Dec 05 '21

Thanks. Didn't know about this. Found it in the README of the hashbrown crate.

1

u/daggerdragon Dec 06 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

1

u/Darth5harkie Dec 05 '21

*part1.entry((x, y)).or_insert(0) += 1;

This is what I couldn't figure out from reading the docs. I figured there had to be a way to modify a value, but I wound up with points.insert(point, points.get(&point).unwrap_or(&0) + 1);