mirror of
https://gitlab.com/MisterBiggs/aoc2021.git
synced 2025-08-05 12:51:22 +00:00
beginning of days 4 and 5
This commit is contained in:
9
day5/rust/Cargo.toml
Normal file
9
day5/rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10"
|
34
day5/rust/src/main.rs
Normal file
34
day5/rust/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string("../input.txt").expect("Could not read file");
|
||||
|
||||
let lines = input
|
||||
.lines()
|
||||
.filter_map(|l| {
|
||||
l.split(" -> ")
|
||||
.map(|s| s.split(','))
|
||||
.flatten()
|
||||
.map(|i| i.parse::<i8>().unwrap())
|
||||
.collect_tuple()
|
||||
})
|
||||
.collect::<Vec<(i8, i8, i8, i8)>>();
|
||||
|
||||
let mut points = HashMap::new();
|
||||
|
||||
for (x1, y1, x2, y2) in lines {
|
||||
let dx: i8 = x2 - x1;
|
||||
let dy: i8 = y2 - y1;
|
||||
for x in x1..x1 + dx {
|
||||
for y in y1..y1 + dy {
|
||||
*points.entry((x, y)).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (key, value) in points.into_iter() {
|
||||
println!("({}, {}), {}", key.0, key.1, value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user