day1
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day1"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,156 @@
|
|||||||
|
use std::{fs::File, io::Read};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let word_numbers: Vec<String> = vec![
|
||||||
|
String::from("zero"),
|
||||||
|
String::from("one"),
|
||||||
|
String::from("two"),
|
||||||
|
String::from("three"),
|
||||||
|
String::from("four"),
|
||||||
|
String::from("five"),
|
||||||
|
String::from("six"),
|
||||||
|
String::from("seven"),
|
||||||
|
String::from("eight"),
|
||||||
|
String::from("nine"),
|
||||||
|
];
|
||||||
|
let data = get_data("data.txt");
|
||||||
|
|
||||||
|
let mut values: Vec<u32> = Vec::new();
|
||||||
|
|
||||||
|
for n in 0..data.len() {
|
||||||
|
let line = &data[n];
|
||||||
|
|
||||||
|
let digits = format!(
|
||||||
|
"{}{}",
|
||||||
|
get_first_digit(&word_numbers, line),
|
||||||
|
get_last_digit(&word_numbers, line)
|
||||||
|
);
|
||||||
|
|
||||||
|
let combined: u32 = match digits.parse() {
|
||||||
|
Ok(i) => i,
|
||||||
|
Err(err) => panic!("failed to parse digits: {} - {:?}", digits, err),
|
||||||
|
};
|
||||||
|
|
||||||
|
values.push(combined);
|
||||||
|
}
|
||||||
|
|
||||||
|
let total: u32 = values.iter().sum();
|
||||||
|
|
||||||
|
println!("## TOTAL: {} ##", total);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_last_digit(word_numbers: &Vec<String>, line: &String) -> char {
|
||||||
|
if line.len() == 0 {
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut idx_word = 0;
|
||||||
|
let mut word: &String = &String::new();
|
||||||
|
for n in 0..word_numbers.len() {
|
||||||
|
let w = &word_numbers[n];
|
||||||
|
let mut is_match = false;
|
||||||
|
|
||||||
|
let idx = match line.rfind(&word_numbers[n]) {
|
||||||
|
Some(i) => {
|
||||||
|
is_match = true;
|
||||||
|
i
|
||||||
|
}
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
if is_match && idx >= idx_word {
|
||||||
|
idx_word = idx;
|
||||||
|
word = w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut idx_char = line.len() - 1;
|
||||||
|
let mut digit: char = '0';
|
||||||
|
for c in line.chars().rev() {
|
||||||
|
if c.is_digit(10) {
|
||||||
|
digit = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx_char > 0 {
|
||||||
|
idx_char -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !word.is_empty() && idx_word >= idx_char {
|
||||||
|
return get_digit_from_str(word);
|
||||||
|
} else {
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_first_digit(word_numbers: &Vec<String>, line: &String) -> char {
|
||||||
|
let mut idx_word = line.len();
|
||||||
|
let mut word: &String = &String::new();
|
||||||
|
for n in 0..word_numbers.len() {
|
||||||
|
let w = &word_numbers[n];
|
||||||
|
|
||||||
|
let idx = match line.find(&word_numbers[n]) {
|
||||||
|
Some(i) => i,
|
||||||
|
None => line.len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if idx < idx_word {
|
||||||
|
idx_word = idx;
|
||||||
|
word = w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut idx_char = 0;
|
||||||
|
let mut digit: char = '0';
|
||||||
|
for c in line.chars() {
|
||||||
|
if c.is_digit(10) {
|
||||||
|
digit = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx_char += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx_word == idx_char {
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx_word < idx_char {
|
||||||
|
return get_digit_from_str(word);
|
||||||
|
} else {
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_data(path: &str) -> Vec<String> {
|
||||||
|
let mut data = match File::open(path) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(err) => panic!("failed to read the data file: {:?}", err),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut content = String::new();
|
||||||
|
data.read_to_string(&mut content)
|
||||||
|
.ok()
|
||||||
|
.expect("failed to parse file");
|
||||||
|
|
||||||
|
let lines: Vec<String> = content.split("\n").map(|l| l.to_string()).collect();
|
||||||
|
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_digit_from_str(word: &str) -> char {
|
||||||
|
match word {
|
||||||
|
"zero" => return '0',
|
||||||
|
"one" => return '1',
|
||||||
|
"two" => return '2',
|
||||||
|
"three" => return '3',
|
||||||
|
"four" => return '4',
|
||||||
|
"five" => return '5',
|
||||||
|
"six" => return '6',
|
||||||
|
"seven" => return '7',
|
||||||
|
"eight" => return '8',
|
||||||
|
"nine" => return '9',
|
||||||
|
_ => return '0',
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"rustc_fingerprint":17255368705219526691,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.72.1 (d5c2e9c34 2023-09-13)\nbinary: rustc\ncommit-hash: d5c2e9c342b358556da91d61ed4133f6f50fc0c3\ncommit-date: 2023-09-13\nhost: x86_64-unknown-linux-gnu\nrelease: 1.72.1\nLLVM version: 16.0.5\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/mimu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Signature: 8a477f597d28d172789f06886806bc55
|
||||||
|
# This file is a cache directory tag created by cargo.
|
||||||
|
# For information about cache directory tags see https://bford.info/cachedir/
|
||||||
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
This file has an mtime of when this was started.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
48fb439fb25dba7f
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"rustc":11325937081331929426,"features":"[]","target":11548893223606093704,"profile":18326522262828315194,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/day1-4592670e843f8e91/dep-test-bin-day1"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
55ff79776156b9b3
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"rustc":11325937081331929426,"features":"[]","target":11548893223606093704,"profile":13126374248311259211,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/day1-8ad641c4d6186058/dep-bin-day1"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
|
||||||
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
This file has an mtime of when this was started.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
d56c632af2247a7d
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"rustc":11325937081331929426,"features":"[]","target":11548893223606093704,"profile":11039742474438789458,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/day1-c8622095bd0e97ba/dep-bin-day1"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
|
||||||
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
This file has an mtime of when this was started.
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/day1: /home/mimu/projects/aoc2023/day1/src/main.rs
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-4592670e843f8e91.rmeta: src/main.rs
|
||||||
|
|
||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-4592670e843f8e91.d: src/main.rs
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-8ad641c4d6186058.rmeta: src/main.rs
|
||||||
|
|
||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-8ad641c4d6186058.d: src/main.rs
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,5 @@
|
|||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-c8622095bd0e97ba: src/main.rs
|
||||||
|
|
||||||
|
/home/mimu/projects/aoc2023/day1/target/debug/deps/day1-c8622095bd0e97ba.d: src/main.rs
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user