my solutions to advent of code
aoc
advent-of-code
1use std::{
2 collections::{HashMap, HashSet},
3 mem::swap,
4};
5
6fn main() {
7 let input = include_str!("../../input.txt").trim();
8
9 let input: Vec<&str> = input.trim().split("\n").collect();
10 // let width = input[0].len() as u64;
11 let start = input[0].find("S").unwrap() as u64;
12 let splitters_map: Vec<HashSet<u64>> = input[0..input.len()]
13 .iter()
14 .enumerate()
15 .map(|(i, line)| if i % 2 != 0 { "" } else { line })
16 .filter(|line| line != &"")
17 .map(|line| {
18 line.chars()
19 .enumerate()
20 .fold(HashSet::new(), |mut s, (i, char)| {
21 if char == '^' {
22 s.insert(i as u64);
23 }
24 s
25 })
26 })
27 .collect();
28
29 let timelines = {
30 let mut timelines: HashMap<u64, u64> = HashMap::new();
31 timelines.insert(start, 1);
32 let mut timelines_new: HashMap<u64, u64> = HashMap::new();
33 for splitters in splitters_map {
34 for (pos, amount) in &timelines {
35 if splitters.contains(&pos) {
36 let m1 = timelines_new.entry(pos - 1).or_insert(0);
37 *m1 += amount;
38 let p1 = timelines_new.entry(pos + 1).or_insert(0);
39 *p1 += amount;
40 } else {
41 let e = timelines_new.entry(*pos).or_insert(0);
42 *e += amount;
43 }
44 }
45 // for pos in 0..width as u64 {
46 // if splitters.contains(&pos) {
47 // print!("^");
48 // } else if timelines_new.contains_key(&pos) {
49 // print!("|");
50 // } else {
51 // print!(".");
52 // }
53 // }
54 // print!("\n\n");
55 swap(&mut timelines, &mut timelines_new);
56 timelines_new.clear();
57 }
58 timelines
59 };
60 // println!("{:?}", timelines);
61 println!("{}", timelines.iter().fold(0, |acc, v| { acc + v.1 }))
62}