Day01
This commit is contained in:
parent
d9446653f0
commit
fe702cc9c4
|
@ -0,0 +1,11 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "template"
|
||||
version = "0.1.0"
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
members = [
|
||||
"template",
|
||||
# "day01",
|
||||
"day01",
|
||||
# "day02",
|
||||
# "day03",
|
||||
# "day04",
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,87 @@
|
|||
use std::{fs, ops::Add};
|
||||
|
||||
fn main() {
|
||||
const FILE_PATH: &str = "input.txt";
|
||||
println!(
|
||||
"Welcome to AOC2023 day 1, first we will read the file {}",
|
||||
FILE_PATH
|
||||
);
|
||||
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
|
||||
|
||||
println!("And we parse the file!");
|
||||
println!("First thing: {}", calculate(&parse(&contents)));
|
||||
println!(
|
||||
"Second thing: {}",
|
||||
calculate(&parse(&text2digits(&contents)))
|
||||
);
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Vec<i32> {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let digits = line.chars().filter(|c| c.is_digit(10)).collect::<Vec<_>>();
|
||||
let len_digits = digits.len() - 1;
|
||||
let number: String = digits[0].to_string().add(&digits[len_digits].to_string());
|
||||
number.parse().expect("NAN")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
const NUMTEXT: [(&str, &str); 12] = [
|
||||
("four", "4"),
|
||||
("seven", "7"),
|
||||
("e", "ee"),
|
||||
("nine", "9"),
|
||||
("five", "5"),
|
||||
("t", "tt"),
|
||||
("three", "3"),
|
||||
("eight", "8"),
|
||||
("o", "oo"),
|
||||
("one", "1"),
|
||||
("two", "2"),
|
||||
("six", "6"),
|
||||
];
|
||||
fn text2digits(input: &str) -> String {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut line: String = line.to_string();
|
||||
for (name, num) in NUMTEXT {
|
||||
line = line.replacen(name, num, 99999);
|
||||
}
|
||||
line.add("\n")
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn calculate(numbers: &Vec<i32>) -> i32 {
|
||||
numbers.iter().sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
const INPUT1: &str = r#"1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet"#;
|
||||
|
||||
const INPUT2: &str = r#"two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen"#;
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test1() {
|
||||
let tst = parse(INPUT1);
|
||||
assert_eq!(calculate(&tst), 142);
|
||||
}
|
||||
#[test]
|
||||
fn test2() {
|
||||
let tst = parse(&text2digits(INPUT2));
|
||||
assert_eq!(calculate(&tst), 281);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue