day 1 (incomplete)
This commit is contained in:
commit
f374eceb32
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
day*/target
|
||||||
|
.DS_Store
|
7
day1-trebuchet/Cargo.lock
generated
Normal file
7
day1-trebuchet/Cargo.lock
generated
Normal 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"
|
8
day1-trebuchet/Cargo.toml
Normal file
8
day1-trebuchet/Cargo.toml
Normal 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
1000
day1-trebuchet/input
Normal file
File diff suppressed because it is too large
Load diff
44
day1-trebuchet/src/main.rs
Normal file
44
day1-trebuchet/src/main.rs
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue