1
0
Fork 0

Day 4 Part 1

This commit is contained in:
Yohan Boujon 2023-10-13 09:40:39 +02:00
parent 02ec699023
commit 767f4f4c04
5 changed files with 1052 additions and 0 deletions

7
rust/day4/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 = "aoc-day4"
version = "0.1.0"

11
rust/day4/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "aoc-day4"
version = "0.1.0"
edition = "2021"
authors = ["Yohan Boujon <yoboujon@gmail.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[workspace]

6
rust/day4/exemple Normal file
View file

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

1000
rust/day4/input Normal file

File diff suppressed because it is too large Load diff

28
rust/day4/src/main.rs Normal file
View file

@ -0,0 +1,28 @@
use std::fs::read_to_string;
fn convert(vec: &Vec<&str>, index: usize, str_index: usize) -> u32 {
let two_num: Vec<&str> = vec[index].split("-").collect();
two_num[str_index].parse().unwrap()
}
fn first_part(str: &String) {
let mut count = 0;
let pairs: Vec<Vec<&str>> = str
.split("\r\n")
.map(|ch| ch.split(",").collect())
.collect();
for pair in &pairs {
if convert(pair, 0, 0) <= convert(pair, 1, 0) && convert(pair, 0, 1) >= convert(pair, 1, 1)
|| convert(pair, 0, 0) >= convert(pair, 1, 0)
&& convert(pair, 0, 1) <= convert(pair, 1, 1)
{
count += 1;
}
}
println!("Final Count: {}", count);
}
fn main() {
let file_content = read_to_string("input").unwrap();
first_part(&file_content);
}