diff --git a/aoc2020/benches/bench.rs b/aoc2020/benches/bench.rs index 1e916d1..c4be179 100644 --- a/aoc2020/benches/bench.rs +++ b/aoc2020/benches/bench.rs @@ -9,6 +9,7 @@ use aoc2020::day06; use aoc2020::day07; use aoc2020::day08; use aoc2020::day09; +use aoc2020::day10; fn aoc2020_all(c: &mut Criterion) { c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); @@ -20,6 +21,7 @@ fn aoc2020_all(c: &mut Criterion) { c.bench_function("day07", |b| b.iter(|| day07::run().unwrap())); c.bench_function("day08", |b| b.iter(|| day08::run().unwrap())); c.bench_function("day09", |b| b.iter(|| day09::run().unwrap())); + c.bench_function("day10", |b| b.iter(|| day10::run().unwrap())); } criterion_group! { diff --git a/aoc2020/input/day10.txt b/aoc2020/input/day10.txt new file mode 100644 index 0000000..5b4b267 --- /dev/null +++ b/aoc2020/input/day10.txt @@ -0,0 +1,97 @@ +8 +131 +91 +35 +47 +116 +105 +121 +56 +62 +94 +72 +13 +82 +156 +102 +12 +59 +31 +138 +46 +120 +7 +127 +126 +111 +2 +123 +22 +69 +18 +157 +75 +149 +88 +81 +23 +98 +132 +1 +63 +142 +37 +133 +61 +112 +122 +128 +155 +145 +139 +66 +42 +134 +24 +60 +9 +28 +17 +29 +101 +148 +96 +68 +25 +19 +6 +67 +113 +55 +40 +135 +97 +79 +48 +159 +14 +43 +86 +36 +41 +85 +87 +119 +30 +108 +80 +152 +158 +151 +32 +78 +150 +95 +3 +52 +49 diff --git a/aoc2020/input/day10_provided1.txt b/aoc2020/input/day10_provided1.txt new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/aoc2020/input/day10_provided1.txt @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/aoc2020/input/day10_provided2.txt b/aoc2020/input/day10_provided2.txt new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/aoc2020/input/day10_provided2.txt @@ -0,0 +1,31 @@ +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/aoc2020/src/day10.rs b/aoc2020/src/day10.rs new file mode 100644 index 0000000..2479e81 --- /dev/null +++ b/aoc2020/src/day10.rs @@ -0,0 +1,65 @@ +use std::fmt::Write; + +use aoc::err; + +const INPUT: &str = include_str!("../input/day10.txt"); + +pub fn run() -> aoc::Result { + let mut res = String::with_capacity(128); + + writeln!(res, "part 1: {}", part1(INPUT)?)?; + + Ok(res) +} + +fn part1(input: &str) -> aoc::Result { + let mut jolts = input + .lines() + .map(|line| { + line.parse() + .map_err(|e| err!("couldn't parse joltage: {}", e)) + }) + .collect::>>()?; + + if jolts.is_empty() { + return Err(err!("input was empty!")); + } + + // charging outlet can be added + jolts.insert(0, 0); + + jolts.sort_unstable(); + + // device is rated for max adapter + 3 jolts + let max_adapter = jolts[jolts.len() - 1]; + let device_rating = max_adapter + 3; + jolts.push(device_rating); + + let mut differences: [usize; 4] = [0; 4]; + + for (prev, next) in jolts.iter().zip(jolts.iter().skip(1)) { + let difference = (next - prev) as usize; + differences[difference] += 1; + } + + Ok(differences[1] * differences[3]) +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED1: &str = include_str!("../input/day10_provided1.txt"); + const PROVIDED2: &str = include_str!("../input/day10_provided2.txt"); + + #[test] + fn part1_provided() { + assert_eq!(part1(PROVIDED1).unwrap(), 7 * 5); + assert_eq!(part1(PROVIDED2).unwrap(), 22 * 10); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 2112); + } +} diff --git a/aoc2020/src/lib.rs b/aoc2020/src/lib.rs index a8fc1a9..aaf8128 100644 --- a/aoc2020/src/lib.rs +++ b/aoc2020/src/lib.rs @@ -7,3 +7,4 @@ pub mod day06; pub mod day07; pub mod day08; pub mod day09; +pub mod day10; diff --git a/aoc2020/src/main.rs b/aoc2020/src/main.rs index 9208c5a..a46da95 100644 --- a/aoc2020/src/main.rs +++ b/aoc2020/src/main.rs @@ -10,6 +10,7 @@ use aoc2020::day06; use aoc2020::day07; use aoc2020::day08; use aoc2020::day09; +use aoc2020::day10; fn main() -> Result<()> { let days: &[DayFunc] = &[ @@ -22,6 +23,7 @@ fn main() -> Result<()> { day07::run, day08::run, day09::run, + day10::run, ]; aoc::run(days)