Compare commits
2 Commits
5d6d91f075
...
40922a13cb
Author | SHA1 | Date |
---|---|---|
Codinget | 40922a13cb | 2 years ago |
Codinget | c7a9f22c1a | 2 years ago |
@ -1 +1,2 @@ |
|||||||
input.txt |
input.txt |
||||||
|
/*/target |
||||||
|
@ -1 +0,0 @@ |
|||||||
/target |
|
@ -1 +0,0 @@ |
|||||||
/target |
|
@ -0,0 +1,7 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "d3" |
||||||
|
version = "0.1.0" |
@ -0,0 +1,8 @@ |
|||||||
|
[package] |
||||||
|
name = "d3" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
@ -0,0 +1,44 @@ |
|||||||
|
use std::{io::Result, fs::read_to_string, collections::HashSet}; |
||||||
|
|
||||||
|
pub fn priority(x: u8) -> u32 { |
||||||
|
(match x { |
||||||
|
0x61..=0x7a => x - 0x61 + 1, |
||||||
|
0x41..=0x5a => x - 0x41 + 27, |
||||||
|
_ => unreachable!() |
||||||
|
}) as u32 |
||||||
|
} |
||||||
|
|
||||||
|
fn main() -> Result<()> { |
||||||
|
let mut p1_priority: u32 = 0; |
||||||
|
let mut p2_priority: u32 = 0; |
||||||
|
|
||||||
|
let mut i = 0; |
||||||
|
let mut elves: Vec<HashSet<_>> = Vec::new(); |
||||||
|
|
||||||
|
for line in read_to_string("input.txt")?.split("\n") { |
||||||
|
if line.is_empty() { continue; } |
||||||
|
let bytes = line.as_bytes(); |
||||||
|
|
||||||
|
let (first_half, second_half) = bytes.split_at(bytes.len() / 2); |
||||||
|
let in_first_half = first_half.iter().copied().collect::<HashSet<_>>(); |
||||||
|
let in_second_half = second_half.iter().copied().collect::<HashSet<_>>(); |
||||||
|
let in_both_halves = in_first_half.intersection(&in_second_half).copied().next().unwrap(); |
||||||
|
p1_priority += priority(in_both_halves); |
||||||
|
|
||||||
|
let items = bytes.iter().copied().collect::<HashSet<_>>(); |
||||||
|
elves.push(items.clone()); |
||||||
|
if i == 2 { |
||||||
|
let badge = items.iter().filter(|item| elves.iter().filter(|elf| elf.contains(item)).count() == 3).copied().next().unwrap(); |
||||||
|
p2_priority += priority(badge); |
||||||
|
elves.clear(); |
||||||
|
i = 0; |
||||||
|
} else { |
||||||
|
i += 1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
println!("part 1: {}", p1_priority); |
||||||
|
println!("part 2: {}", p2_priority); |
||||||
|
|
||||||
|
Ok(()) |
||||||
|
} |
Loading…
Reference in new issue