Skip to content

Commit

Permalink
day02: simplify slightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jld committed Dec 21, 2019
1 parent 8fea6e1 commit eff6946
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions day02/src/main.rs
Expand Up @@ -6,22 +6,20 @@ use std::process::exit;

use intcode::{Computer, Word};

type Num = Word;

fn compute(mem: Vec<Num>, noun: Num, verb: Num) -> Num {
fn compute(mem: Vec<Word>, noun: Word, verb: Word) -> Word {
let mut cpu = Computer::new(mem.into_iter().collect());
cpu.write(1, noun).unwrap();
cpu.write(2, verb).unwrap();
cpu.run(&mut ()).unwrap();
return cpu.read(0).unwrap();
}

fn part1(mem: Vec<Num>) {
fn part1(mem: Vec<Word>) {
println!("{}", compute(mem, 12, 2));
}

fn part2(mem: Vec<Num>) {
const MOON: Num = 19690720;
fn part2(mem: Vec<Word>) {
const MOON: Word = 19690720;

for noun in 0..=99 {
for verb in 0..=99 {
Expand All @@ -36,12 +34,12 @@ fn part2(mem: Vec<Num>) {

fn main() {
let stdin = stdin();
let mem: Vec<Num> =
let mem: Vec<Word> =
stdin.lock()
.split(b',')
.map(|r| r.expect("I/O error reading stdin"))
.map(|b| String::from_utf8(b).expect("encoding error on stdin"))
.map(|s| Num::from_str(s.trim())
.map(|s| Word::from_str(s.trim())
.unwrap_or_else(|e| panic!("bad number {:?}: {}", s, e)))
.collect();
match args().nth(1).as_ref().map(|s| s as &str) { // sigh
Expand Down

0 comments on commit eff6946

Please sign in to comment.