Add D5P1 in Rust
This commit is contained in:
parent
ceff5b0eb5
commit
4a1063c522
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "day05"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["kageru <kageru@encode.moe>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1 @@
|
||||||
|
3,225,1,225,6,6,1100,1,238,225,104,0,1002,114,19,224,1001,224,-646,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,40,62,225,1101,60,38,225,1101,30,29,225,2,195,148,224,1001,224,-40,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1001,143,40,224,101,-125,224,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,101,29,139,224,1001,224,-99,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,14,34,225,102,57,39,224,101,-3420,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,70,40,225,1102,85,69,225,1102,94,5,225,1,36,43,224,101,-92,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,1102,94,24,224,1001,224,-2256,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1102,8,13,225,1101,36,65,224,1001,224,-101,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,677,226,224,1002,223,2,223,1006,224,329,1001,223,1,223,1108,226,226,224,1002,223,2,223,1005,224,344,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,359,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,374,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,389,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,404,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,419,101,1,223,223,108,677,226,224,1002,223,2,223,1006,224,434,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,449,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,108,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,7,677,677,224,102,2,223,223,1005,224,494,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,509,101,1,223,223,107,677,226,224,1002,223,2,223,1005,224,524,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,539,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,554,1001,223,1,223,8,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,584,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,614,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,629,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,659,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226
|
|
@ -0,0 +1,112 @@
|
||||||
|
use std::io;
|
||||||
|
use std::io::BufRead;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Operation {
|
||||||
|
Add { x: i32, y: i32, addr: usize },
|
||||||
|
Multiply { x: i32, y: i32, addr: usize },
|
||||||
|
Input { value: i32, addr: usize },
|
||||||
|
Output { value: i32 },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Mode {
|
||||||
|
Immediate,
|
||||||
|
Position,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ParameterPosition {
|
||||||
|
First,
|
||||||
|
Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<usize> for ParameterPosition {
|
||||||
|
fn into(self) -> usize {
|
||||||
|
match self {
|
||||||
|
ParameterPosition::First => 2,
|
||||||
|
ParameterPosition::Second => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Mode> for &char {
|
||||||
|
fn into(self) -> Mode {
|
||||||
|
match self {
|
||||||
|
'0' => Mode::Position,
|
||||||
|
'1' => Mode::Immediate,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_next(input: &Vec<i32>, pos: &mut i32, mode: Mode) -> i32 {
|
||||||
|
let value = input[*pos as usize];
|
||||||
|
//dbg!(&mode, &pos, value);
|
||||||
|
let next = match mode {
|
||||||
|
Mode::Position => input[value as usize],
|
||||||
|
Mode::Immediate => value,
|
||||||
|
};
|
||||||
|
//dbg!(&next);
|
||||||
|
*pos += 1;
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mode(raw_opcode: &Vec<char>, pos: ParameterPosition) -> Mode {
|
||||||
|
raw_opcode.get::<usize>(pos.into()).unwrap_or(&'0').into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_operation(input: &Vec<i32>, pos: &mut i32) -> Option<Operation> {
|
||||||
|
let next = get_next(input, pos, Mode::Immediate);
|
||||||
|
let mut raw_opcode: Vec<_> = next.to_string().chars().collect();
|
||||||
|
raw_opcode.reverse();
|
||||||
|
//dbg!(&raw_opcode);
|
||||||
|
match raw_opcode[0] {
|
||||||
|
'1' => Some(Operation::Add {
|
||||||
|
x: get_next(input, pos, get_mode(&raw_opcode, ParameterPosition::First)),
|
||||||
|
y: get_next(input, pos, get_mode(&raw_opcode, ParameterPosition::Second)),
|
||||||
|
addr: get_next(input, pos, Mode::Immediate) as usize,
|
||||||
|
}),
|
||||||
|
'2' => Some(Operation::Multiply {
|
||||||
|
x: get_next(input, pos, get_mode(&raw_opcode, ParameterPosition::First)),
|
||||||
|
y: get_next(input, pos, get_mode(&raw_opcode, ParameterPosition::Second)),
|
||||||
|
addr: get_next(input, pos, Mode::Immediate) as usize,
|
||||||
|
}),
|
||||||
|
'3' => Some(Operation::Input {
|
||||||
|
value: 1,
|
||||||
|
addr: get_next(input, pos, Mode::Immediate) as usize,
|
||||||
|
}),
|
||||||
|
'4' => Some(Operation::Output {
|
||||||
|
value: get_next(input, pos, get_mode(&raw_opcode, ParameterPosition::First)),
|
||||||
|
}),
|
||||||
|
'9' => None,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute(op: Operation, input: &mut Vec<i32>) {
|
||||||
|
//dbg!(&op);
|
||||||
|
match op {
|
||||||
|
Operation::Add { x, y, addr } => input[addr as usize] = x + y,
|
||||||
|
Operation::Multiply { x, y, addr } => input[addr as usize] = x * y,
|
||||||
|
Operation::Input { value, addr } => input[addr] = value,
|
||||||
|
Operation::Output { value } => println!("{}", value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let mut input: Vec<i32> = io::stdin()
|
||||||
|
.lock()
|
||||||
|
.lines()
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.split(",")
|
||||||
|
.map(|n| n.parse().unwrap())
|
||||||
|
.collect();
|
||||||
|
let mut pos = 0;
|
||||||
|
//dbg!(&input);
|
||||||
|
|
||||||
|
while let Some(op) = next_operation(&input, &mut pos) {
|
||||||
|
execute(op, &mut input);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user