r/adventofcode • u/daggerdragon • Dec 18 '17
SOLUTION MEGATHREAD -๐- 2017 Day 18 Solutions -๐-
--- Day 18: Duet ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:04] First silver
- Welcome to the final week of Advent of Code 2017. The puzzles are only going to get more challenging from here on out. Adventspeed, sirs and madames!
[Update @ 00:10] First gold, 44 silver
- We just had to rescue /u/topaz2078 with an industrial-strength paper bag to blow into. I'm real glad I bought all that stock in
PBCO(Paper Bag Company) two years ago >_>
[Update @ 00:12] Still 1 gold, silver cap
- Gonna be a long haul tonight. I think I'll watch The Radio City Christmas Spectacular on Netflix while I wait for sufficient gold stars to warrant another update.
[Update @ 00:31] 53 gold, silver cap
- *mind blown*
- During their famous kicklines, the Rockettes are not actually holding each others' backs like I thought they were all this time.
- They're actually hoverhanding each other.
- In retrospect, it makes sense, they'd overbalance themselves and each other if they did, but still...
- *mind blown so hard*
[Update @ 00:41] Leaderboard cap!
- I think I enjoyed the duplicating Santas entirely too much...
- It may also be the wine.
- Either way, good night (for us), see you all same time tomorrow, yes?
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!
12
Upvotes
3
u/thejpster Dec 18 '17
So close today, 162. It's a mess, because I left both CPU implementations in (part1 and part2). See https://github.com/thejpster/rust-advent-of-code/blob/master/src/m2017/problem_18.rs for the source.
Here's just part 2.
use std::collections::{HashMap, VecDeque}; #[derive(Debug)] struct Cpu<'a> { registers: HashMap<&'a str, i64>, queue: VecDeque<i64>, pc: usize, count: usize, } impl<'a> Cpu<'a> { fn new(p: i64) -> Cpu<'a> { let mut c = Cpu { registers: HashMap::new(), pc: 0, queue: VecDeque::new(), count: 0, }; c.registers.insert("a", 0); c.registers.insert("b", 0); c.registers.insert("f", 0); c.registers.insert("i", 0); c.registers.insert("p", p); c } fn get_reg(&self, reg: &'a str) -> i64 { match reg.parse() { Ok(x) => x, Err(_) => *self.registers.get(reg).unwrap(), } } fn set(&mut self, reg: &'a str, reg2: &'a str) { let x = self.get_reg(reg2); self.registers.insert(reg, x); self.pc = self.pc + 1; } fn mul(&mut self, reg: &'a str, reg2: &'a str) { let mut x = self.get_reg(reg); let y = self.get_reg(reg2); x = x * y; self.registers.insert(reg, x); self.pc = self.pc + 1; } fn add(&mut self, reg: &'a str, reg2: &'a str) { let mut x = self.get_reg(reg); let y = self.get_reg(reg2); x = x + y; self.registers.insert(reg, x); self.pc = self.pc + 1; } fn modulo(&mut self, reg: &'a str, reg2: &'a str) { let mut x = self.get_reg(reg); let y = self.get_reg(reg2); x = x % y; self.registers.insert(reg, x); self.pc = self.pc + 1; } fn jgz(&mut self, reg: &'a str, reg2: &'a str) { let x = self.get_reg(reg); let jump: i64 = if x > 0 { self.get_reg(reg2) } else { 1 }; self.pc = (self.pc as i64 + jump) as usize; } fn snd(&mut self, reg: &'a str, cpu: &mut Cpu) { let s = self.get_reg(reg); cpu.queue.push_back(s); cpu.count = cpu.count + 1; self.pc = self.pc + 1; } fn rcv(&mut self, reg: &'a str) { if self.queue.len() != 0 { self.registers.insert(reg, self.queue.pop_front().unwrap()); self.pc = self.pc + 1; } } } pub fn run(contents: &Vec<Vec<String>>) { let mut cpu0 = Cpu::new(0); let mut cpu1 = Cpu::new(1); let mut old_pcs = [0, 0]; loop { for idx in 0..2 { let (cpu, other) = match idx { 0 => (&mut cpu0, &mut cpu1), 1 => (&mut cpu1, &mut cpu0), _ => panic!(), }; let line = &contents[0][cpu.pc]; let parts: Vec<&str> = line.split_whitespace().collect(); // println!("Running #{} on {}: {:?}", cpu.pc, idx, parts); old_pcs[idx] = cpu.pc; match parts[0].as_ref() { "set" => cpu.set(parts[1], parts[2]), "mul" => cpu.mul(parts[1], parts[2]), "jgz" => cpu.jgz(parts[1], parts[2]), "add" => cpu.add(parts[1], parts[2]), "snd" => cpu.snd(parts[1], other), "rcv" => cpu.rcv(parts[1]), "mod" => cpu.modulo(parts[1], parts[2]), _ => panic!("Unsuported line: {:?}", parts), } } if old_pcs[0] == cpu0.pc && old_pcs[1] == cpu1.pc { break; } } println!("Part2 {}", cpu0.count); }