Skip to content

Commit

Permalink
bruh rust borrow checker is so mean
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-the-Bomb committed Dec 22, 2023
1 parent 1f08f46 commit 014494b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 17 deletions.
8 changes: 4 additions & 4 deletions aoc-py/solutions/day20.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def _parse_input(self, inp: str) -> tuple[dict[str, Module], list[str]]:
# defaulting to false to begin
module.memory[name] = False
return modules, broadcast_targets

def _run_modules(self, destinations: list[Destination], module: Module, source: str, pulse: bool) -> None:
"""Runs a single iteration of all the modules' pulse sending process"""

if isinstance(module, Flipper) and not pulse:
module.status = not module.status
pulse_to_send = module.status
Expand Down Expand Up @@ -176,8 +176,8 @@ def part_two(self, inp: str) -> int:
seen[name := source.name] = True

if name not in press_amounts:
press_amounts[name] = n_presses
press_amounts[name] = n_presses

if all(seen.values()):
# we have checked all the modules that feed into the module
# we can then return the result
Expand Down
26 changes: 13 additions & 13 deletions src/bin/day19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ use std::{
};
use aoc_2023::Solution;

pub struct Day19;

#[derive(Debug, Clone)]
struct Rule<'a> {
pub key: &'a str,
pub target: &'a str,
pub rhs: usize,
pub is_gt: bool,
pub condition: fn(&usize, &usize) -> bool,
key: &'a str,
target: &'a str,
rhs: usize,
is_gt: bool,
condition: fn(&usize, &usize) -> bool,
}

impl<'a> Rule<'a> {
Expand All @@ -29,8 +27,8 @@ impl<'a> Rule<'a> {

#[derive(Debug, Clone)]
struct Workflow<'a> {
pub default: &'a str,
pub rules: Vec<Rule<'a>>,
default: &'a str,
rules: Vec<Rule<'a>>,
}

impl<'a> Workflow<'a> {
Expand All @@ -54,7 +52,7 @@ impl<'a> Workflow<'a> {
if is_gt { '>' } else { '<' }
)
.unwrap();

self.rules.push(Rule {
key, target, is_gt,
rhs: rhs.parse::<usize>()
Expand All @@ -66,6 +64,8 @@ impl<'a> Workflow<'a> {
}
}

pub struct Day19;

impl Day19 {
fn parse_workflows<T>(raw: &T) -> HashMap<String, Workflow<'_>>
where
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Day19 {
}

/// # Panics
///
///
/// If failed to parse input (malformed)
pub fn part_one<T: Display>(&self, inp: T) -> usize {
let inp = inp
Expand All @@ -219,9 +219,9 @@ impl Day19 {
)
.sum()
}

/// # Panics
///
///
/// If failed to parse input (malformed)
pub fn part_two<T: Display>(&self, inp: T) -> usize {
let inp = inp
Expand Down
147 changes: 147 additions & 0 deletions src/bin/day20.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//! Day 20: Pulse Propagation
//!
//! <https://adventofcode.com/2023/day/20>
use std::{
collections::{VecDeque, HashMap},
fmt::Display, os::windows::io::BorrowedHandle,
};
use aoc_2023::Solution;

#[derive(Debug, Clone)]
struct Destination {
name: String,
target: String,
pulse: bool,
}

#[derive(Debug, Clone)]
enum Module<'a> {
Flipper {
name: &'a str,
outputs: Vec<String>,
status: bool,
},
Conjunction {
name: &'a str,
outputs: Vec<String>,
memory: HashMap<String, bool>,
}
}

impl<'a> Module<'a> {
fn new(raw: &'a str, outputs: Vec<String>) -> Self {
match raw
.split_at(1)
{
("%", name) => Self::Flipper {
name, outputs,
status: false,
},
("&", name) => Self::Conjunction {
name, outputs,
memory: HashMap::new(),
},
_ => panic!("Invalid module prefix"),
}
}

fn name(&self) -> &'a str {
match self {
Self::Flipper { name, .. } => name,
Self::Conjunction { name, .. } => name,
}
}

fn outputs(&self) -> &Vec<String> {
match self {
Self::Flipper { outputs, .. } => outputs,
Self::Conjunction { outputs, .. } => outputs,
}
}
}

pub struct Day20;

impl Day20 {
fn parse_input<T>(inp: &T) -> (HashMap<String, Module<'_>>, Vec<String>)
where
T: AsRef<str>,
{
let mut modules = HashMap::new();
let mut broadcast_targets = Vec::new();

for line in inp
.as_ref()
.lines()
{
let (name, targets) = line
.split_once("->")
.unwrap();
let targets = targets
.split(',')
.map(|s| s.trim().to_string())
.collect::<Vec<String>>();
match name.trim() {
"broadcaster" => broadcast_targets = targets,
name => {
modules.insert(
name.to_string(),
Module::new(name, targets),
);
}
}
}

for (name, module) in &modules {
for output in module
.outputs()
.iter()
.filter_map(|output| modules
.get_mut(output)
)
{
if let Module::Conjunction { memory, .. } = output {
memory.insert(
name.clone(),
false
);
}
}
}
(modules, broadcast_targets)
}
pub fn part_one<T: Display>(&self, inp: T) -> usize {
todo!()
}

pub fn part_two<T: Display>(&self, inp: T) -> usize {
todo!()
}
}

impl Solution for Day20 {
const NAME: &'static str = "Pulse Propagation";

fn run(&self, inp: String) {
let p1 = self.part_one(&inp);
let p2 = self.part_two(&inp);

println!("Part 1: {p1}");
println!("Part 2: {p2}");

assert_eq!(p1, 680_278_040);
assert_eq!(p2, 243_548_140_870_057);
}
}

fn main() {
aoc_2023::run_day(20, &Day20);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test() { main(); }
}

0 comments on commit 014494b

Please sign in to comment.