diff options
author | tomsmeding <tom.smeding@gmail.com> | 2018-12-21 20:22:02 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2018-12-21 20:22:02 +0100 |
commit | 7dc5aa8bca7699c406c3dacd9e24c26cf74ca510 (patch) | |
tree | 9a4b94125812a10fe93e85b5e9c9f52c19e90e9f | |
parent | a091837040819f25df4f99176820e6ede0687780 (diff) |
Day 19 part 2 WIP
-rw-r--r-- | 2018/19_notes_1.txt | 42 | ||||
-rw-r--r-- | 2018/src/day19.rs | 52 |
2 files changed, 38 insertions, 56 deletions
diff --git a/2018/19_notes_1.txt b/2018/19_notes_1.txt index e317941..c6c5532 100644 --- a/2018/19_notes_1.txt +++ b/2018/19_notes_1.txt @@ -1,36 +1,6 @@ -L0: jmp L4 -L1: r5 <- 1 -L2: r2 <- 1 - r1 <- 2 - r1 <- 2 == r3 - rip <- r1 + 5 - jmp L3 - r0 <- r5 + r0 -L3: r2 <- r2 + 1 - r1 <- r2 > r3 - rip <- 10 + r1 - jmp L2 - r5 <- r5 + 1 - r1 <- r5 > r3 - rip <- r1 + 14 - jmp L1 - exit -L4: r3 <- r3 + 2 - r3 <- r3 * r3 - r3 <- 19 * r3 - r3 <- r3 * 11 - r1 <- r1 + 5 - r1 <- r1 * 22 - r1 <- r1 + 2 - r3 <- r3 + r1 - rip <- 25 + r0 - jmp L0 - r1 <- 27 - r1 <- r1 * 28 - r1 <- 29 + r1 - r1 <- 30 * r1 - r1 <- r1 * 14 - r1 <- r1 * 32 - r3 <- r3 + r1 - r0 <- 0 - jmp L0 +r0 = input(); +if r0 == 0: r3 = 948 +elif r0 == 1: r3 = 10551236 +else: assert False + +r0 = sum_of_divisors_of(r3) diff --git a/2018/src/day19.rs b/2018/src/day19.rs index 5d1c23d..b8ded6a 100644 --- a/2018/src/day19.rs +++ b/2018/src/day19.rs @@ -5,6 +5,8 @@ use std::str; // Number of things taken from day16, also edited +const IP_REG: usize = 4; + #[derive(Copy, Clone)] enum RawOpcode { Addr, Addi, @@ -191,22 +193,21 @@ impl Machine { } } - fn exec_program(&mut self, prog: &[Instr], ip_reg: usize) { + fn exec_program(&mut self, prog: &[Instr]) { let mut freq_map = vec![0; prog.len()]; - let mut ip = 0; - while ip < prog.len() { + self.regs[IP_REG] = 0; + while self.regs[IP_REG] < prog.len() { + let ip = self.regs[IP_REG]; freq_map[ip] += 1; if freq_map[ip] >= 3 { let mut sm = SymbolicMachine::from(self); - sm.exec_find_loop(prog, ip_reg, ip); + sm.exec_find_loop(prog, ip); } - self.regs[ip_reg] = ip; println!("{} ip={} {}", self, ip, prog[ip]); self.exec(&prog[ip]); - ip = self.regs[ip_reg]; - ip += 1; + self.regs[IP_REG] += 1; } } @@ -313,14 +314,14 @@ impl SymbolicMachine { self.regs[c] = val; } - fn exec_find_loop(&mut self, prog: &[Instr], ip_reg: usize, start_ip: usize) -> bool { + fn exec_find_loop(&mut self, prog: &[Instr], start_ip: usize) -> bool { let mut ip = start_ip; while ip != start_ip { - self.regs[ip_reg] = SymVal::Val(ip); + self.regs[IP_REG] = SymVal::Val(ip); // println!("{} ip={} {}", self, ip, prog[ip]); self.exec(&prog[ip]); - self.regs[ip_reg].simplify(); - ip = match self.regs[ip_reg] { + self.regs[IP_REG].simplify(); + ip = match self.regs[IP_REG] { SymVal::Val(v) => v, _ => return false, }; @@ -330,23 +331,33 @@ impl SymbolicMachine { } } -fn optimise(mut instrs: Vec<Instr>, ip_reg: usize) -> Vec<Instr> { +fn optimise(mut instrs: Vec<Instr>) -> Vec<Instr> { for i in 0..instrs.len() { - instrs[i].inline_reg(ip_reg, Oper::Num(i)); + instrs[i].inline_reg(IP_REG, Oper::Num(i)); } instrs } -fn run_program(instrs: &[Instr], r0: usize, ip_reg: usize) -> usize { +fn run_program(instrs: &[Instr], r0: usize) -> usize { let mut m = Machine { regs: [r0, 0, 0, 0, 0, 0] }; - m.exec_program(&instrs, ip_reg); + m.exec_program(&instrs); m.regs[0] } +fn divisor_sum(n: i64) -> i64 { + (1..n+1).filter(|&d| n % d == 0).sum() +} + +fn main_alt() -> (String, String) { + (divisor_sum(948).to_string(), divisor_sum(10551236).to_string()) +} + pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> { - let mut lines = reader.lines(); + Ok(main_alt()) + /* let mut lines = reader.lines(); let ip_reg = (lines.next().unwrap().unwrap().as_bytes()[4] - '0' as u8) as usize; + assert!(ip_reg == IP_REG); let instrs = lines.map(|l| Instr::from_raw_instr(&l.unwrap().parse().unwrap()) @@ -354,12 +365,13 @@ pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> { // for instr in &instrs { println!("{}", instr); } - let instrs = optimise(instrs, ip_reg); + let instrs = optimise(instrs); for instr in &instrs { println!("{}", instr); } - let part1 = run_program(&instrs, 0, ip_reg); - let part2 = run_program(&instrs, 1, ip_reg); + let part1 = run_program(&instrs, 0); + // let part2 = run_program(&instrs, 1); - Ok((part1.to_string(), part2.to_string())) + // Ok((part1.to_string(), part2.to_string())) + Ok((String::new(), String::new())) */ } |