2024: clippies
This commit is contained in:
7 files changed
+69
-41
No files matched your search
+19
-18
@@ -1,9 +1,9 @@
|
||||
use std::{borrow::BorrowMut, collections::VecDeque, fmt::Display, fs::File, io::Write, mem::swap};
|
||||
use std::{fmt::Display, fs::File, io::Write};
|
||||
|
||||
use aoc_runner_derive::aoc;
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
enum Op {
|
||||
@@ -78,13 +78,13 @@ impl GateMachine {
|
||||
.keys()
|
||||
.filter(|k| k.starts_with(reg))
|
||||
.map(|s| (s, s.split_at(1).1.parse::<usize>().unwrap()));
|
||||
let val = reg_gates
|
||||
|
||||
reg_gates
|
||||
.map(|(name, bit)| if self.val_of(name) { 1 << bit } else { 0 })
|
||||
.fold(0, |accum, val| accum | val);
|
||||
val
|
||||
.fold(0, |accum, val| accum | val)
|
||||
}
|
||||
fn expected_result(&self) -> u64 {
|
||||
self.register('x') + self.register('y') & (1 << self.out_bits) - 1
|
||||
(self.register('x') + self.register('y')) & ((1 << self.out_bits) - 1)
|
||||
}
|
||||
fn actual_result(&self) -> u64 {
|
||||
self.register('z')
|
||||
@@ -105,17 +105,18 @@ impl GateMachine {
|
||||
self.gates.insert(b.to_string(), a_tmp);
|
||||
}
|
||||
// return the gates that this gate depends on
|
||||
#[allow(unused)]
|
||||
fn dependencies(&self, gate: &str) -> Vec<&str> {
|
||||
if let Some((key, arg)) = self.gates.get_key_value(gate) {
|
||||
let mut deps = vec![key.as_str()];
|
||||
for dep in &arg.arguments {
|
||||
if dep != "" && self.gates[dep].op != Op::Constant {
|
||||
if !dep.is_empty() && self.gates[dep].op != Op::Constant {
|
||||
deps.push(dep.as_str());
|
||||
deps.extend(self.dependencies(&dep).iter().copied())
|
||||
deps.extend(self.dependencies(dep).iter().copied())
|
||||
}
|
||||
}
|
||||
deps
|
||||
} else if gate != "" {
|
||||
} else if !gate.is_empty() {
|
||||
panic!("tried to look up invalid gate `{}`", gate);
|
||||
} else {
|
||||
Vec::new()
|
||||
@@ -123,6 +124,7 @@ impl GateMachine {
|
||||
}
|
||||
// return the bits that this gate affects
|
||||
// fn dependents(&self, gate: &str) -> Vec<&str> {}
|
||||
#[allow(unused)]
|
||||
fn reset(&mut self) {
|
||||
for gate in self.gates.values_mut().filter(|g| g.op != Op::Constant) {
|
||||
gate.value = None
|
||||
@@ -132,10 +134,9 @@ impl GateMachine {
|
||||
|
||||
fn parse(input: &str) -> GateMachine {
|
||||
let mut gates = FxHashMap::default();
|
||||
let gate_re = Regex::new(r"^([a-z0-9]{3}) (AND|XOR|OR) ([a-z0-9]{3}) -> ([a-z0-9]{3})$").unwrap();
|
||||
let const_re = Regex::new(r"^([xyz][0-9]{2}): ([01])$").unwrap();
|
||||
for line in input.lines() {
|
||||
let const_re = Regex::new(r"^([xyz][0-9]{2}): ([01])$").unwrap();
|
||||
let gate_re = Regex::new(r"^([a-z0-9]{3}) (AND|XOR|OR) ([a-z0-9]{3}) -> ([a-z0-9]{3})$").unwrap();
|
||||
|
||||
if let Some(caps) = const_re.captures(line) {
|
||||
gates.insert(
|
||||
caps[1].to_string(),
|
||||
@@ -182,7 +183,7 @@ pub fn part1(input: &str) -> u64 {
|
||||
}
|
||||
|
||||
#[aoc(day24, part2)]
|
||||
pub fn part2(input: &str) -> String {
|
||||
pub fn part2(input: &str) -> Result<String, std::io::Error> {
|
||||
let mut machine = parse(input);
|
||||
|
||||
let swaps = vec![("z06", "ksv"), ("nbd", "kbs"), ("z20", "tqq"), ("z39", "ckb")];
|
||||
@@ -204,7 +205,7 @@ pub fn part2(input: &str) -> String {
|
||||
|
||||
let mut file = File::create("day24.dot").unwrap();
|
||||
writeln!(file, "graph day24 {{").unwrap();
|
||||
writeln!(file, " label=\"first error at bit {}\"", first_error);
|
||||
writeln!(file, " label=\"first error at bit {}\"", first_error)?;
|
||||
|
||||
for (name, gate) in machine.gates {
|
||||
if gate.op != Op::Constant {
|
||||
@@ -215,11 +216,11 @@ pub fn part2(input: &str) -> String {
|
||||
Op::Xor => " [color=red]",
|
||||
_ => "",
|
||||
};
|
||||
writeln!(file, "{} -- {} {};", name, arg, color);
|
||||
writeln!(file, "{} -- {} {};", name, arg, color)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln!(file, "}}");
|
||||
writeln!(file, "}}")?;
|
||||
|
||||
// let mut test_machine = machine.clone();
|
||||
// let mut swaps_left = 4;
|
||||
@@ -239,7 +240,7 @@ pub fn part2(input: &str) -> String {
|
||||
.collect_vec();
|
||||
answer.sort();
|
||||
|
||||
answer.iter().join(",")
|
||||
Ok(answer.iter().join(","))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -332,6 +333,6 @@ x05 AND y05 -> z00";
|
||||
#[ignore = "not solved"]
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(part2(EXAMPLE3), "z00,z01,z02,z05");
|
||||
assert_eq!(part2(EXAMPLE3).unwrap(), "z00,z01,z02,z05");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user