day 3
This commit is contained in:
parent
6079ff8c0d
commit
39436c752f
5 changed files with 154 additions and 2 deletions
|
|
@ -11,3 +11,4 @@ name = "advent_of_code_2024"
|
|||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.11.1"
|
||||
|
|
|
|||
68
src/day_2/mod.rs
Normal file
68
src/day_2/mod.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
// Day 2: Red-Nosed Reports
|
||||
|
||||
/*
|
||||
one report per line
|
||||
-> list of number called levels
|
||||
|
||||
Safe:
|
||||
Decrease or Increase by 1, 2 or 3 && must *all* increase or decrease
|
||||
|
||||
Unsafe:
|
||||
Decrease or Increase by >= 4
|
||||
|
||||
test safe:
|
||||
7 6 4 2 1
|
||||
*/
|
||||
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Direction {
|
||||
Increase,
|
||||
Decrease,
|
||||
NotDefined
|
||||
}
|
||||
|
||||
|
||||
pub fn red_nosed_reports() {
|
||||
red_nosed_reports_part_1("src/day_2/input.txt");
|
||||
|
||||
}
|
||||
|
||||
pub fn red_nosed_reports_part_1(path: &str) {
|
||||
let report = "7 6 4 2 1";
|
||||
|
||||
let split: Vec<u32> = report.split_ascii_whitespace().map(|v| v.parse::<u32>().unwrap()).collect();
|
||||
for n in 1 .. split.len() {
|
||||
let current = split[n];
|
||||
let prev = split[n - 1];
|
||||
|
||||
let mut direction = Direction::NotDefined;
|
||||
|
||||
if current - prev > 0 && direction == Direction::NotDefined {
|
||||
direction = Direction::Increase
|
||||
} else {
|
||||
direction = Direction::Decrease
|
||||
}
|
||||
|
||||
let difference = current.abs_diff(prev);
|
||||
|
||||
|
||||
if difference < 1 || difference > 3 {
|
||||
panic!("unsafe")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn is_safe(report: &str) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn check_value_change(value: Vec<u32>) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
62
src/day_3/mod.rs
Normal file
62
src/day_3/mod.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// Day 3: Mull It Over
|
||||
|
||||
/*
|
||||
|
||||
"mul[(]\d{1,3}\S\d{1,3}[)]"gm
|
||||
|
||||
|
||||
maybe:
|
||||
[^don't().*do()]\s*mul[(](\d{1,3}),(\d{1,3})[)]
|
||||
*/
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use crate::read_file;
|
||||
|
||||
pub fn mull_it_over() {
|
||||
mull_it_over_part_one();
|
||||
mull_it_over_part_two();
|
||||
}
|
||||
|
||||
fn mull_it_over_part_one() {
|
||||
let re = Regex::new(r"mul[(](\d{1,3}),(\d{1,3})[)]").unwrap();
|
||||
|
||||
let test_string = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
|
||||
|
||||
let data = read_file("src/day_3/input.txt");
|
||||
|
||||
let valid_numbers: Vec<(i32, i32)> = re.captures_iter(&data).map(|caps| {
|
||||
let (_, [a, b]) = caps.extract();
|
||||
(a.parse::<i32>().unwrap(), b.parse::<i32>().unwrap())
|
||||
}).collect();
|
||||
|
||||
let mut result = 0;
|
||||
for numbers in valid_numbers {
|
||||
result += numbers.0 * numbers.1;
|
||||
}
|
||||
println!("Part 1: {:?}", result);
|
||||
}
|
||||
|
||||
fn mull_it_over_part_two() {
|
||||
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)|(do)\(\)|(don't)\(\)").unwrap();
|
||||
|
||||
let test_string = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
|
||||
|
||||
let mut result = 0;
|
||||
let mut enabled = true;
|
||||
|
||||
for c in re.captures_iter(read_file("src/day_3/input.txt").as_str()) {
|
||||
if let Some(_) = c.get(1) {
|
||||
if enabled {
|
||||
let a: i32 = c[1].parse().unwrap();
|
||||
let b: i32 = c[2].parse().unwrap();
|
||||
result += a * b;
|
||||
}
|
||||
} else if let Some(_) = c.get(3) {
|
||||
enabled = true;
|
||||
} else if let Some(_) = c.get(4) {
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
println!("Part 2: {:?}", result);
|
||||
}
|
||||
14
src/lib.rs
14
src/lib.rs
|
|
@ -1,9 +1,19 @@
|
|||
pub mod day_1;
|
||||
pub mod day_2;
|
||||
pub mod day_3;
|
||||
|
||||
use std::{fs::File, io::{self, BufRead}, path::Path};
|
||||
use std::{fs::{self, File}, io::{self, BufRead}, path::Path};
|
||||
|
||||
pub fn advent_of_code_2024() {
|
||||
day_1::historian_hysteria();
|
||||
//day_1::historian_hysteria();
|
||||
//day_2::red_nosed_reports();
|
||||
day_3::mull_it_over();
|
||||
}
|
||||
|
||||
|
||||
pub fn read_file<P>(filename: P) -> String
|
||||
where P: AsRef<Path> {
|
||||
fs::read_to_string(filename).expect("Unable to read file")
|
||||
}
|
||||
|
||||
// from: rust-by-example
|
||||
|
|
|
|||
11
tests/day_two.rs
Normal file
11
tests/day_two.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use advent_of_code_2024::day_2::*;
|
||||
|
||||
#[test]
|
||||
fn d2_is_safe() {
|
||||
assert_eq!(is_safe("7 6 4 2 1"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn d2_is_unsafe() {
|
||||
assert_eq!(is_safe("1 2 7 8 9"), false);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue