y2024d16p2 not done
This commit is contained in:
parent
466d79e81a
commit
c2d5545139
@ -1,9 +1,9 @@
|
||||
use core::panic;
|
||||
use std::{char, error::Error};
|
||||
use std::{char, collections::HashMap, error::Error, sync::mpsc};
|
||||
|
||||
use itertools::Itertools;
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
|
||||
pub fn process_part1(input: &str) -> usize {
|
||||
let mut memo = HashMap::new();
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
@ -21,6 +21,24 @@ pub fn process_part1(input: &str) -> usize {
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn extend_sequence(
|
||||
sequence_parts: &[Vec<String>],
|
||||
memo: &mut HashMap<(char, char), Vec<String>>,
|
||||
) -> Vec<Vec<String>> {
|
||||
sequence_parts.iter().map(|possible_parts| {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let mut new_possible_parts = Vec::new();
|
||||
possible_parts.par_iter().for_each(|part| {
|
||||
let _ = tx.send();
|
||||
});
|
||||
drop(tx);
|
||||
while let Ok(part) = rx.recv() {
|
||||
new_possible_parts.extend_from_slice(part);
|
||||
}
|
||||
new_possible_parts
|
||||
})
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> usize {
|
||||
0
|
||||
}
|
||||
@ -63,13 +81,13 @@ impl NumpadState {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sequence(&self, goto: NumpadState) -> String {
|
||||
fn get_sequences(&self, goto: NumpadState) -> Vec<String> {
|
||||
let row = self.get_row();
|
||||
let col = self.get_col();
|
||||
let goto_row = goto.get_row();
|
||||
let goto_col = goto.get_col();
|
||||
if row == goto_row && col == goto_col {
|
||||
return "A".to_string();
|
||||
return vec!["A".to_string()];
|
||||
}
|
||||
let row_diff = row.abs_diff(goto_row);
|
||||
let col_diff = col.abs_diff(goto_col);
|
||||
@ -160,71 +178,6 @@ impl From<NumpadState> for char {
|
||||
}
|
||||
}
|
||||
|
||||
struct Numpad {
|
||||
state: NumpadState,
|
||||
sequence: String,
|
||||
}
|
||||
|
||||
impl Numpad {
|
||||
fn press_key(&mut self, goto: NumpadState) {
|
||||
let sequence = self.state.get_sequence(goto);
|
||||
self.state = goto;
|
||||
self.sequence.push_str(&sequence);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn revert_from(sequence: &str) -> String {
|
||||
let mut state = NumpadState::A;
|
||||
let mut col_idx = 2;
|
||||
let mut row_idx = 3;
|
||||
let mut reverted = String::new();
|
||||
for movements in sequence.split_inclusive("A") {
|
||||
for movement in movements.chars() {
|
||||
match movement {
|
||||
'<' => {
|
||||
col_idx -= 1;
|
||||
state = NumpadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'>' => {
|
||||
col_idx += 1;
|
||||
state = NumpadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'^' => {
|
||||
row_idx -= 1;
|
||||
state = NumpadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'v' => {
|
||||
row_idx += 1;
|
||||
state = NumpadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'A' => {
|
||||
reverted.push(state.into());
|
||||
}
|
||||
_ => panic!("Impossible"),
|
||||
}
|
||||
}
|
||||
}
|
||||
reverted
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Numpad {
|
||||
fn from(value: &str) -> Self {
|
||||
let mut numpad = Numpad {
|
||||
state: NumpadState::A,
|
||||
sequence: String::new(),
|
||||
};
|
||||
let keys = value
|
||||
.chars()
|
||||
.map(|numkey| NumpadState::try_from(numkey).unwrap())
|
||||
.collect_vec();
|
||||
for key in keys {
|
||||
numpad.press_key(key);
|
||||
}
|
||||
numpad
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum KeypadState {
|
||||
Up,
|
||||
@ -250,13 +203,13 @@ impl KeypadState {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sequence(&self, goto: KeypadState) -> String {
|
||||
fn get_sequence(&self, goto: KeypadState) -> Vec<String> {
|
||||
let row = self.get_row();
|
||||
let col = self.get_col();
|
||||
let goto_row = goto.get_row();
|
||||
let goto_col = goto.get_col();
|
||||
if row == goto_row && col == goto_col {
|
||||
return "A".to_string();
|
||||
return vec!["A".to_string()];
|
||||
}
|
||||
let row_diff = row.abs_diff(goto_row);
|
||||
let col_diff = col.abs_diff(goto_col);
|
||||
@ -329,71 +282,6 @@ impl From<KeypadState> for char {
|
||||
}
|
||||
}
|
||||
|
||||
struct Keypad {
|
||||
state: KeypadState,
|
||||
sequence: String,
|
||||
}
|
||||
|
||||
impl Keypad {
|
||||
fn press_key(&mut self, goto: KeypadState) {
|
||||
let sequence = self.state.get_sequence(goto);
|
||||
self.state = goto;
|
||||
self.sequence.push_str(&sequence);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn revert_from(sequence: &str) -> String {
|
||||
let mut state = KeypadState::A;
|
||||
let mut col_idx = 2;
|
||||
let mut row_idx = 0;
|
||||
let mut reverted = String::new();
|
||||
for movements in sequence.split_inclusive("A") {
|
||||
for movement in movements.chars() {
|
||||
match movement {
|
||||
'<' => {
|
||||
col_idx -= 1;
|
||||
state = KeypadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'>' => {
|
||||
col_idx += 1;
|
||||
state = KeypadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'^' => {
|
||||
row_idx -= 1;
|
||||
state = KeypadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'v' => {
|
||||
row_idx += 1;
|
||||
state = KeypadState::try_from((col_idx, row_idx)).unwrap()
|
||||
}
|
||||
'A' => {
|
||||
reverted.push(state.into());
|
||||
}
|
||||
_ => panic!("Impossible"),
|
||||
}
|
||||
}
|
||||
}
|
||||
reverted
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Keypad {
|
||||
fn from(value: &str) -> Self {
|
||||
let mut numpad = Keypad {
|
||||
state: KeypadState::A,
|
||||
sequence: String::new(),
|
||||
};
|
||||
let keys = value
|
||||
.chars()
|
||||
.map(|numkey| KeypadState::try_from(numkey).unwrap())
|
||||
.collect_vec();
|
||||
for key in keys {
|
||||
numpad.press_key(key);
|
||||
}
|
||||
numpad
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -448,79 +336,6 @@ mod tests {
|
||||
assert_eq!(result, 64 * 379);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_edge() {
|
||||
let code = "1";
|
||||
let numpad = Numpad::from(code);
|
||||
let keypad_1 = Keypad::from(numpad.sequence.as_str());
|
||||
assert_eq!(numpad.sequence, "^<<A");
|
||||
assert_eq!(keypad_1.sequence, "<Av<AA>>^A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_1_back() {
|
||||
let result = Keypad::revert_from(INPUT_1_BACK);
|
||||
println!("back 1 {result}");
|
||||
let result = Keypad::revert_from(&result);
|
||||
println!("back 2 {result}");
|
||||
let result = Numpad::revert_from(&result);
|
||||
assert_eq!(result, "029A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_2_back() {
|
||||
let result = Keypad::revert_from(INPUT_2_BACK);
|
||||
println!("back 1 {result}");
|
||||
let result = Keypad::revert_from(&result);
|
||||
println!("back 2 {result}");
|
||||
let result = Numpad::revert_from(&result);
|
||||
assert_eq!(result, "980A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_3_back() {
|
||||
let result = Keypad::revert_from(INPUT_3_BACK);
|
||||
println!("back 1 {result}");
|
||||
let result = Keypad::revert_from(&result);
|
||||
println!("back 2 {result}");
|
||||
let result = Numpad::revert_from(&result);
|
||||
assert_eq!(result, "179A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_4_back() {
|
||||
let result = Keypad::revert_from(INPUT_4_BACK);
|
||||
println!("back 1 {result}");
|
||||
let result = Keypad::revert_from(&result);
|
||||
println!("back 2 {result}");
|
||||
let result = Numpad::revert_from(&result);
|
||||
assert_eq!(result, "456A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_5_back() {
|
||||
let result = Keypad::revert_from(INPUT_5_BACK);
|
||||
println!("back 1 {result}");
|
||||
let result = Keypad::revert_from(&result);
|
||||
println!("back 2 {result}");
|
||||
let result = Numpad::revert_from(&result);
|
||||
assert_eq!(result, "379A");
|
||||
}
|
||||
|
||||
//#[test]
|
||||
//fn part1_5_backnforth() {
|
||||
// let numpad = Numpad::from(INPUT_5);
|
||||
// let keypad_1 = Keypad::from(numpad.sequence.as_str());
|
||||
// let keypad_2 = Keypad::from(keypad_1.sequence.as_str());
|
||||
// let keypad_1_back = Keypad::revert_from(INPUT_5_BACK);
|
||||
// let numpad_back = Keypad::revert_from(&keypad_1_back);
|
||||
// let code = Numpad::revert_from(&numpad_back);
|
||||
// //assert_eq!(code, INPUT_5);
|
||||
// assert_eq!(numpad_back, numpad.sequence);
|
||||
// //assert_eq!(keypad_1_back, keypad_1.sequence);
|
||||
// //assert_eq!(INPUT_5_BACK, keypad_2.sequence);
|
||||
//}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT_1);
|
||||
|
Loading…
Reference in New Issue
Block a user