day 1 (incomplete)

This commit is contained in:
ari melody 2023-12-02 13:06:07 +00:00
commit f374eceb32
Signed by: ari
GPG key ID: CF99829C92678188
5 changed files with 1061 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
day*/target
.DS_Store

7
day1-trebuchet/Cargo.lock generated Normal file
View file

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

View file

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

1000
day1-trebuchet/input Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,44 @@
fn main () {
let data: String = std::fs::read_to_string("input").unwrap();
let lines = data.lines();
let new_lines = lines
.map(|raw_line| replace_numbers(raw_line))
.fold(String::new(), |a, b| format!("{a}{b}\n"));
std::fs::write("new_lines", new_lines);
// let sum: u32 = lines.map(|line| { get_calibration_values(line) }).sum();
// println!("the sum of all calibration values is {sum}!");
}
fn replace_numbers(raw_line: &str) -> String {
let numbers: [&str; 10] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
let mut line: String = raw_line.to_string();
loop {
let mut clean = false;
for i in 0..numbers.len() {
let old_line = line.clone();
line = line.replace(numbers[i], i.to_string().as_str());
if line.eq(&old_line) {
clean = true;
}
}
if clean {
break;
}
}
line
}
fn get_calibration_values(mut line: &str) -> u32 {
line = replace_numbers(line).clone().as_str();
let first: u32 = line.chars().find_map(|ch| { ch.to_digit(10) }).unwrap();
let last: u32 = line.chars().rev().find_map(|ch| { ch.to_digit(10) }).unwrap();
let result = first * 10 + last;
println!("{line} ({result})");
// first * 10 + last
result
}