moved code around so each year is a package.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name="aoc2020"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
core = { path = "../core" }
|
||||
@@ -0,0 +1,24 @@
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2020_01_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut numbers = vec![];
|
||||
let mut left = 0;
|
||||
let mut right = 0;
|
||||
|
||||
for line in lines {
|
||||
numbers.push(line.parse::<u32>().unwrap());
|
||||
}
|
||||
|
||||
for left_number in numbers.clone() {
|
||||
for right_number in numbers.clone() {
|
||||
if left_number + right_number == 2020 {
|
||||
println!("Found {left_number} and {right_number}");
|
||||
println!("Multiplied they are {}", left_number * right_number);
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 972576
|
||||
@@ -0,0 +1,63 @@
|
||||
use core::read_data;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ParsedLine {
|
||||
pub min: u32,
|
||||
pub max: u32,
|
||||
pub char: char,
|
||||
pub pass: String,
|
||||
pub raw: String
|
||||
}
|
||||
|
||||
fn is_valid(input: ParsedLine) -> bool {
|
||||
let mut num_char_found = 0;
|
||||
for char in input.pass.chars() {
|
||||
if char == input.char {
|
||||
num_char_found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("||{}|| Found CHAR {} REPEATED {num_char_found} TIMES", input.raw, input.char);
|
||||
|
||||
num_char_found <= input.max && num_char_found >= input.min
|
||||
}
|
||||
|
||||
fn parse_string_to_parts(input: &str) -> ParsedLine {
|
||||
let (both_ranges, balance) = input.split_once(" ").unwrap();
|
||||
let (low_count, high_count) = both_ranges.split_once('-').unwrap();
|
||||
let (find_char, balance) = balance.split_once(':').unwrap();
|
||||
|
||||
ParsedLine {
|
||||
min: low_count.parse().unwrap(),
|
||||
max: high_count.parse().unwrap(),
|
||||
char: find_char.chars().next().unwrap(),
|
||||
pass: balance.to_string(),
|
||||
raw: input.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut num_valid = 0;
|
||||
let params = vec![
|
||||
("1-3 a: abcde", true),
|
||||
("1-3 b: cdefg", false),
|
||||
("2-9 c: ccccccccc", true)
|
||||
];
|
||||
|
||||
let binding = read_data("2020_02_data.txt");
|
||||
let lines = binding.lines();
|
||||
|
||||
for input in lines {
|
||||
let parsed = parse_string_to_parts(input);
|
||||
let result = is_valid(parsed.clone());
|
||||
// println!("Testing [{input}] and RESULT [{result}]");
|
||||
// assert_eq!(is_valid(parsed), output);
|
||||
if result {
|
||||
num_valid += 1;
|
||||
}
|
||||
}
|
||||
println!("Found {num_valid} valid passwords.");
|
||||
}
|
||||
// 493
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::io::{stdout, Write};
|
||||
use core::read_data;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ParsedLine {
|
||||
pub must: u32,
|
||||
pub isnt: u32,
|
||||
pub char: char,
|
||||
pub pass: String,
|
||||
pub raw: String
|
||||
}
|
||||
|
||||
fn parse_string_to_parts(input: &str) -> ParsedLine {
|
||||
let (both_ranges, balance) = input.split_once(" ").unwrap();
|
||||
let (low_count, high_count) = both_ranges.split_once('-').unwrap();
|
||||
let (find_char, balance) = balance.split_once(':').unwrap();
|
||||
|
||||
ParsedLine {
|
||||
must: low_count.parse().unwrap(),
|
||||
isnt: high_count.parse().unwrap(),
|
||||
char: find_char.chars().next().unwrap(),
|
||||
pass: balance.trim().to_string(),
|
||||
raw: input.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// are the Nth characters a match, but only 1?
|
||||
fn is_valid(input: ParsedLine) -> bool {
|
||||
(input.pass.chars().nth((input.must -1) as usize).unwrap() == input.char) ^
|
||||
(input.pass.chars().nth((input.isnt - 1) as usize).unwrap() == input.char)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut num_valid = 0;
|
||||
|
||||
let binding = read_data("2020_02_data.txt");
|
||||
let lines = binding.lines();
|
||||
let params = lines;
|
||||
|
||||
for param in params {
|
||||
let parsed = parse_string_to_parts(param);
|
||||
|
||||
if is_valid(parsed) {
|
||||
num_valid += 1;
|
||||
}
|
||||
}
|
||||
println!("There are {num_valid} valid passwords.");
|
||||
}
|
||||
// 593
|
||||
Reference in New Issue
Block a user