initial
This commit is contained in:
commit
1299527ab2
19 changed files with 1335 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
11
.idea/AdventOfCode.iml
generated
Normal file
11
.idea/AdventOfCode.iml
generated
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/AdventOfCode.iml" filepath="$PROJECT_DIR$/.idea/AdventOfCode.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
67
Cargo.lock
generated
Normal file
67
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "AdventOfCode"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc2023",
|
||||
"shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc2023"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
"shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "shared"
|
||||
version = "0.1.0"
|
||||
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "AdventOfCode"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[workspace]
|
||||
members = ["shared", "aoc2023"]
|
||||
|
||||
[dependencies]
|
||||
shared = { path = "shared" }
|
||||
aoc2023 = { path = "aoc2023" }
|
||||
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;
|
||||
1000
data/2023/day01
Normal file
1000
data/2023/day01
Normal file
File diff suppressed because it is too large
Load diff
6
shared/Cargo.toml
Normal file
6
shared/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "shared"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
59
shared/src/answer.rs
Normal file
59
shared/src/answer.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use std::fmt::{self, Display, Formatter, write};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Answer {
|
||||
String(String),
|
||||
Number(u64),
|
||||
Float(f64),
|
||||
Unimplemented,
|
||||
}
|
||||
|
||||
impl Display for Answer {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Answer::String(s) => write!(f, "{s}"),
|
||||
Answer::Number(n) => write!(f, "{n}"),
|
||||
Answer::Float(d) => write!(f, "{d}"),
|
||||
Answer::Unimplemented => write!(f, "Unimplemented"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Answer {
|
||||
fn from(value: String) -> Self {
|
||||
Self::String(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Answer {
|
||||
fn from(value: &str) -> Self {
|
||||
Self::String(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Answer {
|
||||
fn from(value: f64) -> Self {
|
||||
Self::Float(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for Answer {
|
||||
fn from(value: f32) -> Self {
|
||||
Self::Float(value as f64)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_from_numeric {
|
||||
($($type:ty),*) => {
|
||||
$(
|
||||
impl From<$type> for Answer {
|
||||
fn from(item: $type) -> Self {
|
||||
Answer::Number(item as u64)
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_from_numeric!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
|
||||
|
||||
0
shared/src/input_data.rs
Normal file
0
shared/src/input_data.rs
Normal file
7
shared/src/lib.rs
Normal file
7
shared/src/lib.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
mod answer;
|
||||
mod input_data;
|
||||
mod solution;
|
||||
|
||||
pub use answer::Answer;
|
||||
pub use solution::Solution;
|
||||
|
||||
6
shared/src/solution.rs
Normal file
6
shared/src/solution.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use crate::Answer;
|
||||
|
||||
pub trait Solution {
|
||||
fn part_1(&self, input: &str) -> Answer;
|
||||
fn part_2(&self, input: &str) -> Answer;
|
||||
}
|
||||
6
src/load_data.rs
Normal file
6
src/load_data.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use std::{fs, io};
|
||||
|
||||
pub fn load(year: u32, day:u32) -> io::Result<String> {
|
||||
let path = format!("./data/{year}/day{:02}", day);
|
||||
fs::read_to_string(path)
|
||||
}
|
||||
12
src/main.rs
Normal file
12
src/main.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use shared::Solution;
|
||||
mod load_data;
|
||||
|
||||
use aoc2023;
|
||||
use load_data::load;
|
||||
|
||||
fn main() {
|
||||
let Ok(data) = load(2023, 1)
|
||||
else { return; };
|
||||
|
||||
print!("{}", aoc2023::day02::Day02.part_1(&data));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue