Skip to content
Snippets Groups Projects
Commit 770f5b99 authored by Evy Storozhenko's avatar Evy Storozhenko
Browse files

initial commit

parents
Branches
No related tags found
No related merge requests found
# rust
target/
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'aoc'",
"cargo": {
"args": [
"build",
"--bin=aoc",
"--package=aoc"
],
"filter": {
"name": "aoc",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'aoc'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=aoc",
"--package=aoc"
],
"filter": {
"name": "aoc",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "aoc"
version = "0.1.0"
dependencies = [
"anyhow",
]
[package]
name = "aoc"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
This diff is collapsed.
use std::{collections::HashMap, fs};
use anyhow::Result;
fn reverse_string(text: &str) -> String {
text.chars().rev().collect()
}
fn remove_art(line: &str) -> (u32, u32) {
let patterns = [
["one", "1"],
["two", "2"],
["three", "3"],
["four", "4"],
["five", "5"],
["six", "6"],
["seven", "7"],
["eight", "8"],
["nine", "9"],
];
let a = patterns
.iter()
.filter_map(|v| {
if let Some(found) = v.iter().filter_map(|v| line.find(v)).min() {
Some((v[1].parse::<u32>().unwrap(), found))
} else {
None
}
})
.min_by(|x, y| x.1.cmp(&y.1))
.map(|v| v.0.to_owned())
.unwrap();
let b = patterns
.iter()
.filter_map(|v| {
if let Some(found) = v
.iter()
.filter_map(|v| reverse_string(&line).find(&reverse_string(v)))
.min()
{
Some((v[1].parse::<u32>().unwrap(), found))
} else {
None
}
})
.min_by(|x, y| x.1.cmp(&y.1))
.map(|v| v.0.to_owned())
.unwrap();
(a, b)
}
fn main() -> Result<()> {
let data = fs::read_to_string("data.txt")?;
let result_a: u32 = data
.lines()
.map(|v| {
let a = v.chars().find(|v| v.is_ascii_digit()).unwrap();
let b = v.chars().rev().find(|v| v.is_ascii_digit()).unwrap();
format!("{a}{b}").parse::<u32>().unwrap()
})
.sum();
println!("{result_a}");
let result_b: u32 = data
.lines()
.map(|v| {
let (a, b) = remove_art(v);
format!("{a}{b}").parse::<u32>().unwrap()
})
.sum();
println!("{result_b}");
Ok(())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment