day 1 part 1 done

day_5_part_2_broken
Joey Pollack 2 years ago
commit 409120c113

1
day_1/.gitignore vendored

@ -0,0 +1 @@
/target

7
day_1/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day_1"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "day_1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,31 @@
use std::{env, fs, path::Path};
fn main()
{
// let out_dir = env::var("OUT_DIR").unwrap();
// let cwd = env::var("CARGO_MANIFEST_DIR").unwrap();
// println!("CWD: {}\n", cwd);
// let data_dir = cwd + "\\data";
// let data_path = Path::new(&data_dir);
// println!("Data path: {}", data_path.to_string_lossy());
// let data_path = Path::new("data/test_input");
let out_path = format!("target/{}", &env::var("PROFILE").unwrap());
let out_path = Path::new(&out_path);
//let out_path = Path::new(&out_dir).join(&env::var("PROFILE").unwrap());
let out_path_data = out_path.join(Path::new("data"));
if !out_path_data.exists()
{
fs::create_dir(&out_path_data).expect(&format!("Could not create data directory at: {}", out_path_data.to_string_lossy()));
}
for file in fs::read_dir("data").unwrap()
{
let file = file.unwrap();
let dest = out_path.join(file.path());
fs::copy(file.path(), &dest).expect(&format!("Could not copy file {} to {}", file.path().to_string_lossy(), dest.to_string_lossy()));
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

@ -0,0 +1,92 @@
use std::{io::prelude::*, fs::File, path::Path };
fn main()
{
do_part_1();
do_part_2();
}
fn do_part_2()
{
}
fn do_part_1()
{
let data = load_data("data/input");
println!("\nProcessing Data...");
let mut sum: u64 = 0;
for line in data.lines()
{
let first = get_first_digit(line);
let last = get_last_digit(line);
let num = (first * 10) + last;
println!("\nLine: {} -- First: {}, Second: {}, Num: {}", line, first, last, num);
sum += num as u64;
}
println!("\nFinal Sum: {}", sum);
}
fn get_first_digit(line: &str) -> u8
{
for c in line.as_bytes()
{
if is_num(*c)
{
return (*c - 48) as u8;
}
}
return 0;
}
fn get_last_digit(line: &str) -> u8
{
//let mut last_digit = 0 as u8;
for c in line.chars().rev()
{
if is_num(c as u8)
{
return (c as u8 - 48) as u8;
}
}
0
}
fn is_num(c: u8) -> bool
{
c >= 48 && c <= 57
}
fn is_spelled_num(start: &str) -> Option<u8>
{
None
}
fn load_data(file_name: &str) -> String
{
let mut file = match File::open(Path::new(file_name))
{
Ok(file) => file,
Err(why) => panic!("Could not open file {}: {}", Path::new(file_name).display(), why),
};
let mut s = String::new();
let file_contents = match file.read_to_string(&mut s)
{
Err(why) => panic!("couldn't read {}: {}", Path::new(file_name).display(), why),
Ok(_) => s,
};
return file_contents;
}
Loading…
Cancel
Save