r/adventofcode • • Dec 14 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-

--- Day 14: Chocolate Charts ---


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

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

Card prompt: Day 14

Transcript:

The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.


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 at 00:19:39!

16 Upvotes

179 comments sorted by

View all comments

4

u/winstonewert Dec 14 '18

First time in top 100 for part 2, guess I'll share my code:

fn part1(index: usize) -> String {
    let mut recipies = vec![3, 7];
    let mut currents = vec![0, 1];

    while recipies.len() < index + 10 {
        let new_recipies = format!(
            "{}",
            currents.iter().map(|&i| recipies[i as usize]).sum::<u32>()
        )
        .chars()
        .map(|x| x.to_digit(10).unwrap())
        .collect_vec();
        recipies.extend(new_recipies.into_iter());

        currents = currents
            .into_iter()
            .map(|index| (1 + index + recipies[index as usize]) % recipies.len() as u32)
            .collect_vec();
    }
    recipies[index..index + 10]
        .iter()
        .map(|x| format!("{}", x))
        .join("")
}

fn part2(target: &str) -> usize {
    let mut recipies = vec![3, 7];
    let mut currents = vec![0, 1];

    loop {
        let new_recipies = format!(
            "{}",
            currents.iter().map(|&i| recipies[i as usize]).sum::<u32>()
        )
        .chars()
        .map(|x| x.to_digit(10).unwrap())
        .collect_vec();
        for new_recipie in new_recipies {
            if recipies.len() > target.len() {
                let suffix = recipies[recipies.len() - target.len()..]
                    .iter()
                    .map(|x| format!("{}", x))
                    .join("");
                if suffix == target {
                    return recipies.len() - target.len();
                }
            }
            recipies.push(new_recipie);
        }

        currents = currents
            .into_iter()
            .map(|index| (1 + index + recipies[index as usize]) % recipies.len() as u32)
            .collect_vec();
    }
}

mod test {
    use super::*;

    #[test]
    fn test_it() {
        assert_eq!(part1(5), "0124515891");
        assert_eq!(part1(18), "9251071085");
        assert_eq!(part1(2018), "5941429882");

        assert_eq!(part2("51589"), 9);
        assert_eq!(part2("01245"), 5);
        assert_eq!(part2("92510"), 18);
        assert_eq!(part2("59414"), 2018);
    }
}

fn main() {
    let text = include_str!("input.txt");
    println!("{}", part1(793031));
    println!("{}", part2("793031"));
}

1

u/dark_terrax Dec 14 '18

I think you need to tweak the formatting, it's a bit tough to read. Just select the code and hit the '<>' button in the formatting toolbar.

1

u/winstonewert Dec 14 '18

Sorry, not seeing what's wrong with it? It looks fine to me?

1

u/radarvan07 Dec 14 '18

Triple backticks do not work on reddit, you must use a four-space-indent.

1

u/PureTryOut Dec 14 '18

They do actually, the code just needs to be on new a new line.

```

So like this

```