diff --git a/2023/8/Cargo.lock b/2023/8/Cargo.lock deleted file mode 100644 index 0f60e5c..0000000 --- a/2023/8/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "eight" -version = "0.1.0" diff --git a/2023/8/Cargo.toml b/2023/8/Cargo.toml deleted file mode 100644 index 3000071..0000000 --- a/2023/8/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "eight" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/2023/8/part2.py b/2023/8/part2.py index 29f8795..297be73 100644 --- a/2023/8/part2.py +++ b/2023/8/part2.py @@ -9,43 +9,16 @@ for line in open("./input2"): instructions = [(0 if x == "L" else 1) for x in line.strip()] continue if line == "\n": continue - name = line.split(" ")[0].strip() - leftnode = line.split("(")[1].split(",")[0].strip() - rightnode = line.split("(")[1].split(",")[1].split(")")[0].strip() - nodes[name] = (leftnode, rightnode) + nodes[line.split(" ")[0].strip()] = line.split("(")[1].split(")")[0].split(", ") -# count = 0 -current = [(node, []) for node in nodes.keys() if node.endswith("A")] - -for i, node in enumerate(current): +def node_value(node): instructionsc = cycle(instructions) - instruction_offset = 0 - nnode = node[0] - zets = [] steps = 0 - while not nnode.endswith("Z"): - nnode = nodes[nnode][next(instructionsc)] + while not node.endswith("Z"): + node = nodes[node][next(instructionsc)] steps += 1 - # if not steps - 1 in zets: - # zets.append(steps - 1) - current[i] = (node[0], steps) - print(node[0], steps) + return steps -print(current) +current = [node_value(node) for node in nodes.keys() if node.endswith("A")] -values = [node[1] for node in current] - -print(values) - -# num = 0 -# lowest_value = min(value[1] for value in values) -# print("low", lowest_value) - -# while True: -# num += lowest_value -# # print(num) -# if all([num % value[1] == value[0] for value in values]): -# print("Found", num) -# break - -print(math.lcm(*[node for node in values])) \ No newline at end of file +print(math.lcm(*current)) \ No newline at end of file diff --git a/2023/8/src/main.rs b/2023/8/src/main.rs deleted file mode 100644 index a6a23ff..0000000 --- a/2023/8/src/main.rs +++ /dev/null @@ -1,122 +0,0 @@ -use std::{io::{BufReader, BufRead}, fs::File, ops::Index}; -use std::collections::HashMap; -use std::sync::mpsc; - -struct Node { - left: String, - right: String -} -enum Instruction { - Left, - Right -} - -fn main() { - let values = [[18156, 36313], [11652, 23305], [21408, 42817], [12736, 25473], [14362, 28725], [15988, 31977]]; - let lowest_value = values.iter().map(|v| v[1]).min().unwrap(); - let mut num: i64 = values[1][0]; - let mut printcount = 0; - dbg!(lowest_value); - - 'main: loop { - num += lowest_value; - for value in &values { - if num % value[1] != value[0] { - printcount += 1; - if printcount % 100000000 == 0 { - dbg!(num); - } - // if value[0] != 18156 { - // dbg!(num, value[1], value[0], num % value[1]); - // } - continue 'main; - } - } - break; - } - - dbg!(num); - - // let file = BufReader::new(File::open("./input2").unwrap()); - // let mut instructions = vec![]; - // let mut nodes = HashMap::new(); - - // for line in file.lines() { - // let line = line.unwrap(); - // if instructions.is_empty() { - // let chars = line.chars().collect::>(); - // // L = Left, R = Right - // for c in chars { - // match c { - // 'L' => instructions.push(Instruction::Left), - // 'R' => instructions.push(Instruction::Right), - // _ => () - // } - // } - // continue; - // } - // if line.is_empty() { - // continue; - // } - // let name = line.split(" ").next().unwrap(); - // let left = line.split("(").nth(1).unwrap().split(",").next().unwrap(); - // let right = line.split("(").nth(1).unwrap().split(",").nth(1).unwrap().strip_suffix(")").unwrap().trim(); - - // nodes.insert(name.to_string(), Node { left: left.to_string(), right: right.to_string() }); - // } - - // let mut instruction_pointer = 0; - // let mut current_nodes = nodes.keys().filter(|k| k.ends_with("A")).collect::>(); - // /// Array of arrays corresponding to current_nodes, with each value in the array being the step in which the node reaches Z node - // let mut z_values = Arc::new(RwLock::new(vec![Mutex::new(vec![]); current_nodes.len()])); - - // dbg!(¤t_nodes); - - // let mut count = 0; - - // let z_values2 = z_values.clone(); - // let main_join_thread = thread::spawn(move || { - // // If there's a common number between z_values, stop and return - // loop { - // let z_values = z_values2.read().unwrap(); - // let mut common_values = z_values.get(0).unwrap().lock().unwrap().clone(); - // for i in 1..z_values.len() { - // let values = z_values.get(i).unwrap().lock().unwrap().clone(); - // common_values = common_values.iter().filter(|v| values.contains(v)).collect::>(); - // } - // if !common_values.is_empty() { - // return common_values[0].clone(); - // } - // } - // }); - - // let mut offset = 0; - // for node in current_nodes { - // thread::spawn(move || { - // let mut node = node; - // let offset = offset; - // }); - // offset += 1; - // } - - // let val = main_join_thread.join().unwrap(); - // dbg!(val); - - // while !current_nodes.iter().all(|n| n.ends_with("Z")) { - // let mut next_nodes = vec![]; - // for node in current_nodes { - // let node = nodes.get(node).unwrap(); - // match instructions[instruction_pointer] { - // Instruction::Left => next_nodes.push(&node.left), - // Instruction::Right => next_nodes.push(&node.right) - // } - // } - // instruction_pointer += 1; - // instruction_pointer %= instructions.len(); - // count += 1; - // current_nodes = next_nodes; - // } - - // dbg!(count); - // println!("{count}"); -}