From: Brett Parker Date: Fri, 3 Dec 2021 13:57:11 +0000 (+0000) Subject: Advent of code Day 3 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2021.git/commitdiff_plain/37f395fcc0f553c839358bbc9f840aa5f6cf83b1?ds=sidebyside Advent of code Day 3 --- diff --git a/day03/diag.sh b/day03/diag.sh new file mode 100755 index 0000000..9e8c823 --- /dev/null +++ b/day03/diag.sh @@ -0,0 +1,152 @@ +#!/bin/bash + +set -e +set -u + +filename="${1:-example.txt}" + +exec 3<"$filename" + +line_length=0 +declare -a most_common=( 0 ) +declare -a all_binaries +line_count=0 + +while read -u 3 line; do + all_binaries+=($line) + if [ $line_length -eq 0 ]; then + line_length=${#line} + fi + for (( i=0; i < $line_length; i++ )); do + if [ ${#most_common[@]} -lt $i ]; then + most_common[$i]=0 + fi + if [ ${line:$i:1} -eq 1 ]; then + ((most_common[$i]+=1)) + fi + done + ((line_count+=1)) +done + +half_line_count=$((line_count/2)) +binary_string="" +inverse_binary_string="" + +for (( i=0; i < $line_length; i++ )); do + if [ ${most_common[$i]} -gt $half_line_count ]; then + binary_string+=1 + inverse_binary_string+=0 + else + binary_string+=0 + inverse_binary_string+=1 + fi +done + +total=$((2#$binary_string * 2#$inverse_binary_string)) + +echo Answer: $total + +declare -a co2_scrubber +declare -a o2_generator + +# first populate co2_scrubbers and o2_generator + +co2_filter_digit=1 +o2_generator_digit=0 + +if [ ${most_common[0]} -gt $half_line_count ]; then + co2_filter_digit=0 + o2_generator_digit=1 +else + co2_filter_digit=1 + o2_generator_digit=0 +fi + +for val in "${all_binaries[@]}"; do + if [ ${val:0:1} -eq $co2_filter_digit ]; then + co2_scrubber+=($val) + fi + if [ ${val:0:1} -eq $o2_generator_digit ]; then + o2_generator+=($val) + fi +done + +get_most_common() { + offset=$1 + local -n arr=$2 + ones=0 + zeros=0 + + for val in "${arr[@]}"; do + part=${val:$offset:1} + if [ $part -eq 1 ]; then + ((ones+=1)) + else + ((zeros+=1)) + fi + done + + if [ $ones -gt $zeros ]; then + echo 1 + elif [ $ones -lt $zeros ]; then + echo 0 + else + echo 2 + fi +} + +get_least_common() { + offset=$1 + array_name=$2 + + val=$(get_most_common $offset $array_name) + case $val in + 0) + echo 1 + ;; + 1) + echo 0 + ;; + 2) + echo 2 + ;; + esac +} + +filter_array() { + val=$1 + pos=$2 + local -n arr=$3 + + if [ ${#arr[@]} -eq 1 ]; then + return + fi + + declare -a new_arr + for thing in "${arr[@]}"; do + if [ ${thing:$pos:1} -eq $val ]; then + new_arr+=($thing) + fi + done + + arr=("${new_arr[@]}") +} + +for ((i=1; i < $line_length; i++)); do + co2_filter=$(get_least_common $i co2_scrubber) + o2_filter=$(get_most_common $i o2_generator) + + if [ $co2_filter -eq 2 ]; then + co2_filter=0 + fi + + if [ $o2_filter -eq 2 ]; then + o2_filter=1 + fi + + filter_array $co2_filter $i co2_scrubber + filter_array $o2_filter $i o2_generator +done + +# we've only got 1 in each - so... +echo Life Support Rating: $((2#${o2_generator[0]} * 2#${co2_scrubber[0]})) diff --git a/day03/example.txt b/day03/example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/day03/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/day03/input.txt b/day03/input.txt new file mode 100644 index 0000000..4d9afa4 --- /dev/null +++ b/day03/input.txt @@ -0,0 +1,1000 @@ +111010111011 +001011110010 +000110111100 +110101110001 +101110011111 +110011111001 +100000110011 +011100101100 +111111000111 +010100001000 +010111000100 +100001000110 +110000011011 +011001101110 +001010001101 +010001010010 +000011011101 +000110100001 +000100111000 +011101000100 +011000101001 +110111000110 +011001010111 +011110101110 +010001111111 +010110001011 +001110010111 +000110000101 +110011110000 +010010101000 +101010011001 +010000000000 +101110000001 +001111101010 +000011000110 +101100100001 +000001110011 +100110011111 +111011111000 +010010001101 +000000001111 +101011100011 +010110001001 +101101101001 +101000001101 +100111111100 +100001011101 +110100111101 +010110000111 +110000001010 +010001000111 +001010110010 +000000101011 +001111011000 +011000010100 +000101110011 +011000010011 +001000101100 +100110000010 +000100110110 +011010010000 +101101010100 +111101101011 +101101000100 +001001010011 +001011000011 +101100100010 +010001110001 +000101111010 +010110001110 +000111000011 +011111111000 +011001100101 +111000111111 +101001001001 +010000001000 +010101100100 +101001011100 +010111001001 +110100011010 +000011111111 +000011111101 +000000111011 +010101000111 +110100001111 +110010101000 +011011111101 +110001111011 +110011011110 +001110011100 +101001110010 +111110100101 +101110110000 +101100000100 +101110100010 +111000111110 +010010100010 +111111101000 +001110001101 +000001110110 +000100111100 +111000001010 +100001010100 +000100001010 +000010100101 +100011110110 +111000011111 +111100111110 +000101101011 +001010000100 +011011111011 +100000111001 +010101100000 +101100111010 +100100011011 +110000101000 +000011111110 +000110010000 +110110100100 +100110100100 +110101100011 +011011000101 +110000000010 +001111100001 +000000111001 +100111001000 +000110000000 +011111001001 +100111111000 +110010000111 +101000101100 +100011011110 +100100011100 +000011001100 +000000110111 +001011100110 +000000100001 +110000010110 +001111001000 +111111111011 +000001010001 +010010011100 +011100001110 +100010000010 +101000010001 +110000110101 +010011110010 +110000111111 +100011111100 +101100110100 +010001110110 +000000110110 +111001000101 +111011011011 +101010111011 +001111011101 +111011000000 +010101110101 +101010000111 +111101000110 +110111000101 +001110111110 +101100001101 +101111010001 +001111000110 +011100110110 +001110100111 +000101100010 +111100010010 +010101010011 +000010001011 +010101011110 +110000001000 +011110000111 +100001111100 +011011111100 +011000001111 +101100011101 +111100010000 +111010110011 +110001010100 +111010110010 +100010101110 +111101110111 +001011101010 +100111100001 +001011100100 +110001000111 +001111111100 +100100011000 +111100011110 +011110011111 +001110100010 +010010011101 +001001101101 +100101011111 +100100110111 +001001000000 +111001011000 +000110000010 +000111000111 +000101001100 +011011100001 +010000100110 +011100011111 +110010100110 +101101100111 +101101001110 +101100011011 +111100101100 +011001110101 +110101111000 +111011010001 +011110111011 +110100011001 +101101011000 +101101111110 +111101101001 +111011011101 +001100001000 +001101111010 +001001010001 +001111110000 +000001000111 +010101000100 +111100100101 +100001011000 +000101111001 +101111001010 +100000110111 +111010000101 +110110101111 +011000110001 +111101010011 +111001110100 +000010000110 +001100001101 +001111110101 +001000011011 +011101100000 +111011101100 +001100111100 +101111001111 +110010001001 +000100011101 +000111011111 +001100011011 +100110000000 +011010110001 +110000010010 +011011101111 +110011101011 +100110101010 +111011110100 +010011000000 +111110011000 +000111010111 +101001100010 +101110010100 +101000010011 +001110001100 +011110000011 +110001001010 +001010110111 +011001101001 +110001110011 +100101011011 +101010100110 +010011001101 +010000001111 +110111011110 +110011100011 +100011101001 +011111000000 +010001001001 +110001011110 +011000101011 +001000001111 +010110111101 +110001100100 +101101111111 +001101010100 +000001011010 +011010110010 +110011101000 +000001111000 +111001100111 +001011000010 +000000011000 +000101010101 +111100001001 +011000001010 +101010101011 +011101001111 +010101110010 +100101100000 +110000110000 +011000110010 +100101001000 +100101011001 +100110001001 +011010010001 +001011111001 +001111100111 +101000101101 +010111111001 +011100001000 +100111111111 +011110111000 +110101101111 +100001001000 +101100101001 +000001011000 +101010101001 +010011000100 +101100011111 +000111100110 +111010101100 +010011111101 +011010110100 +011101100011 +001101001001 +111110001101 +011010010110 +011000001110 +001010011001 +111111101011 +011100111010 +001111000001 +101111100100 +101000110000 +010101100111 +100100000101 +110100101000 +011010011101 +111110000110 +011001000111 +101101000000 +110111010110 +011100111111 +101101110101 +000000000100 +100100010001 +000000110011 +011110100000 +010111010010 +001010010101 +110111100101 +100011100001 +011011010111 +100001001100 +101101000001 +000111010011 +101001100100 +001010011101 +010000100100 +101011111101 +010110000011 +110010110111 +111100110001 +100010011011 +000000101001 +010011110011 +110111011111 +100111000000 +001111111110 +111100100111 +100010011001 +110011010110 +110000110011 +101001001000 +111010000011 +010010011000 +100000000111 +001011011101 +100010111001 +000011101101 +100001100001 +011011100110 +101001010001 +110001011101 +101101010111 +010001111010 +111011001110 +010001100111 +011000000011 +101000001001 +010100000111 +000011000001 +011001110001 +111010001110 +010011111110 +011100100110 +111111110001 +010110101110 +100010010001 +100100100111 +100111010011 +001101111111 +111100011101 +011101010101 +100111010100 +101011101101 +011001000110 +100110010111 +110101001110 +101000101110 +001001000100 +001100111111 +111110111001 +000011101100 +001001100010 +001101001111 +001111110011 +011000110011 +111110110100 +000111001110 +001101110101 +001011010000 +001100011111 +010010101001 +110000011100 +010010010101 +001110001000 +000011010000 +001011011100 +100111010000 +101110000101 +100111110000 +000001011100 +101000110100 +010111011001 +000001001111 +110110001100 +111110011111 +011010000100 +010110111011 +011101000111 +010100011010 +011110111111 +000011001010 +011101001001 +101010011011 +000011111100 +101011010000 +001101010101 +101011010111 +111000011010 +110101011101 +110111010100 +110100101100 +110110001001 +011000001001 +111100000100 +000100110101 +001101101110 +001011111100 +000000010001 +100011011001 +001000100111 +111111000001 +000111110101 +001000101001 +110111111010 +110010011000 +010100010110 +000111001001 +100000111010 +110011011100 +011101000010 +010100010111 +001001000101 +001010110011 +100000101110 +100110000011 +110011111100 +110111001001 +001000111101 +011000100000 +000100101111 +010111110111 +110111001101 +110001001111 +001000010101 +001111001001 +001001100100 +011110100111 +001100100000 +010111100011 +001000001011 +010010011110 +000011110110 +110101011010 +011101100010 +011111111001 +111001100000 +010100110101 +011010110111 +101011110001 +011001010011 +100011000011 +011010101011 +000100100111 +010110010001 +010100010000 +101101110001 +011100011100 +001101001011 +101110111111 +100101000010 +111001111101 +111110110111 +010111110100 +101000100010 +101010001000 +010000001110 +001000101011 +100011111101 +001100010010 +011111110011 +100101001110 +000110011110 +000001011001 +001000100001 +111100000000 +010101100001 +011100111000 +110100000011 +110011010001 +001010000001 +000100011010 +111010010110 +010000001100 +011011011011 +100011111110 +101101000110 +111000010100 +011011001100 +101000111111 +000101101101 +111010101010 +011010010011 +111001001100 +001101001000 +110101100101 +011000010101 +000100000101 +011101111111 +000101001001 +110101100111 +111000001100 +110001111010 +100110010100 +010100010010 +010000110110 +100111101101 +101110101000 +101001011101 +110101010101 +100111011000 +100001011011 +100100100011 +100110100011 +100000010001 +110110101100 +001010001111 +101001111000 +010011110111 +011100100101 +000111010110 +101010100001 +100101001101 +100001010011 +000000011011 +110010000100 +010011010101 +101110011100 +101101011110 +101110110110 +101111001101 +001001111011 +011100100011 +000100110001 +000110011101 +011011000100 +100011101110 +010101111111 +011110000100 +101001101111 +110100111010 +011011110011 +001010010110 +110010000101 +001100010011 +000100110011 +101110110010 +011101010111 +001000000101 +101101011101 +110001011011 +110011001100 +011101111101 +000111101011 +011111001000 +010100101100 +100001010001 +010000010101 +101100101110 +000010101000 +010001001111 +100001001111 +000101001110 +011111100100 +110110111000 +001010110001 +100111001101 +111100010101 +000101111000 +010001110100 +111001111100 +110111010101 +000011001001 +100010000001 +000011110000 +101110001000 +101110000010 +100010110110 +001011100011 +011010111111 +101100000111 +100000011110 +100100101001 +111100111000 +101100000011 +000000000010 +001100101010 +010111111010 +001101100010 +000110011011 +001001000110 +111010100000 +100111100100 +000110101110 +010100011000 +110100100101 +111100101101 +100011111001 +011001010101 +001100110000 +111010101001 +001000011000 +000100101010 +101000110010 +111011001100 +110111101000 +010101011001 +011001111110 +000101100001 +011100000111 +011101111110 +000101000001 +110011000010 +011110111110 +101110001101 +010100101110 +000100001100 +001100010001 +100100101111 +111110000011 +010011011111 +100110001000 +100000001100 +001001010110 +001010000111 +010111000110 +110100000101 +000101110100 +010110110111 +010000100111 +101001000110 +111010001100 +000000111111 +101100000010 +101011001110 +111001000110 +111110010011 +001010000010 +000101010011 +011111101000 +011010011011 +010011100011 +011100010011 +100001100101 +000011110100 +110001010000 +111111010011 +100010100010 +101111111010 +001100100011 +011100000001 +010110110001 +011011011100 +010011110100 +110110110001 +111111101111 +001010101010 +011010101101 +011000110000 +010111100001 +011011111000 +010110100110 +110011100111 +011011001110 +011010111001 +010111010110 +011101100001 +101000101010 +010010101011 +111100011111 +110001101111 +010011010010 +111000100011 +000100010001 +100001111101 +001010001000 +001001100101 +010000001010 +011011010100 +110110111111 +000010110010 +000010011100 +010001000001 +110010110010 +000010011011 +111001001011 +110100110011 +101100001001 +000100000000 +101010110010 +110111100011 +000110111110 +111000011001 +001000101101 +101000011010 +101110100001 +011010100010 +001001110101 +011001011000 +100110001100 +110110011011 +111101101000 +001110101011 +000010000111 +101110100110 +000110010110 +001011111011 +000010001000 +011110111100 +000011101011 +100010001101 +001011000000 +011000000100 +110101101101 +000001101110 +000111011001 +011101011000 +000000101111 +000101111101 +101011011110 +100010000011 +111001110000 +110011110011 +000101011001 +110101010001 +010101100110 +010011000110 +111111110101 +100101010110 +001001100001 +001110011111 +000000000111 +110100001101 +101110011110 +101101110000 +100101000011 +111011000001 +100011001110 +010111011111 +000000001100 +100110001111 +110100101111 +101010111010 +110000001100 +010100101101 +110101001010 +000001100101 +111011000011 +101110100100 +110000101110 +110111000100 +101001011010 +011010010100 +101110100101 +111110100111 +101100111011 +110000100000 +111101110001 +001100011001 +001000010001 +010101001100 +110110100011 +011000100110 +111001001001 +110010110100 +010000100101 +111101111100 +101001101110 +110110011111 +100000001011 +100011011011 +101110000100 +011110010110 +101011100000 +011101101111 +001011101000 +011101011010 +110001001101 +110111101111 +100011011111 +100001100100 +111111101101 +011110000000 +101000011110 +000110001000 +000100010010 +100100010000 +111001101000 +001101101011 +110001111111 +000111000101 +100001000101 +111111111001 +000010111011 +001100000110 +110101011001 +110110000110 +100010101101 +010110010111 +011101010110 +011111101011 +111111110000 +110110100110 +111001101111 +001000001110 +111100111100 +000110110101 +000100001001 +011101110010 +111010011011 +001101111011 +110110000101 +000110110111 +111111100110 +010001110011 +100010101111 +011110010011 +111011111001 +101010110110 +110101101001 +010001011001 +101101101010 +011110101001 +001100011100 +101001000001 +000011011001 +101101100101 +011001110011 +110101000111 +001110000101 +100111011110 +011101010010 +110000101001 +010110010110 +011100110000 +110010111100 +110101001111 +111110111110 +101100001110 +111000110010 +110100111110 +101001100000 +111111100001 +011000100111 +101010001111 +010010011011 +000101011110 +011000101100 +110111101001 +110100001100 +100000011101 +110110100111 +111011011110 +100011001011 +001100100111 +001110011011 +100101111101 +111011010111 +111101111101 +010110001101 +011010101100 +111000011011 +011110000010 +110111010011 +101101001100 +100111011010 +011111111101 +101011000000 +100100101101 +111111011000 +000100101000 +100010100001 +010010100100 +001010001100 +111001010001 +010101101100 +111111110111 +000010111101 +100000001111 +110111101010 +101111100110 +010100000110 +000111011010 +111011101110 +111001001101 +011100010110 +100001100110 +011111010011 +000011100111 +010011101111 +010010000101 +100001011010 +100110111011 +010110101001 +011110011011 +011001010010 +011010011110 +110011100001 +010101001000 +111101110000 +100001100111 +011111010111 +010000011011 +111111000011 +010111111111 +100001101101 +010000100010 +110000101101 +100110101000 +111011011111 +110000010100 +111101011001 +001111100010 +000001011011 +010000010001 +111101010001 +001101000000 +000011010111 +011011010010 +001110100011 +111100010100 +110001101010 +001001111010 +111001110111 +100001101000 +110010110001 +011001101100 +011110010001 +110111011000 +110011011101 +110001010011 +111111111101 +010001010100 +110000010011 +111010110001 +011000010001 +011101111011 +101100010010 +010100111100 +011001001000 +110010010001 +000101100000 +100011101010 +100000110001 +010101101010 +000001111111 +011110011100 +111011111010 +110100101001 +001100100100 +011100101000 diff --git a/day03/task.txt b/day03/task.txt new file mode 100644 index 0000000..ca28f8e --- /dev/null +++ b/day03/task.txt @@ -0,0 +1,74 @@ +--- 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.) + +Your puzzle answer was 4160394. + +--- Part Two --- +Next, you should verify the life support rating, which can be determined by multiplying the oxygen generator rating by the CO2 scrubber rating. + +Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and consider just the first bit of those numbers. Then: + +Keep only numbers selected by the bit criteria for the type of rating value for which you are searching. Discard numbers which do not match the bit criteria. +If you only have one number left, stop; this is the rating value for which you are searching. +Otherwise, repeat the process, considering the next bit to the right. +The bit criteria depends on which type of rating value you want to find: + +To find oxygen generator rating, determine the most common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 1 in the position being considered. +To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 0 in the position being considered. +For example, to determine the oxygen generator rating value using the same example diagnostic report from above: + +Start with all 12 numbers and consider only the first bit of each number. There are more 1 bits (7) than 0 bits (5), so keep only the 7 numbers with a 1 in the first position: 11110, 10110, 10111, 10101, 11100, 10000, and 11001. +Then, consider the second bit of the 7 remaining numbers: there are more 0 bits (4) than 1 bits (3), so keep only the 4 numbers with a 0 in the second position: 10110, 10111, 10101, and 10000. +In the third position, three of the four numbers have a 1, so keep those three: 10110, 10111, and 10101. +In the fourth position, two of the three numbers have a 1, so keep those two: 10110 and 10111. +In the fifth position, there are an equal number of 0 bits and 1 bits (one each). So, to find the oxygen generator rating, keep the number with a 1 in that position: 10111. +As there is only one number left, stop; the oxygen generator rating is 10111, or 23 in decimal. +Then, to determine the CO2 scrubber rating value from the same example above: + +Start again with all 12 numbers and consider only the first bit of each number. There are fewer 0 bits (5) than 1 bits (7), so keep only the 5 numbers with a 0 in the first position: 00100, 01111, 00111, 00010, and 01010. +Then, consider the second bit of the 5 remaining numbers: there are fewer 1 bits (2) than 0 bits (3), so keep only the 2 numbers with a 1 in the second position: 01111 and 01010. +In the third position, there are an equal number of 0 bits and 1 bits (one each). So, to find the CO2 scrubber rating, keep the number with a 0 in that position: 01010. +As there is only one number left, stop; the CO2 scrubber rating is 01010, or 10 in decimal. +Finally, to find the life support rating, multiply the oxygen generator rating (23) by the CO2 scrubber rating (10) to get 230. + +Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.) + +Your puzzle answer was 4125600. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle.