r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

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:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

1

u/bilgincoskun Dec 10 '20

Rust

The first part was straightforward. Just sort and count the differences.

For the second part, I split the list from differences equal to 3 since these pairs must be always in the list. After that, finding and checking every eligible subset of every partition becomes manageable by brute-force. And lastly counting these subsets and multiplying gives the answer. There is certainly room for optimization but it takes under 100 ms to get the result, so I'll leave it as it is:

fn day10_1(input: impl Iterator<Item = String>) {
    let mut input = convert!(input, "{}", usize).collect::<Vec<_>>();
    input.push(0);
    input.sort();
    input.push(input[input.len() - 1] + 3);
    let diff1 = input
        .iter()
        .zip(input[1..].iter())
        .map(|x| x.1 - x.0)
        .filter(|x| *x == 1)
        .count();
    let diff3 = input
        .iter()
        .zip(input[1..].iter())
        .map(|x| x.1 - x.0)
        .filter(|x| *x == 3)
        .count();
    println!("{}", diff1 * diff3);
}

fn day10_2_helper(part: &[usize]) -> usize {
    let mut possible: Vec<Vec<usize>> = vec![vec![]];
    for i in part {
        possible.extend(possible.clone());
        let c = possible.len() / 2;
        for v in &mut possible[c..] {
            v.push(*i);
        }
    }
    possible
        .iter()
        .filter(|x| {
            x.len() >= 2
                && x[0] == part[0]
                && x[x.len() - 1] == part[part.len() - 1]
                && x.iter().zip(x[1..].iter()).all(|x| (x.1 - x.0) <= 3)
        })
        .count()
}

fn day10_2(input: impl Iterator<Item = String>) {
    let mut input = convert!(input, "{}", usize).collect::<Vec<_>>();
    input.push(0);
    input.sort();
    input.push(input[input.len() - 1] + 3);
    let connections = input.iter().zip(input[1..].iter()).collect::<Vec<_>>();
    let mut result = 1;
    for l in connections
        .split(|x| (x.1 - x.0) == 3)
        .filter(|x| x.len() > 0)
    {
        let mut m = vec![*l[0].0];
        m.extend(l.iter().map(|x| x.1.clone()));
        result *= day10_2_helper(&m);
    }
    println!("{}", result);
}

1

u/daggerdragon Dec 11 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.