summaryrefslogtreecommitdiff
path: root/2018/src
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2018-12-21 20:22:02 +0100
committertomsmeding <tom.smeding@gmail.com>2018-12-21 20:22:02 +0100
commit7dc5aa8bca7699c406c3dacd9e24c26cf74ca510 (patch)
tree9a4b94125812a10fe93e85b5e9c9f52c19e90e9f /2018/src
parenta091837040819f25df4f99176820e6ede0687780 (diff)
Day 19 part 2 WIP
Diffstat (limited to '2018/src')
-rw-r--r--2018/src/day19.rs52
1 files changed, 32 insertions, 20 deletions
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())) */
}