diff --git a/day10/Cargo.lock b/day10/Cargo.lock new file mode 100644 index 0000000000000000000000000000000000000000..4f652485ec29a820f2dd910877ede9f8432a0c4d --- /dev/null +++ b/day10/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day10" +version = "0.1.0" diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..f42273f918d02d0344e1bf6985035b14a0e62217 --- /dev/null +++ b/day10/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day10" +version = "0.1.0" +authors = ["Philip Molares <philip.molares@udo.edu>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..0b7840e1873b525f8fafa6105eeeed627b34266e --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,91 @@ +95 +43 +114 +118 +2 +124 +120 +127 +140 +21 +66 +103 +102 +132 +136 +93 +59 +131 +32 +9 +20 +141 +94 +109 +143 +142 +65 +73 +27 +83 +133 +104 +60 +110 +89 +29 +78 +49 +76 +16 +34 +17 +105 +98 +15 +106 +4 +57 +1 +67 +71 +14 +92 +39 +68 +125 +113 +115 +26 +33 +61 +45 +46 +11 +99 +7 +25 +130 +42 +3 +10 +54 +44 +139 +50 +8 +58 +86 +64 +77 +35 +79 +72 +36 +80 +126 +28 +123 +119 +51 +22 \ No newline at end of file diff --git a/day10/src/main.rs b/day10/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..46bb169d45d9365e73e919efaebf6a59a48dea40 --- /dev/null +++ b/day10/src/main.rs @@ -0,0 +1,80 @@ +use std::fs::File; +use std::io::{self, prelude::*, BufReader}; + +fn calculate_differences(input: &Vec<i64>) -> (i64, i64, i64) { + let mut ones = 0; + let mut twos = 0; + let mut threes = 0; + + let mut joltage = 0; + + for number in input { + match number - joltage { + 0 => (), + 1 => ones += 1, + 2 => twos += 1, + 3 => threes += 1, + _ => unimplemented!() + } + joltage = *number; + } + + (ones, twos, threes) +} + +fn part_2(input: &Vec<i64>) -> i64 { + let mut slices = vec![]; + let mut current_slice = vec![]; + + for window in input.windows(2) { + match window[1] - window[0] { + 1 => current_slice.push(window[0]), + 3 => { + current_slice.push(window[0]); + slices.push(current_slice); + current_slice = vec![]; + } + _ => (), + } + } + + slices + .iter() + .map(|slice| match slice.len() { + 1 => 1, + 2 => 1, + 3 => 2, + 4 => 4, + 5 => 7, + _ => panic!("unexpected slice of size N > 5 consecutive 1-diff elements"), + }) + .product() + +} + +fn main() -> io::Result<()> { + println!("Advent of Code 2020 – Day 10:"); + + let file = File::open("input.txt")?; + let reader = BufReader::new(file); + + let mut input = reader + .lines() + .map(|l| l.unwrap().parse::<i64>().unwrap()) + .collect::<Vec<i64>>(); + + input.sort(); + input.insert(0, 0); + input.push(*input.last().unwrap() + 3); + + let (ones, _, threes) = calculate_differences(&input); + + println!("Answer Part 1: {}", ones*threes); + + let answer2 = part_2(&input); + + println!("Answer Part 2: {}", answer2); + + + Ok(()) +}