diff --git a/inputs/day10.txt b/inputs/day10.txt new file mode 100644 index 0000000..a68e236 --- /dev/null +++ b/inputs/day10.txt @@ -0,0 +1,108 @@ +115 +134 +121 +184 +78 +84 +77 +159 +133 +90 +71 +185 +152 +165 +39 +64 +85 +50 +20 +75 +2 +120 +137 +164 +101 +56 +153 +63 +70 +10 +72 +37 +86 +27 +166 +186 +154 +131 +1 +122 +95 +14 +119 +3 +99 +172 +111 +142 +26 +82 +8 +31 +53 +28 +139 +110 +138 +175 +108 +145 +58 +76 +7 +23 +83 +49 +132 +57 +40 +48 +102 +11 +105 +146 +149 +66 +38 +155 +109 +128 +181 +43 +44 +94 +4 +169 +89 +96 +60 +69 +9 +163 +116 +45 +59 +15 +178 +34 +114 +17 +16 +79 +91 +100 +162 +125 +156 +65 diff --git a/inputs/test.txt b/inputs/test.txt index b036d3e..e6376dc 100644 --- a/inputs/test.txt +++ b/inputs/test.txt @@ -1,3 +1,31 @@ -BFFFBBFRRR -FFFBBBFRRR -BBFFBBFRLL +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 diff --git a/src/bin/day10.rs b/src/bin/day10.rs new file mode 100644 index 0000000..87a39eb --- /dev/null +++ b/src/bin/day10.rs @@ -0,0 +1,66 @@ +use itertools::Itertools; +use std::collections::HashMap; +use std::error::Error; +use std::fs::File; +use std::io::{self, BufRead}; +use std::vec::Vec; + +fn enumerate_chains(curr_jolts: u32, rest: &[u32], cache: &mut HashMap<(u32, u32), u64>) -> u64 { + if rest.len() == 0 { + println!("Finished path"); + 1 + } else if rest[0] - curr_jolts > 3 { + println!("Termindated chain at {}", curr_jolts); + 0 + } else { + let jolts = rest[0]; + + if cache.contains_key(&(curr_jolts, jolts)) { + println!("Chache hit for {} {}", curr_jolts, jolts); + return cache[&(curr_jolts, jolts)]; + } + + println!("Chache miss for {} {}", curr_jolts, jolts); + + let res = if rest.len() > 3 { + enumerate_chains(jolts, &rest[1..], cache) + + enumerate_chains(jolts, &rest[2..], cache) + + enumerate_chains(jolts, &rest[3..], cache) + } else if rest.len() > 2 { + enumerate_chains(jolts, &rest[1..], cache) + enumerate_chains(jolts, &rest[2..], cache) + } else { + enumerate_chains(jolts, &rest[1..], cache) + }; + + cache.insert((curr_jolts, jolts), res); + + res + } +} + +fn main() -> Result<(), Box> { + let file = File::open("inputs/day10.txt")?; + let lines = io::BufReader::new(file).lines().map(|l| l.unwrap()); + + let mut adapters: Vec = lines.map(|l| l.parse().unwrap()).collect(); + adapters.sort(); + adapters.insert(0, 0); + adapters.push(adapters.iter().last().unwrap() + 3); + + let diff_hist: HashMap = + adapters + .windows(2) + .map(|win| win[1] - win[0]) + .fold(HashMap::new(), |mut map, x| { + map.entry(x).and_modify(|e| *e = *e + 1).or_insert(1); + map + }); + + println!("{:?}", diff_hist); + println!("Answer: {}", diff_hist[&1] * diff_hist[&3]); + + let chains = enumerate_chains(0, &adapters, &mut HashMap::new()); + println!("Number of possible chains: {}", chains); + + Ok(()) +} diff --git a/src/bin/day5.rs b/src/bin/day5.rs index b7484df..53594f5 100644 --- a/src/bin/day5.rs +++ b/src/bin/day5.rs @@ -61,7 +61,7 @@ fn main() -> Result<(), Box> { for pair in seat_ids.windows(2) { if pair[1] != pair[0] + 1 { - println!("Missing ID: {}", pair[0] + 1 ); + println!("Missing ID: {}", pair[0] + 1); break; } }