From 268e1a82fa61531edaaec472dbd5cd7df044176d Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 15 Dec 2021 22:33:31 -0800 Subject: 16, yay --- 16/Cargo.toml | 11 +++ 16/input | 1 + 16/src/main.rs | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 16/tests/main.rs | 18 +++++ 4 files changed, 249 insertions(+) create mode 100644 16/Cargo.toml create mode 100644 16/input create mode 100644 16/src/main.rs create mode 100644 16/tests/main.rs (limited to '16') diff --git a/16/Cargo.toml b/16/Cargo.toml new file mode 100644 index 0000000..a8b1866 --- /dev/null +++ b/16/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_16" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/16/input b/16/input new file mode 100644 index 0000000..019f19b --- /dev/null +++ b/16/input @@ -0,0 +1 @@ +220D790065B2745FF004672D99A34E5B33439D96CEC80373C0068663101A98C406A5E7395DC1804678BF25A4093BFBDB886CA6E11FDE6D93D16A100325E5597A118F6640600ACF7274E6A5829B00526C167F9C089F15973C4002AA4B22E800FDCFD72B9351359601300424B8C9A00BCBC8EE069802D2D0B945002AB2D7D583E3F00016B05E0E9802BA00B4F29CD4E961491CCB44C6008E80273C393C333F92020134B003530004221347F83A200D47F89913A66FB6620016E24A007853BE5E944297AB64E66D6669FCEA0112AE06009CAA57006A0200EC258FB0440010A8A716A321009DE200D44C8E31F00010887B146188803317A3FC5F30056C0150004321244E88C000874468A91D2291802B25EB875802B28D13550030056C0169FB5B7ECE2C6B2EF3296D6FD5F54858015B8D730BB24E32569049009BF801980803B05A3B41F1007625C1C821256D7C848025DE0040E5016717247E18001BAC37930E9FA6AE3B358B5D4A7A6EA200D4E463EA364EDE9F852FF1B9C8731869300BE684649F6446E584E61DE61CD4021998DB4C334E72B78BA49C126722B4E009C6295F879002093EF32A64C018ECDFAF605989D4BA7B396D9B0C200C9F0017C98C72FD2C8932B7EE0EA6ADB0F1006C8010E89B15A2A90021713610C202004263E46D82AC06498017C6E007901542C04F9A0128880449A8014403AA38014C030B08012C0269A8018E007A801620058003C64009810010722EC8010ECFFF9AAC32373F6583007A48CA587E55367227A40118C2AC004AE79FE77E28C007F4E42500D10096779D728EB1066B57F698C802139708B004A5C5E5C44C01698D490E800B584F09C8049593A6C66C017100721647E8E0200CC6985F11E634EA6008CB207002593785497652008065992443E7872714 diff --git a/16/src/main.rs b/16/src/main.rs new file mode 100644 index 0000000..f4f6253 --- /dev/null +++ b/16/src/main.rs @@ -0,0 +1,219 @@ +use advent_lib::prelude::*; + +#[derive(Debug)] +struct Packet { + version: u8, + type_id: u8, + content: Vec, + subpackets: Vec, +} + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut bit_string: Vec = Vec::new(); + for c in input[0].chars() { + let value = c.to_digit(16).unwrap() as u8; + for i in 0 .. 4 { + let bit = (value >> (3 - i)) & 1; + bit_string.push(bit); + } + } + + let mut iter = bit_string.iter().peekable(); + let (version_total, _top_packet, result) = read_packet(&mut iter).unwrap(); + println!("{}", version_total); + println!("{}", result); + + Ok(()) +} + + +fn read_packet<'a>(iter: &mut impl Iterator) -> Result<(u64, Packet, u128)> +{ + let mut version_total: u64 = 0; + + let version = bits_to_u8(take_bits(iter, 3)?); + let type_id = bits_to_u8(take_bits(iter, 3)?); + + version_total += version as u64; + + if type_id == 4 { + let mut result = 0; + loop { + let bits = take_bits(iter, 5)?; + + let bits_value = bits_to_u128(bits[1..].to_vec()); + result = result << 4; + result += bits_value; + + if bits[0] == 0 { + break; + } + } + + Ok((version_total, Packet { + version, + type_id, + content: Vec::new(), + subpackets: Vec::new(), + }, result)) + } else { + let mut subpackets = Vec::new(); + let mut parameters = Vec::new(); + + let length_type_id = bits_to_u8(take_bits(iter, 1)?); + if length_type_id == 0 { + let length_in_bits = bits_to_u64(take_bits(iter, 15)?); + + let mut content_bits: Vec = Vec::new(); + for bit in iter.take(length_in_bits as usize) { + content_bits.push(*bit); + } + + let mut content_bits = content_bits.iter().peekable(); + + loop { + match read_packet(&mut content_bits) { + Err(_) => { + break; + } + Ok((sub_version_total, subpacket, value)) => { + version_total += sub_version_total; + subpackets.push(subpacket); + parameters.push(value); + } + } + } + } else { + let n_subpackets = bits_to_u64(take_bits(iter, 11)?); + + for _ in 0 .. n_subpackets { + match read_packet(iter) { + Err(_) => { + return Err(advent_lib::error::Error::Unspecified); + } + Ok((sub_version_total, subpacket, value)) => { + version_total += sub_version_total; + subpackets.push(subpacket); + parameters.push(value); + } + } + } + } + + let mut result = 0; + match type_id { + 0 => { + result = 0; + for parameter in ¶meters { + result += parameter; + } + } + 1 => { + result = 1; + for parameter in ¶meters { + result *= parameter; + } + } + 2 => { + result = parameters[0]; + for parameter in ¶meters { + if *parameter < result { + result = *parameter; + } + } + } + 3 => { + result = parameters[0]; + for parameter in ¶meters { + if *parameter > result { + result = *parameter; + } + } + } + 5 => { + result = if parameters[0] > parameters[1] { 1 } else { 0 }; + } + 6 => { + result = if parameters[0] < parameters[1] { 1 } else { 0 }; + } + 7 => { + result = if parameters[0] == parameters[1] { 1 } else { 0 }; + } + _ => { } + } + + Ok((version_total, Packet { + version, + type_id, + content: Vec::new(), + subpackets: subpackets, + }, result)) + } +} + + +fn take_bits<'a>(iter: &mut impl Iterator, count: usize) + -> Result> +{ + let mut result: Vec = Vec::new(); + + for _ in 0 .. count { + match iter.next() { + None => { + return Err(advent_lib::error::Error::Unspecified); + } + Some(bit) => { + result.push(*bit); + } + } + } + + Ok(result) +} + + +fn bits_to_u8(bits: Vec) -> u8 { + let mut result = 0; + + for bit in &bits { + result *= 2; + result += bit; + } + + result +} + + +fn bits_to_u64(bits: Vec) -> u64 { + let mut result = 0; + + for bit in &bits { + result *= 2; + result += *bit as u64; + } + + result +} + + +fn bits_to_u128(bits: Vec) -> u128 { + let mut result = 0; + + for bit in &bits { + result *= 2; + result += *bit as u128; + } + + result +} + diff --git a/16/tests/main.rs b/16/tests/main.rs new file mode 100644 index 0000000..ba7a189 --- /dev/null +++ b/16/tests/main.rs @@ -0,0 +1,18 @@ +use assert_cmd::prelude::*; +//use predicates::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box> { + let mut command = Command::cargo_bin("advent_16")?; + + command.arg("input"); + command.assert().success().stdout( + "989\n\ + 7936430475134\n"); + + + Ok(()) +} + -- cgit 1.4.1