heh
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs89
1 files changed, 3 insertions, 86 deletions
diff --git a/src/main.rs b/src/main.rs
index fe94dab..36bb618 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -40,94 +40,11 @@ pub mod util;
use atools::prelude::*;
pub use util::prelude::*;
-#[derive(Debug)]
-struct Gate<'s> {
- inp: GateTy<'s>,
- run: bool,
-}
-impl<'s> Gate<'s> {
- pub fn new(x: GateTy<'s>) -> Self {
- Gate { inp: x, run: false }
- }
-}
-#[derive(Debug)]
-enum GateTy<'s> {
- Unary(fn(u16) -> u16, &'s str, &'s str),
- Binary(fn(u16, u16) -> u16, [&'s str; 2], &'s str),
- With(fn(u16, u16) -> u16, u16, &'s str, &'s str),
-}
#[no_mangle]
pub fn p1(x: &str) -> impl Display {
- let mut wires = HashMap::default();
- let mut gates = Vec::with_capacity(128);
- let mut ending = None;
- for connection in x.行() {
- if connection.starts_with(b"NOT") {
- let [_, x, _, out] = connection.str().split(' ').carr();
- gates.push(Gate {
- inp: GateTy::Unary(|x| !x, x, out),
- run: false,
- });
- continue;
- }
- let [a, op, out] = connection.μₙ(b' ').carr::<3>();
-
- if op == b"->" {
- if let Ok(x) = a.str().parse::<u16>() {
- wires.insert(out.str(), x);
- } else {
- ending = Some(a.str());
- }
- continue;
- }
- let [a, _, b, _, out] = connection.μₙ(b' ').carr();
- if let Ok(x) = a.str().parse::<u16>() {
- gates.push(Gate {
- inp: GateTy::With(|a, b| a & b, x, b.str(), out.str()),
- run: false,
- });
- continue;
- }
-
- let [a, op, b, _, out] = connection.str().split(' ').carr();
- gates.push(match op {
- "AND" => Gate::new(GateTy::Binary(|a, b| a & b, [a, b], out)),
- "OR" => Gate::new(GateTy::Binary(|a, b| a | b, [a, b], out)),
- "LSHIFT" => Gate::new(GateTy::With(|a, b| a << b, b.λ(), a, out)),
- "RSHIFT" => Gate::new(GateTy::With(|a, b| a >> b, b.λ(), a, out)),
- x => panic!("{}", x),
- });
- }
- // wires.insert("b", 956);
- let mut all_run = false;
- while !all_run {
- all_run = true;
- for gate in &mut gates {
- if gate.run {
- continue;
- };
- match gate.inp {
- GateTy::Unary(op, input, output) => wires.get(input).copied().map(|x| {
- gate.run = true;
- wires.insert(output, op(x))
- }),
- GateTy::Binary(op, [a, b], output) => wires
- .get(a)
- .copied()
- .zip(wires.get(b).copied())
- .map(|(a, b)| {
- gate.run = true;
- wires.insert(output, op(a, b))
- }),
- GateTy::With(op, with, input, output) => wires.get(input).copied().map(|x| {
- gate.run = true;
- wires.insert(output, op(x, with))
- }),
- };
- all_run &= gate.run;
- }
- }
- wires[ending.unwrap()]
+ x.行()
+ .map(|x| format!("{:?}", x.str()).len() - x.len())
+ .sum::<usize>()
}
fn main() {