not working y2015d7

This commit is contained in:
2024-10-28 16:00:35 +01:00
parent a4ef4dfacf
commit 742f35b73e
4 changed files with 473 additions and 0 deletions

20
y2015/src/bin/d7.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::fs;
use y2015::days::d7;
fn main() {
part1();
part2();
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/7_input.txt")).unwrap();
println!("{}", d7::process_part1(&content).get("a").unwrap());
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/7_input.txt")).unwrap();
println!("{:#?}", d7::process_part2(&content));
}

113
y2015/src/days/d7.rs Normal file
View File

@@ -0,0 +1,113 @@
use core::panic;
use std::collections::HashMap;
pub fn process_part1(input: &str) -> HashMap<String, u16> {
let mut wires = HashMap::new();
let mut instructions = HashMap::new();
for instruction in input.lines() {
let (expression, wire) = instruction.split_once(" -> ").unwrap();
instructions.insert(wire.to_string(), expression.to_string());
}
for wire in instructions.keys() {
set_wire(wire.to_owned(), &mut wires, &instructions);
}
wires
}
fn set_wire(
wire: String,
wires: &mut HashMap<String, u16>,
instructions: &HashMap<String, String>,
) -> u16 {
if let Some(value) = wires.get(&wire) {
return *value;
}
let expression = instructions.get(&wire).unwrap().to_string();
if let Ok(num) = expression.parse() {
wires.insert(wire, num);
return num;
}
let parts: Vec<_> = expression
.split_whitespace()
.map(|part| part.to_string())
.collect();
if parts.len() == 1 {
let value = set_wire(parts[0].clone(), wires, instructions);
wires.insert(wire, value);
return value;
}
if parts.len() == 2 {
let value = set_wire(parts[1].clone(), wires, instructions);
wires.insert(wire, !value);
return value;
}
if parts.len() == 3 {
let left = match parts[0].parse::<u16>() {
Ok(num) => num,
Err(_) => {
let value = set_wire(parts[0].clone(), wires, instructions);
wires.insert(wire.clone(), !value);
value
}
};
let right = match parts[2].parse::<u16>() {
Ok(num) => num,
Err(_) => {
let value = set_wire(parts[2].clone(), wires, instructions);
wires.insert(wire.clone(), !value);
value
}
};
match parts[1].as_str() {
"AND" => {
wires.insert(wire, left & right);
return left & right;
}
"OR" => {
wires.insert(wire, left | right);
return left | right;
}
"LSHIFT" => {
wires.insert(wire, left << right);
return left << right;
}
"RSHIFT" => {
wires.insert(wire, left >> right);
return left >> right;
}
_ => panic!("Should not happen"),
}
}
panic!("Should not happen");
}
pub fn process_part2(_input: &str) {}
#[cfg(test)]
mod tests_7 {
use super::*;
const INPUT: &str = "123 -> x
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i
456 -> y";
#[test]
fn it_works() {
let mut expected = HashMap::new();
expected.insert("d".to_string(), 72);
expected.insert("e".to_string(), 507);
expected.insert("f".to_string(), 492);
expected.insert("g".to_string(), 114);
expected.insert("h".to_string(), 65412);
expected.insert("i".to_string(), 65079);
expected.insert("x".to_string(), 123);
expected.insert("y".to_string(), 456);
let result = process_part1(INPUT);
assert_eq!(result, expected);
}
}

View File

@@ -4,3 +4,4 @@ pub mod d3;
pub mod d4;
pub mod d5;
pub mod d6;
pub mod d7;