From ecc548a98719bfaea29b5669d6b7f5641678287a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sat, 12 Dec 2020 11:21:28 +0100 Subject: [PATCH] day10 --- data/day10/input | 104 +++++++++++++++++++++++++++++++++++++++++++++++ src/bin/day10.rs | 39 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 data/day10/input create mode 100644 src/bin/day10.rs diff --git a/data/day10/input b/data/day10/input new file mode 100644 index 0000000..0815cd5 --- /dev/null +++ b/data/day10/input @@ -0,0 +1,104 @@ +79 +142 +139 +33 +56 +133 +138 +61 +125 +88 +158 +123 +65 +69 +105 +6 +81 +31 +60 +70 +159 +114 +71 +15 +13 +72 +118 +14 +9 +93 +162 +140 +165 +1 +80 +148 +32 +53 +102 +5 +68 +101 +111 +85 +45 +25 +16 +59 +131 +23 +91 +92 +115 +103 +166 +22 +145 +161 +108 +155 +135 +55 +86 +34 +37 +78 +28 +75 +7 +104 +121 +24 +153 +167 +95 +87 +94 +134 +154 +84 +151 +124 +62 +49 +38 +39 +54 +109 +128 +19 +2 +98 +122 +132 +141 +168 +8 +160 +50 +42 +46 +110 +12 +152 diff --git a/src/bin/day10.rs b/src/bin/day10.rs new file mode 100644 index 0000000..ea2d282 --- /dev/null +++ b/src/bin/day10.rs @@ -0,0 +1,39 @@ +const INPUT: &str = include_str!("../../data/day10/input"); + +fn calc(input: &[u32], cache: &mut [Option], ndx: usize) -> u64 { + if let Some(n) = cache[ndx] { + n + } else { + let cur = input[ndx]; + let mut n = 0; + for next in ndx+1..input.len() { + if input[next] - cur > 3 { break; } + n += calc(input, cache, next); + } + cache[ndx] = Some(n); + n + } +} + +fn main() { + let mut input: Vec = INPUT.lines().map(|s| s.parse().unwrap()).collect(); + input.sort(); + + let mut prev = 0; + let mut count = [0, 0, 0]; + { + for &j in &input { + count[(j - prev - 1) as usize] += 1; + prev = j; + } + } + count[2] += 1; // device always has diff of 3. + println!("#(joltage diff 1) * #(joltage diff 3): {}", count[2] * count[0]); + + // insert outlet as first element + input.insert(0, 0); + let mut cache: Vec> = input.iter().map(|_| None).collect(); + cache[input.len()-1] = Some(1); // always need last adapter + + println!("Combinations: {}", calc(&input, &mut cache, 0)); +}