y2015d23 refreshingly easy
This commit is contained in:
parent
daead25413
commit
0b4e2449a1
@ -10,11 +10,11 @@ fn main() {
|
|||||||
fn part1() {
|
fn part1() {
|
||||||
let root = env!("CARGO_MANIFEST_DIR");
|
let root = env!("CARGO_MANIFEST_DIR");
|
||||||
let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap();
|
let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap();
|
||||||
println!("{}", d23::process_part1(&content));
|
println!("{:?}", d23::process_part1(&content));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2() {
|
fn part2() {
|
||||||
let root = env!("CARGO_MANIFEST_DIR");
|
let root = env!("CARGO_MANIFEST_DIR");
|
||||||
let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap();
|
let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap();
|
||||||
println!("{}", d23::process_part2(&content));
|
println!("{:?}", d23::process_part2(&content));
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,135 @@
|
|||||||
enum Instruction {
|
use core::panic;
|
||||||
Half,
|
|
||||||
Triple,
|
|
||||||
Increment,
|
|
||||||
Jump,
|
|
||||||
JumpEven,
|
|
||||||
JumpOne,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn process_part1(input: &str) -> i32 {
|
pub fn process_part1(input: &str) -> (i32, i32) {
|
||||||
let mut a = 0;
|
let mut a = 0;
|
||||||
let mut b = 0;
|
let mut b = 0;
|
||||||
let lines = input
|
let instructions = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.to_string())
|
.map(|line| line.to_string())
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
let mut idx = 0;
|
let mut idx: i32 = 0;
|
||||||
b
|
loop {
|
||||||
|
if let Some(instruction) = instructions.get(idx as usize) {
|
||||||
|
let (instruction_name, instruction_action) = instruction.split_once(" ").unwrap();
|
||||||
|
match instruction_name {
|
||||||
|
"hlf" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a /= 2;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b /= 2;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"tpl" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a *= 3;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b *= 3;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"inc" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a += 1;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b += 1;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"jmp" => {
|
||||||
|
let offset = instruction_action.parse::<i32>().unwrap();
|
||||||
|
idx += offset;
|
||||||
|
}
|
||||||
|
"jie" => {
|
||||||
|
let (reg, offset) = instruction_action.split_once(", ").unwrap();
|
||||||
|
let offset: i32 = offset.parse().unwrap();
|
||||||
|
if (reg == "a" && a % 2 == 0) || (reg == "b" && b % 2 == 0) {
|
||||||
|
idx += offset;
|
||||||
|
} else {
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"jio" => {
|
||||||
|
let (reg, offset) = instruction_action.split_once(", ").unwrap();
|
||||||
|
let offset: i32 = offset.parse().unwrap();
|
||||||
|
if (reg == "a" && a == 1) || (reg == "b" && b == 1) {
|
||||||
|
idx += offset;
|
||||||
|
} else {
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("Unknown instruction {instruction_name}"),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_part2(input: &str) -> i32 {
|
pub fn process_part2(input: &str) -> (i32, i32) {
|
||||||
0
|
let mut a = 1;
|
||||||
|
let mut b = 0;
|
||||||
|
let instructions = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.to_string())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
let mut idx: i32 = 0;
|
||||||
|
loop {
|
||||||
|
if let Some(instruction) = instructions.get(idx as usize) {
|
||||||
|
let (instruction_name, instruction_action) = instruction.split_once(" ").unwrap();
|
||||||
|
match instruction_name {
|
||||||
|
"hlf" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a /= 2;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b /= 2;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"tpl" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a *= 3;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b *= 3;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"inc" => {
|
||||||
|
if instruction_action == "a" {
|
||||||
|
a += 1;
|
||||||
|
} else if instruction_action == "b" {
|
||||||
|
b += 1;
|
||||||
|
}
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
"jmp" => {
|
||||||
|
let offset = instruction_action.parse::<i32>().unwrap();
|
||||||
|
idx += offset;
|
||||||
|
}
|
||||||
|
"jie" => {
|
||||||
|
let (reg, offset) = instruction_action.split_once(", ").unwrap();
|
||||||
|
let offset: i32 = offset.parse().unwrap();
|
||||||
|
if (reg == "a" && a % 2 == 0) || (reg == "b" && b % 2 == 0) {
|
||||||
|
idx += offset;
|
||||||
|
} else {
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"jio" => {
|
||||||
|
let (reg, offset) = instruction_action.split_once(", ").unwrap();
|
||||||
|
let offset: i32 = offset.parse().unwrap();
|
||||||
|
if (reg == "a" && a == 1) || (reg == "b" && b == 1) {
|
||||||
|
idx += offset;
|
||||||
|
} else {
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("Unknown instruction {instruction_name}"),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -34,12 +144,12 @@ inc a";
|
|||||||
#[test]
|
#[test]
|
||||||
fn part1() {
|
fn part1() {
|
||||||
let result = process_part1(INPUT);
|
let result = process_part1(INPUT);
|
||||||
assert_eq!(result, 2);
|
assert_eq!(result.0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part2() {
|
fn part2() {
|
||||||
let result = process_part2(INPUT);
|
let result = process_part2(INPUT);
|
||||||
assert_eq!(result, 0);
|
assert_eq!(result.0, 7);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user