You finally have a chance to look at all of the produce moving around. Chocolate, cinnamon, mint, chili peppers, nutmeg, vanilla... the Elves must be growing these plants to make hot chocolate! As you realize this, you hear a conversation in the distance. When you go to investigate, you discover two Elves in what appears to be a makeshift underground kitchen/laboratory.
The Elves are trying to come up with the ultimate hot chocolate recipe; they're even maintaining a scoreboard which tracks the quality score (0
-9
) of each recipe.
Read the full puzzle.
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2018.Day14;
[ProblemName("Chocolate Charts")]
class Solution : Solver {
public object PartOne(string input) => Window(10).ElementAt(int.Parse(input)).st;
public object PartTwo(string input) => Window(input.Length).First(item => item.st == input).i;
IEnumerable<(int i, string st)> Window(int w) {
var st = "";
var i = 0;
foreach (var score in Scores()) {
i++;
st += score;
if (st.Length > w) {
st = st.Substring(st.Length - w);
}
if (st.Length == w) {
yield return (i - w, st);
}
}
}
IEnumerable<int> Scores() {
var scores = new List<int>();
Func<int, int> add = (i) => { scores.Add(i); return i; };
var elf1 = 0;
var elf2 = 1;
yield return add(3);
yield return add(7);
while (true) {
var sum = scores[elf1] + scores[elf2];
if (sum >= 10) {
yield return add(sum / 10);
}
yield return add(sum % 10);
elf1 = (elf1 + scores[elf1] + 1) % scores.Count;
elf2 = (elf2 + scores[elf2] + 1) % scores.Count;
}
}
}
Please ☆ my repo if you like it!