From d53b80fb4acd338bb94d63d0029823b2151bed5a Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Wed, 15 Dec 2021 15:15:29 -0500 Subject: [PATCH] Port day03 part1 to rust --- rust-rewrite/day03/Cargo.toml | 8 + rust-rewrite/day03/day03.in | 1000 ++++++++++++++++++++++++++++++++ rust-rewrite/day03/src/main.rs | 55 ++ 3 files changed, 1063 insertions(+) create mode 100644 rust-rewrite/day03/Cargo.toml create mode 100644 rust-rewrite/day03/day03.in create mode 100644 rust-rewrite/day03/src/main.rs diff --git a/rust-rewrite/day03/Cargo.toml b/rust-rewrite/day03/Cargo.toml new file mode 100644 index 0000000..2804bab --- /dev/null +++ b/rust-rewrite/day03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust-rewrite/day03/day03.in b/rust-rewrite/day03/day03.in new file mode 100644 index 0000000..4d9afa4 --- /dev/null +++ b/rust-rewrite/day03/day03.in @@ -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/rust-rewrite/day03/src/main.rs b/rust-rewrite/day03/src/main.rs new file mode 100644 index 0000000..8a1a7e9 --- /dev/null +++ b/rust-rewrite/day03/src/main.rs @@ -0,0 +1,55 @@ +use std::fs; + +fn main() { + let file = fs::read_to_string("day03.in").unwrap(); + let data = ingest_data(file); + println!("=== PART 1 ==="); + part1(&data); + println!("=== PART 2 ==="); +} + +fn ingest_data(input: String) -> Vec { + let mut data: Vec = vec![]; + for line in input.strip_suffix('\n').unwrap().split('\n') { + data.push(line.to_string()); + } + data +} + +fn common_bit(data: &Vec, bit: usize, invert: bool) -> char { + let mut bit_count = (0, 0); + for x in data { + match x.chars().nth(bit).unwrap() { + '0' => bit_count.0 += 1, + '1' => bit_count.1 += 1, + _ => (), + } + } + if bit_count.0 <= bit_count.1 { + if invert { + '0' + } else { + '1' + } + } else { + if invert { + '1' + } else { + '0' + } + } +} + +fn part1(data: &Vec) { + let mut gamma: String = "".to_string(); + let mut epsilon: String = "".to_string(); + for bit in 0..data[0].len() { + gamma.push(common_bit(data, bit, false)); + epsilon.push(common_bit(data, bit, true)); + } + let gamma_i = usize::from_str_radix(&gamma, 2).unwrap(); + let epsilon_i = usize::from_str_radix(&epsilon, 2).unwrap(); + println!("Gamma: {}", gamma_i); + println!("Epsilon: {}", epsilon_i); + println!("Power consumption: {}", gamma_i * epsilon_i); +}