From 53136289d5ad52bce1a8e842c339eda758738b15 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 3 Dec 2021 14:59:04 -0800 Subject: [PATCH] Day 3 part 1 --- 2021/input/2021/day3.txt | 1000 ++++++++++++++++++++++++++++++++++++++ 2021/src/day3.rs | 99 ++++ 2021/src/lib.rs | 1 + 3 files changed, 1100 insertions(+) create mode 100644 2021/input/2021/day3.txt create mode 100644 2021/src/day3.rs diff --git a/2021/input/2021/day3.txt b/2021/input/2021/day3.txt new file mode 100644 index 0000000..bd5b8cd --- /dev/null +++ b/2021/input/2021/day3.txt @@ -0,0 +1,1000 @@ +001111011011 +000110001010 +011010111111 +010011001110 +011001101000 +010100000011 +100001000011 +100100001101 +011011001010 +101001010010 +110100001001 +010000101010 +001001111111 +010011011101 +100000000000 +011101111000 +101100101010 +000010001110 +011101101101 +101000101010 +100011101110 +011101110011 +010100010101 +001100011011 +100010010110 +010001011001 +001010001100 +011001000111 +010010010010 +111100110101 +011011010110 +000100101011 +001011111001 +000101011100 +101011000001 +000101101101 +010000111010 +001011001001 +100110110100 +001101000011 +000011001000 +101111101000 +110011010011 +111111000111 +110100010101 +111111001111 +110011101101 +110111111110 +001101011110 +000111101001 +100100100110 +010110001110 +111001111011 +001111000101 +110000111011 +101011001110 +010111001110 +111101101111 +111110111100 +100110101010 +101100000011 +110010000101 +110101100110 +001011101101 +001011010000 +110010011010 +101101110101 +000101101110 +001001101010 +111100001011 +110110111000 +010010001000 +110101100010 +101000111111 +001110110110 +101000010100 +010111011100 +001001111000 +000010110010 +110011010010 +010101110100 +010100110000 +000001001010 +000111101000 +100100010100 +101111100010 +100001010110 +111100001110 +100011100101 +110100101010 +010000000000 +100100100000 +010101011101 +101001100011 +011001101110 +000100111111 +011110111100 +100101111001 +111110000001 +001010110111 +101011100101 +100100100101 +011010000110 +111111111010 +011110100011 +100100110111 +111001000000 +001010110100 +011110101010 +111101000011 +101011111010 +010010010111 +011111010111 +111100111011 +011011010001 +001101101111 +111010100100 +100001010000 +111010100111 +000000101101 +001110001111 +000110011100 +101001000011 +011110000011 +000101110101 +111110110100 +000011101001 +000000000101 +101100101011 +000110111000 +111010011010 +000100101000 +111111110001 +000101011111 +001000011110 +100111110000 +001010100111 +100110111010 +001101000111 +111010000100 +111001011001 +001101111001 +101000001100 +010110000111 +100001110010 +011100110000 +010001011111 +110010001101 +100001111010 +000110110111 +011110111111 +101100100111 +010001110001 +001000000111 +101110001101 +011110101001 +000110101001 +010010111110 +010001000010 +111001111001 +001101001110 +010110111010 +101110101111 +010010100001 +011100010101 +000001000011 +100111001000 +011101001110 +011101011011 +011100011001 +111111101010 +001111001011 +111010010110 +111001001101 +100000110001 +010001010010 +100010001110 +001000011011 +000111011111 +000110100011 +101100110010 +000010101101 +101100010111 +011100110100 +000010001010 +000000000011 +100000010001 +111100111111 +110001010011 +100100011101 +101000001011 +110111010011 +000111101011 +001000100000 +010110011011 +110011110101 +110100011101 +101001001000 +001101110110 +100010101111 +111001110010 +110101001001 +111100011011 +001100001001 +001001001011 +011011110100 +000000001111 +111111001001 +001011111010 +000010110111 +110100110101 +110110010011 +010100001110 +100100001011 +111111111101 +101111010110 +100111100110 +110000111000 +101011101001 +001000010010 +010000111001 +110011101000 +010100101101 +110001101100 +011000001110 +011010111100 +011110001101 +101100101001 +010111110000 +100110001111 +000000111110 +011001011010 +110110011000 +000001010101 +000000100111 +101010111001 +010011100011 +100110001100 +110001110010 +010101100001 +000101101111 +100101000011 +010110111101 +000111001101 +010000011000 +010101011001 +010100111000 +011111000010 +110111001100 +010101101111 +000111110000 +000001111100 +000110000001 +010001000011 +011011100111 +100001010101 +101000011101 +001110010101 +001000101100 +011001011001 +001101000101 +110101011000 +000100010001 +110010010101 +111010111011 +010010111000 +010110010101 +110111110110 +000110110110 +100001000000 +011000001001 +010000111011 +010010000100 +100000000111 +100100000010 +101110011111 +010111101101 +010011100000 +000111101100 +011101001100 +100001111000 +110000010000 +000011000101 +010010100000 +000000110011 +100100110010 +010100101000 +010011100111 +011111010100 +111111100010 +111011110011 +011001001100 +100110111001 +001100111001 +000001100011 +011111100010 +100011110011 +011001111010 +010101110110 +001011110100 +101101101000 +001000101110 +111001011010 +011111101000 +111001101101 +111011011011 +111001101010 +011011110010 +001010001101 +111000000110 +100010000001 +011001111101 +101011001111 +101010111110 +100111011001 +010001011010 +001001101101 +001110100101 +010111111101 +100110001101 +001010001010 +101000111000 +000011010010 +101011010001 +011100001001 +001111001110 +100010010011 +101110010001 +110100011111 +000001101110 +010010111111 +000010101010 +100100000101 +011101001011 +100000111000 +010101001100 +111010100010 +001001001100 +111110100110 +010001010000 +000111000000 +100101110111 +011001010000 +110001110101 +010111011111 +011101111011 +001100010001 +111010010010 +011010000100 +010110110011 +011100100110 +000011010110 +001001110000 +101011000011 +101011010111 +100111000000 +010111100101 +000001101011 +000110000101 +101101110111 +010101110001 +011011100011 +100001001110 +000110101111 +100111011100 +010110110010 +010010001110 +010111010100 +010101101100 +110111011001 +101100110011 +110010110001 +011001000100 +011111011111 +101101000111 +110000111110 +100101100100 +111001101000 +001110110011 +110111111010 +100101011111 +000110100101 +000000110001 +000111100001 +110001000111 +011000100110 +101010101111 +100101011110 +101000010001 +001101000100 +010100000010 +011000110100 +000101011010 +101101000100 +110101101110 +001010000000 +110110111101 +101000110011 +100010101101 +111000100101 +010110100011 +101100001100 +101101100001 +100011000001 +011010011010 +100011000000 +110000100110 +010001111100 +001101011000 +100111110001 +010010010100 +111000010011 +101000011100 +110011110001 +001110101000 +101100100101 +001101010100 +011110000100 +111000101011 +100110101011 +000111010000 +000110100010 +111100011101 +110001001010 +011011100010 +101110001000 +000011100110 +000110111111 +011111110000 +101000110110 +011110001111 +010000110111 +001111001000 +000101001010 +000000000100 +000100111110 +000101110010 +011100000010 +101010111011 +001110101110 +000100000001 +100100101111 +110000110100 +001100001100 +111011100001 +010111111100 +010111001000 +101111010111 +001011001010 +101010100001 +101011101110 +000111001100 +000000010110 +000000000001 +000010111011 +000011101110 +011011011011 +011111111110 +001111001111 +110010100100 +011111001010 +110011010100 +010011110100 +110010101100 +111111101110 +001110110111 +100111001110 +110100001111 +101101001011 +110011011110 +100110010010 +011011001000 +110000111001 +011100001101 +101101111100 +100111111110 +111001101100 +100101010011 +000100011111 +011011000100 +100101111011 +100011000010 +010101111110 +001001110110 +010111011000 +000101111100 +101000110101 +011000010001 +010010010101 +100000101100 +101010111100 +110101011110 +110011011011 +001011100001 +001111100010 +110100111100 +100010101110 +100110010110 +101011110010 +000111100000 +011011110011 +101100100010 +111101000000 +101100100110 +100111101011 +111111111011 +011001101100 +010110110000 +100000010010 +000011101101 +111110011100 +010011010011 +101000001101 +010100001001 +011110100111 +000011100010 +101001110101 +101010000000 +101101011001 +001110010001 +000000101000 +110001000100 +111111100101 +001010101001 +111000110001 +100011001110 +110001001000 +110110011011 +100100111000 +001100010110 +001111110110 +110000010010 +110110011101 +100011000111 +010100100100 +001110000000 +100010110000 +110010000011 +111011110101 +100001100110 +010110101111 +011010101010 +110101010100 +010111100011 +111101111110 +110111111111 +011111001101 +100101101010 +101010001011 +110000011001 +000101010001 +000011000000 +110111111001 +000100010100 +111100100000 +000001001001 +000010010100 +001110001001 +001001100011 +110000011100 +010000101011 +110110110000 +111111101000 +001011010001 +011001011101 +111000001000 +101101001001 +100100011110 +001100111000 +011011110101 +010001110111 +010001101101 +010011101011 +000010010111 +011111100101 +100111011000 +001011010101 +011110100010 +011100110110 +011101110000 +011000101010 +011110110011 +001101001101 +101110111001 +111101110100 +111010000101 +001010111110 +110101001011 +010010101000 +100010011011 +111001011101 +100000111100 +010011100001 +010101111011 +011010010101 +000110011111 +001110000011 +010101100011 +000010000011 +010000010111 +011111110100 +010010001001 +101101000001 +011100001011 +001110011000 +100001111001 +100011110111 +110010010000 +001100001101 +110001100000 +011010011111 +000000111101 +100010111100 +001000100101 +010101100100 +011110101110 +011010110001 +001111101110 +001111010011 +001110101101 +000001000001 +001110110100 +111101001011 +010000010010 +010100101010 +110011000101 +010101010001 +111110010000 +110100110000 +011101011100 +111000110101 +001001000011 +101111011001 +011100111111 +011000100101 +010001101001 +100001011011 +001010011011 +110001011011 +100010010001 +001010010011 +101111010010 +000001111111 +011101011010 +100100000001 +110011100111 +101100101000 +000100001000 +001011101100 +000001010010 +110001011001 +101000010111 +011100100111 +100101001110 +000100111011 +010110111011 +101110100110 +000011001111 +011110111011 +101101000000 +110011110000 +011001011111 +010011001100 +000100010111 +100000111001 +010110111001 +010000110000 +011000000111 +101010001001 +000010111111 +010100000000 +101100111000 +110010110111 +100100000110 +100110011111 +101011110100 +001111111111 +101011011111 +011111001011 +000010100010 +100000001111 +001111011101 +110101111001 +000101100110 +011110011110 +001110000001 +111101101100 +010000101001 +010011001011 +001000110000 +111000111001 +111000010111 +110011101010 +110010101001 +111110111110 +111110111000 +110110101010 +100010100110 +110111001101 +111110011001 +111111111110 +100000000101 +001000100110 +101111011111 +001000001001 +000000010000 +010101100110 +110110001000 +010100000100 +010101011000 +001010010000 +000110000010 +111010011011 +010000010011 +011110100101 +111111100000 +000100100000 +101001100110 +111011000111 +110010011111 +001000000101 +111000110010 +111010111001 +010000100010 +000001000111 +100110111011 +001111000000 +001101010110 +000010000100 +010001000001 +011110011100 +010101100101 +100001011101 +011010101111 +000101000111 +101001100100 +000000100110 +110101001110 +100100100011 +101101000110 +011010110011 +111010001000 +011110000111 +101000010101 +011001111100 +010110000011 +011010001110 +001010010100 +100111111001 +010011101010 +111110111010 +000101010010 +100010000111 +101011110001 +010110110111 +111001001010 +111010110100 +101010000100 +110010110100 +110100110001 +100011010100 +100101101101 +011010000010 +111100000010 +101110110111 +110001110110 +000001111011 +001100011100 +101011100110 +101011010101 +111101110110 +111011101000 +010110010001 +001000101111 +101000000101 +100110000010 +100111011111 +110110100100 +111100100111 +110100010000 +100001111110 +010111001011 +011001001110 +110011100100 +001001100111 +100100101011 +110000000111 +100010001101 +010101110011 +001010010101 +111111110011 +001111111110 +011010110000 +111111100110 +000001010011 +111011100111 +001111000010 +111101111011 +011101010011 +110000110111 +000010100100 +110001000101 +100111010110 +100010001100 +011011111110 +101111001000 +000010001100 +101010001101 +010011110101 +100001100010 +010001001110 +010101100111 +111011111111 +000010111100 +110100101100 +011111101100 +100100011000 +110110000011 +111100110000 +111100001111 +000101111010 +111110110110 +110101110001 +101110000010 +010000010001 +100111100100 +000000010001 +010111100100 +011100001010 +111011101100 +111101001100 +111011110000 +101100011011 +000110001111 +011110010111 +000111101010 +001110001101 +010010001011 +000100010101 +101001001101 +001001010110 +011100110010 +010111100111 +100010111111 +011010001111 +000010010001 +111000000000 +111100110111 +100101000101 +101001000111 +101001000001 +101100001111 +000100101010 +111100000001 +111100101110 +110100101110 +001011011010 +000010001101 +101100000100 +010100000101 +010010000011 +000010100101 +110100111111 +001011101110 +011000100010 +000111100101 +010111110100 +100010110110 +000111010001 +000011100000 +110010000100 +100111001101 +010011111110 +000010100000 +001001001010 +110101100111 +011100101001 +001011001110 +111010000000 +010011001000 +001000010011 +010001011000 +010010100110 +100010000000 +100010101100 +101011111001 +010001001101 +110111110100 +110000000000 +001100000110 +000111110010 +000100011011 +001100001000 +011000000010 +001101100000 +111101000111 +101110001100 +100011101111 +010001010011 +110010011110 +111010101110 +011011010101 +100110001110 +110000101110 +101111111001 +101110110011 +000100001111 +001101110011 +011111100011 +100001100001 +101100001010 +010010110110 +110110010000 +110000110001 +100001100000 +110111010001 +000101100111 +100110100010 +111100001001 +111001010100 +001111101001 +101000010110 +000001110011 +110111101101 +010100000111 +110100111011 +111000011101 +110011001001 +110111101000 +010010100100 +000011111010 +010101001000 +101110011001 +100000011001 +111010001011 +000000100001 +111110010100 +100101001101 +010110011010 +000110010110 +111000001110 +111111100001 +101111100011 +111101100011 +011100000011 +110111111000 +011111110110 +010010110111 +101001000110 +011111000100 +100111100011 +101101100101 +000000101111 +001101010000 +000010000001 +000001110101 +001011000010 +000011000111 +001011001111 +101101011110 +100100110001 +110000011011 +010010011100 +011100110101 +000011110100 +010000110001 +100010111001 +001101001111 +101101100100 +010001111001 +111110011010 +100001100011 +101011001000 +100110111111 +100101001010 +011110101111 +001101011001 +000101000001 +001010111000 +010100110111 +101111110100 +001110111011 +101100000110 +101011001001 +100011011000 +100100111100 +111110110000 +000001100010 +000101100000 +110000100001 +011000011010 +110000010111 +111111001010 +001111100001 +111011110100 +100110010011 +111010110000 +110111010000 +100010011110 +110011100110 diff --git a/2021/src/day3.rs b/2021/src/day3.rs new file mode 100644 index 0000000..eaff33a --- /dev/null +++ b/2021/src/day3.rs @@ -0,0 +1,99 @@ +//! --- Day 3: Binary Diagnostic --- +//! The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case. +//! +//! The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the power consumption. +//! +//! You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate. +//! +//! Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example, given the following diagnostic report: +//! +//! 00100 +//! 11110 +//! 10110 +//! 10111 +//! 10101 +//! 01111 +//! 00111 +//! 11100 +//! 10000 +//! 11001 +//! 00010 +//! 01010 +//! Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1. +//! +//! The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0. +//! +//! The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110. +//! +//! So, the gamma rate is the binary number 10110, or 22 in decimal. +//! +//! The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198. +//! +//! Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.) + +use anyhow::Result; +use aoc_runner_derive::aoc; + +#[aoc(day3, part1)] +fn part1(input: &str) -> Result { + let lines: Vec<_> = input.trim().split("\n").collect(); + let num_bits = lines[0].len(); + let majority = lines.len() / 2; + let mut bits = vec![0; num_bits]; + lines.iter().for_each(|l| { + for (i, c) in l.chars().enumerate() { + if c == '1' { + bits[i] += 1; + } + } + }); + let mut gamma: u64 = 0; + for (i, &b) in bits.iter().rev().enumerate() { + if b > majority { + gamma |= 1 << i; + } + } + let mask = (1 << (num_bits)) - 1; + let epsilon = (!gamma) & mask; + Ok(epsilon * gamma) +} + +/* +#[aoc(day3, part2)] +fn part2(depths: &[u32]) -> Result { +todo!("part2") +Ok(()) +} +*/ + +#[test] +fn test_part1() -> Result<()> { + let input = r#" +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 +"# + .trim(); + assert_eq!(part1(input)?, 198); + Ok(()) +} + +/* +#[test] +fn test_part2()->Result<()> { +let input = r#" +"# +.trim(); +assert_eq!(part2(&parse(input)?)?, TODO); +Ok(()) +} +*/ diff --git a/2021/src/lib.rs b/2021/src/lib.rs index 44b33e3..f647adc 100644 --- a/2021/src/lib.rs +++ b/2021/src/lib.rs @@ -1,5 +1,6 @@ pub mod day1; pub mod day2; +pub mod day3; use aoc_runner_derive::aoc_lib;