more progress.
over 40 stars!
This commit is contained in:
@@ -1,15 +1,31 @@
|
||||
use std::io::{stdout, Write};
|
||||
use md5::{Digest, Md5};
|
||||
|
||||
fn has_8_chars(input: &Vec<char>) -> bool {
|
||||
let mut num_spaces = 0;
|
||||
for ch in input {
|
||||
if *ch == ' ' { num_spaces += 1 }
|
||||
}
|
||||
num_spaces == 0
|
||||
}
|
||||
|
||||
fn display_current_password(input: &Vec<char>) {
|
||||
print!("\x1b[2K\rPassword: ");
|
||||
for char in input {
|
||||
if *char == ' ' { print!("_"); } else { print!("{}", char) };
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = "wtnhxymk";
|
||||
let mut password = vec![' '; 8];
|
||||
let mut last_password_displayed = password.clone();
|
||||
|
||||
for index in 2231253..=u32::MAX {
|
||||
let to_hash = format!("{}{}", input, index);
|
||||
let as_hash = format!("{:x}", Md5::digest(to_hash.as_bytes()));
|
||||
if as_hash.starts_with("00000") {
|
||||
println!("Found hash with {index} -> {as_hash}");
|
||||
// println!("Found hash with {index} -> {as_hash}");
|
||||
let ( sixth,balance ) = as_hash.as_str().split_at(5).1.split_at(1);
|
||||
let ( seventh, _) = balance.split_at(1);
|
||||
stdout().flush().unwrap();
|
||||
@@ -17,8 +33,15 @@ fn main() {
|
||||
if index_for_new_char < 9 && password[index_for_new_char as usize] == ' ' {
|
||||
password[index_for_new_char as usize] = seventh.parse().unwrap();
|
||||
}
|
||||
println!("Index for new char = {index_for_new_char} / Password [{password:?}] / Seventh {seventh}"); // bail out if we have 8 characters
|
||||
|
||||
let mut to_disp = String::new();
|
||||
for char in &password {
|
||||
to_disp.push(*char);
|
||||
}
|
||||
if password != last_password_displayed {
|
||||
last_password_displayed = password.clone();
|
||||
display_current_password(&last_password_displayed);
|
||||
}
|
||||
if has_8_chars(&password.clone()) { break }
|
||||
}
|
||||
}
|
||||
println!("\nPassword is [{password:?}]");
|
||||
|
||||
@@ -17,11 +17,42 @@
|
||||
// larger string).
|
||||
// How many IPs in your puzzle input support TLS?
|
||||
|
||||
fn has_tls(input: &str) -> bool {
|
||||
true
|
||||
use core::read_data;
|
||||
fn has_abba(input: &str) -> bool {
|
||||
let chars: Vec<char> = input.chars().collect();
|
||||
|
||||
for i in 0..chars.len().saturating_sub(3) {
|
||||
if chars[i] != chars[i + 1] &&
|
||||
chars[i] == chars[i + 3] &&
|
||||
chars[i + 1] == chars[i + 2] {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn extract_hypernet(input: &str) -> (String, String) {
|
||||
let mut payload = String::new();
|
||||
let mut real_balance = String::from(input);
|
||||
while real_balance.contains('[') {
|
||||
let (prefix, balance) = real_balance.split_once('[').unwrap();
|
||||
let (tmp, t_balance) = balance.split_once(']').unwrap();
|
||||
payload = format!("{}{}", payload, tmp.to_string());
|
||||
real_balance = format!("{}{}", prefix, t_balance);
|
||||
// extract the text between the '[' and ']' characters
|
||||
}
|
||||
(real_balance, payload.to_string())
|
||||
}
|
||||
|
||||
fn has_tls(address: &str, hypernet: &str) -> bool {
|
||||
let abba = has_abba(address);
|
||||
let noabba = !has_abba(hypernet);
|
||||
println!("[{address}] ABBA: {abba} [{hypernet}] NOABBA: {noabba}");
|
||||
abba && noabba
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut num_good = 0;
|
||||
let params = vec![
|
||||
("abba[mnop]qrst", true),
|
||||
("abcd[bddb]xyyx", false),
|
||||
@@ -30,6 +61,23 @@ fn main() {
|
||||
];
|
||||
|
||||
for (input, expected) in params {
|
||||
assert_eq!(has_tls(input), expected)
|
||||
println!("------------------Testing [[{input}]]");
|
||||
let (address, hypernet) = extract_hypernet(input);
|
||||
println!("[[{input}]] = ADD: {address} HN: {hypernet}");
|
||||
assert_eq!(has_tls(address.as_str(), hypernet.as_str()), expected)
|
||||
}
|
||||
|
||||
let binding = read_data("2016_07_data.txt");
|
||||
let lines = binding.lines();
|
||||
for line in lines {
|
||||
let (address, hypernet) = extract_hypernet(line);
|
||||
println!("Processing ||||{line}|||| -> {}/{}", address, hypernet);
|
||||
if has_tls(address.as_str(), hypernet.as_str()) {
|
||||
num_good += 1;
|
||||
}
|
||||
}
|
||||
println!("Found {num_good} good addresses.");
|
||||
}
|
||||
|
||||
|
||||
// 132 too high
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
const SCREEN_COLS: usize = 50;
|
||||
const SCREEN_ROWS: usize = 6;
|
||||
const SCREEN_SIZE: usize = SCREEN_COLS * SCREEN_ROWS;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
enum Actions {
|
||||
Rect(u32, u32),
|
||||
RotateRow(u32, u32),
|
||||
RotateColumn(u32, u32)
|
||||
}
|
||||
|
||||
impl Actions {
|
||||
// rotate a column 1 time.
|
||||
fn rotate_column(&self, column_index: u32, target: &mut [bool; SCREEN_SIZE]) {
|
||||
let first_value = target[column_index as usize];
|
||||
for index in (SCREEN_COLS..0).rev() {
|
||||
let offset = index as u32 * SCREEN_COLS as u32 + column_index;
|
||||
println!("index = {index} offset = {offset}");
|
||||
}
|
||||
}
|
||||
|
||||
// rotate a row 1 time.
|
||||
fn rotate_row(&self, row_index: u32, target: &mut [bool; SCREEN_SIZE]) {
|
||||
for index in (SCREEN_ROWS..0).rev() {
|
||||
let offset = index as u32 * SCREEN_COLS as u32 + index;
|
||||
println!("index = {index} offset = {offset}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply(&self, target: &mut [bool; SCREEN_SIZE]) {
|
||||
match self {
|
||||
Actions::Rect(x, y) => {
|
||||
// println!("Draw box sized {x}x{y}");
|
||||
for row in 0..*y {
|
||||
for column in 0..*x {
|
||||
let offset = row * SCREEN_COLS as u32 + column;
|
||||
// println!("Setting {row}x{column}");
|
||||
target[offset as usize] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Actions::RotateRow(row_to_rotate, distance_to_rotate) => {
|
||||
let actual_rotate = distance_to_rotate % 50;
|
||||
println!("Rotate Row {row_to_rotate} by {distance_to_rotate} ({actual_rotate})");
|
||||
for _ in 0..*distance_to_rotate {
|
||||
self.rotate_row(*row_to_rotate, target)
|
||||
}
|
||||
}
|
||||
Actions::RotateColumn(column_to_rotate, distance_to_rotate) => {
|
||||
let actual_rotate = distance_to_rotate % 6;
|
||||
println!("Rotate Column {column_to_rotate} by {distance_to_rotate} ({actual_rotate})");
|
||||
for _ in 0..*distance_to_rotate {
|
||||
self.rotate_column(*column_to_rotate, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(input: &str) -> Self {
|
||||
let (command, params) = input.split_once(' ').unwrap();
|
||||
|
||||
match command {
|
||||
"rect" => {
|
||||
let (xs, ys) = params.trim().split_once('x').unwrap();
|
||||
let x = xs.parse::<u32>().unwrap();
|
||||
let y = ys.parse::<u32>().unwrap();
|
||||
Actions::Rect(x, y)
|
||||
},
|
||||
"rotate" => {
|
||||
let (direction, distance) = params.trim().split_once(' ').unwrap();
|
||||
let (axis_stub, distance_to_rotate_s) = params.trim().split_once(" by ").unwrap();
|
||||
let (_, axis_to_rotate_s) = axis_stub.split_once('=').unwrap();
|
||||
let distance_to_rotate = distance_to_rotate_s.parse::<u32>().unwrap();
|
||||
let axis_to_rotate = axis_to_rotate_s.parse::<u32>().unwrap();
|
||||
Actions::RotateColumn(axis_to_rotate, distance_to_rotate)
|
||||
}
|
||||
_ => {
|
||||
unreachable!("Invalid command -> [[{input}]]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn dump_screen(to_display: &[bool; SCREEN_SIZE]) {
|
||||
for row in 0..SCREEN_ROWS {
|
||||
for column in 0..SCREEN_COLS {
|
||||
let offset = row * SCREEN_COLS + column;
|
||||
// println!("Rendering offset {offset}");
|
||||
print!("{}", if to_display[offset] { '#' } else { ' ' });
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut screen_space:[bool; SCREEN_SIZE] = [false; SCREEN_SIZE];
|
||||
let directions = vec![
|
||||
"rect 3x2",
|
||||
"rotate column x=1 by 1",
|
||||
"rotate row y=0 by 4",
|
||||
"rotate column x=1 by 1"
|
||||
];
|
||||
|
||||
for direction in directions {
|
||||
let parsed = Actions::parse(direction);
|
||||
parsed.apply(&mut screen_space);
|
||||
println!("[[{direction}]] -> [[{parsed:?}]]");
|
||||
dump_screen(&screen_space);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user