initial
This commit is contained in:
commit
1299527ab2
19 changed files with 1335 additions and 0 deletions
8
aoc2023/Cargo.toml
Normal file
8
aoc2023/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aoc2023"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
shared = { path = "../shared" }
|
||||
regex = "1.10.2"
|
||||
59
aoc2023/src/day01.rs
Normal file
59
aoc2023/src/day01.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use std::any::Any;
|
||||
use shared::{Answer, Solution};
|
||||
use regex::{Regex, Captures};
|
||||
|
||||
pub struct Day01;
|
||||
|
||||
impl Solution for Day01 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
let words: Vec<&str> = input.split("\n")
|
||||
.collect();
|
||||
|
||||
let mut sum: u64 = 0;
|
||||
|
||||
for word in words {
|
||||
let mut first: Option<u64> = None;
|
||||
let mut last: Option<u64> = None;
|
||||
for char in word.chars() {
|
||||
if char.is_digit(10) {
|
||||
last = Some(char.to_digit(10).unwrap().into());
|
||||
if first == None {
|
||||
first = Some(char.to_digit(10).unwrap().into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sum += 10 * first.unwrap() + last.unwrap()
|
||||
}
|
||||
|
||||
Answer::from(sum)
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
let words: Vec<&str> = input.split("\n")
|
||||
.collect();
|
||||
|
||||
let first = Regex::new(
|
||||
r".*?(?:(1|one)|(2|two)|(3|three)|(4|four)|(5|five)|(6|six)|(7|seven)|(8|eight)|(9|nine)|(0|zero)).*"
|
||||
).unwrap();
|
||||
let last = Regex::new(
|
||||
r".*(?:(1|one)|(2|two)|(3|three)|(4|four)|(5|five)|(6|six)|(7|seven)|(8|eight)|(9|nine)|(0|zero)).*?"
|
||||
).unwrap();
|
||||
|
||||
Answer::from(words.iter()
|
||||
.map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(&word).unwrap())})
|
||||
.collect::<Vec<_>>()
|
||||
.iter()
|
||||
.sum::<u64>())
|
||||
}
|
||||
}
|
||||
|
||||
fn matches(cap: ®ex::Captures) -> u64 {
|
||||
for i in 1..10 {
|
||||
match cap.get(i) {
|
||||
Some(val) => return (i as u64),
|
||||
None => (),
|
||||
};
|
||||
}
|
||||
return 0
|
||||
}
|
||||
54
aoc2023/src/day02.rs
Normal file
54
aoc2023/src/day02.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use shared::{Answer, Solution};
|
||||
use regex::{Regex, Captures};
|
||||
|
||||
pub struct Day02;
|
||||
|
||||
const POSSIBLE: HashMap<&str, u64> = HashMap::from([
|
||||
("red", 12),
|
||||
("green", 13),
|
||||
("blue", 14)
|
||||
]);
|
||||
|
||||
const POSSIBLE_RED : u64 = 12;
|
||||
const POSSIBLE_BLUE : u64 = 14;
|
||||
const POSSIBLE_GREEN : u64 = 13;
|
||||
|
||||
|
||||
|
||||
impl Solution for Day02 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
let games: Vec<&str> = input.split("\n")
|
||||
.collect();
|
||||
|
||||
let re = Regex::new(r"(\d+) (\w+)").unwrap();
|
||||
|
||||
let mut sum: u64 = 0;
|
||||
|
||||
for (i, game) in games.iter().enumerate() {
|
||||
let mut max_red : u64 = 0;
|
||||
let mut max_blue : u64 = 0;
|
||||
let mut max_green : u64 = 0;
|
||||
|
||||
for m in re.captures_iter(&game) {
|
||||
match m.extract() {
|
||||
Some("red") => max_red = max_red.max(m[1].parse::<u64>().unwrap()),
|
||||
Some("blue") => max_blue = max_blue.max(m[1].parse::<u64>().unwrap()),
|
||||
Some("green") => max_green = max_green.max(m[1].parse::<u64>().unwrap()),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
if max_red <= POSSIBLE_RED && max_blue <= POSSIBLE_BLUE && max_green <= POSSIBLE_GREEN {
|
||||
sum += i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
}
|
||||
}
|
||||
4
aoc2023/src/lib.rs
Normal file
4
aoc2023/src/lib.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use shared::Solution;
|
||||
|
||||
pub mod day01;
|
||||
pub mod day02;
|
||||
Loading…
Add table
Add a link
Reference in a new issue