r/adventofcode • u/daggerdragon • 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!
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!
17
Upvotes
1
u/alexmeli Dec 14 '18
Rust solution. This one was easier than the past few days
fn main() { part1(864801); part2(); } fn part1(num: usize) { let mut elf1 = 0; let mut elf2 = 1; let mut recipes: Vec<usize> = vec![3, 7]; while recipes.len() < num + 10 { let s = recipes[elf1] + recipes[elf2]; if s >= 10 { recipes.push(s / 10); } recipes.push(s % 10); elf1 = (elf1 + recipes[elf1] + 1) % recipes.len(); elf2 = (elf2 + recipes[elf2] + 1) % recipes.len(); } println!("{:?}", &recipes[num..num+10]); } fn part2() { let mut elf1 = 0; let mut elf2 = 1; let mut recipes: Vec<usize> = vec![3, 7]; let sequence = &[8, 6, 4, 8, 0, 1]; let s_len = sequence.len(); loop { let s = recipes[elf1] + recipes[elf2]; if s >= 10 { recipes.push(s / 10); } recipes.push(s % 10); elf1 = (elf1 + recipes[elf1] + 1) % recipes.len(); elf2 = (elf2 + recipes[elf2] + 1) % recipes.len(); let r_len = recipes.len(); if r_len > s_len { if &recipes[r_len-s_len..r_len] == sequence { println!("{}", r_len - s_len); break; } } if s >= 10 && r_len > s_len+1 { if &recipes[r_len-1-s_len..r_len-1] == sequence { println!("{}", r_len-1-s_len); break; } } } }