Day 1
This commit is contained in:
parent
3e8b6ea9ee
commit
e887e789d4
|
@ -1 +1,2 @@
|
|||
/target
|
||||
.DS_Store
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc23"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[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"
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10"
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,80 @@
|
|||
use regex::Regex;
|
||||
|
||||
pub fn run() {
|
||||
let input = include_str!("../input/day1.txt");
|
||||
// let input = r#"1abc2
|
||||
// pqr3stu8vwx
|
||||
// a1b2c3d4e5f
|
||||
// treb7uchet"#;
|
||||
// let input = r#"two1nine
|
||||
// eightwothree
|
||||
// abcone2threexyz
|
||||
// xtwone3four
|
||||
// 4nineeightseven2
|
||||
// zoneight234
|
||||
// 7pqrstsixteen"#;
|
||||
|
||||
// let sum: u32 = input
|
||||
// .lines()
|
||||
// .filter(|s| !s.is_empty())
|
||||
// .map(|s| find_digits(s))
|
||||
// .sum();
|
||||
// dbg!(sum);
|
||||
|
||||
dbg!(find_digits_part2("gdgj3f"));
|
||||
|
||||
let sum2 = input
|
||||
.lines()
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| find_digits_part2(s))
|
||||
.sum::<u32>();
|
||||
dbg!(sum2);
|
||||
}
|
||||
|
||||
fn find_digits(s: &str) -> u32 {
|
||||
let first = s
|
||||
.chars()
|
||||
.nth(s.find(|c: char| c.is_ascii_digit()).unwrap())
|
||||
.unwrap()
|
||||
.to_digit(10)
|
||||
.unwrap();
|
||||
let last = s
|
||||
.chars()
|
||||
.nth(s.rfind(|c: char| c.is_ascii_digit()).unwrap())
|
||||
.unwrap()
|
||||
.to_digit(10)
|
||||
.unwrap();
|
||||
first * 10 + last
|
||||
}
|
||||
|
||||
fn find_digits_part2(s: &str) -> u32 {
|
||||
let re = Regex::new("^([1-9]|one|two|three|four|five|six|seven|eight|nine)").unwrap();
|
||||
let mut s = s.to_owned();
|
||||
let mut first: Option<u32> = None;
|
||||
let mut last: Option<u32> = None;
|
||||
while !s.is_empty() {
|
||||
if let Some(m) = re.find(&s) {
|
||||
last = Some(parse_digit(m.as_str()));
|
||||
if first == None {
|
||||
first = last;
|
||||
}
|
||||
}
|
||||
s.remove(0);
|
||||
}
|
||||
return first.unwrap() * 10 + last.unwrap();
|
||||
}
|
||||
|
||||
fn parse_digit(s: &str) -> u32 {
|
||||
match s {
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"four" => 4,
|
||||
"five" => 5,
|
||||
"six" => 6,
|
||||
"seven" => 7,
|
||||
"eight" => 8,
|
||||
"nine" => 9,
|
||||
_ => s.parse().unwrap(),
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
mod day01;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
day01::run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue