Day 1
This commit is contained in:
parent
dd054ac2fd
commit
4f3da898a0
7 changed files with 1089 additions and 1 deletions
10
.fleet/run.json
Normal file
10
.fleet/run.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "cargo",
|
||||||
|
"name": "Cargo configuration",
|
||||||
|
"cargoArgs": ["run"],
|
||||||
|
},
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
11
.idea/advent-of-code-2023.iml
Normal file
11
.idea/advent-of-code-2023.iml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="EMPTY_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/advent-of-code-2023.iml" filepath="$PROJECT_DIR$/.idea/advent-of-code-2023.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
1000
res/01/input
Normal file
1000
res/01/input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,9 +1,54 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn solve() {
|
pub fn solve() {
|
||||||
let path = "res/01/input.txt";
|
let path = "res/01/input";
|
||||||
|
|
||||||
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
||||||
|
|
||||||
|
let result: u32 = contents
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let nums = line.chars().filter(|c| c.is_digit(10)).collect::<Vec::<char>>();
|
||||||
|
nums.first().unwrap().to_digit(10).unwrap() * 10 + nums.last().unwrap().to_digit(10).unwrap()})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("Result 1: {}", result);
|
||||||
|
|
||||||
|
let mut txt_nums = HashMap::<&str, u32>::new();
|
||||||
|
txt_nums.insert("zero", 0);
|
||||||
|
txt_nums.insert("one", 1);
|
||||||
|
txt_nums.insert("two", 2);
|
||||||
|
txt_nums.insert("three", 3);
|
||||||
|
txt_nums.insert("four", 4);
|
||||||
|
txt_nums.insert("five", 5);
|
||||||
|
txt_nums.insert("six", 6);
|
||||||
|
txt_nums.insert("seven", 7);
|
||||||
|
txt_nums.insert("eight", 8);
|
||||||
|
txt_nums.insert("nine", 9);
|
||||||
|
|
||||||
|
let result: u32 = contents
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let mut nums = Vec::<u32>::new();
|
||||||
|
line.chars().enumerate().for_each(|(i,c)| {
|
||||||
|
if c.is_digit(10) {
|
||||||
|
nums.push(c.to_digit(10).unwrap());
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for (k,v) in txt_nums.clone() {
|
||||||
|
if c == k.chars().next().unwrap() && line.len() - i >= k.len() && (&line[i..i + k.len()]) == k {
|
||||||
|
nums.push(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
nums.first().unwrap() * 10 + nums.last().unwrap()
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("Result 2: {}", result);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue