y2024d9
This commit is contained in:
20
y2016/src/bin/d6.rs
Normal file
20
y2016/src/bin/d6.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::fs;
|
||||
|
||||
use y2016::days::d6;
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/6_input.txt")).unwrap();
|
||||
println!("{}", d6::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/6_input.txt")).unwrap();
|
||||
println!("{}", d6::process_part2(&content));
|
||||
}
|
||||
73
y2016/src/days/d6.rs
Normal file
73
y2016/src/days/d6.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn process_part1(input: &str) -> String {
|
||||
let line_len = input.find("\n").unwrap();
|
||||
let mut rows: Vec<HashMap<char, u32>> = vec![HashMap::new(); line_len];
|
||||
input.lines().for_each(|line| {
|
||||
for (idx, chara) in line.chars().enumerate() {
|
||||
rows[idx]
|
||||
.entry(chara)
|
||||
.and_modify(|count| *count += 1)
|
||||
.or_insert(1);
|
||||
}
|
||||
});
|
||||
let mut res = String::new();
|
||||
for row in rows {
|
||||
let most_common = row.iter().max_by(|a, b| a.1.cmp(b.1)).unwrap().0;
|
||||
res.push(*most_common);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> String {
|
||||
let line_len = input.find("\n").unwrap();
|
||||
let mut rows: Vec<HashMap<char, u32>> = vec![HashMap::new(); line_len];
|
||||
input.lines().for_each(|line| {
|
||||
for (idx, chara) in line.chars().enumerate() {
|
||||
rows[idx]
|
||||
.entry(chara)
|
||||
.and_modify(|count| *count += 1)
|
||||
.or_insert(1);
|
||||
}
|
||||
});
|
||||
let mut res = String::new();
|
||||
for row in rows {
|
||||
let most_common = row.iter().max_by(|a, b| b.1.cmp(a.1)).unwrap().0;
|
||||
res.push(*most_common);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "eedadn
|
||||
drvtee
|
||||
eandsr
|
||||
raavrd
|
||||
atevrs
|
||||
tsrnev
|
||||
sdttsa
|
||||
rasrtv
|
||||
nssdts
|
||||
ntnada
|
||||
svetve
|
||||
tesnvt
|
||||
vntsnd
|
||||
vrdear
|
||||
dvrsen
|
||||
enarar";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, *"easter");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, *"advent");
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,5 @@ pub mod d3;
|
||||
pub mod d4;
|
||||
|
||||
pub mod d5;
|
||||
|
||||
pub mod d6;
|
||||
|
||||
Reference in New Issue
Block a user