From cce9dc3e4eee41d5898b3e6576e0528c58f6e401 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 10 Dec 2016 11:06:36 +0200 Subject: AOC10 --- aoc10/Cargo.lock | 98 +++++++++++++++++++++++ aoc10/Cargo.toml | 7 ++ aoc10/input.txt | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ aoc10/src/main.rs | 155 ++++++++++++++++++++++++++++++++++++ 4 files changed, 491 insertions(+) create mode 100644 aoc10/Cargo.lock create mode 100644 aoc10/Cargo.toml create mode 100644 aoc10/input.txt create mode 100644 aoc10/src/main.rs (limited to 'aoc10') diff --git a/aoc10/Cargo.lock b/aoc10/Cargo.lock new file mode 100644 index 0000000..f8f7189 --- /dev/null +++ b/aoc10/Cargo.lock @@ -0,0 +1,98 @@ +[root] +name = "aoc10" +version = "0.1.0" +dependencies = [ + "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70" +"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" +"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" +"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" +"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" +"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" +"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/aoc10/Cargo.toml b/aoc10/Cargo.toml new file mode 100644 index 0000000..d6abdaf --- /dev/null +++ b/aoc10/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc10" +version = "0.1.0" +authors = ["Justin Worthe "] + +[dependencies] +regex = "0.1" \ No newline at end of file diff --git a/aoc10/input.txt b/aoc10/input.txt new file mode 100644 index 0000000..aeda3b5 --- /dev/null +++ b/aoc10/input.txt @@ -0,0 +1,231 @@ +bot 59 gives low to bot 176 and high to bot 120 +bot 92 gives low to bot 42 and high to bot 187 +value 31 goes to bot 114 +bot 182 gives low to bot 49 and high to bot 176 +bot 17 gives low to bot 181 and high to bot 162 +bot 36 gives low to bot 118 and high to bot 121 +bot 118 gives low to bot 164 and high to bot 55 +bot 172 gives low to bot 79 and high to bot 123 +bot 51 gives low to bot 60 and high to bot 31 +bot 48 gives low to bot 107 and high to bot 58 +bot 142 gives low to output 6 and high to bot 35 +bot 133 gives low to output 4 and high to bot 47 +bot 134 gives low to bot 122 and high to bot 66 +bot 106 gives low to bot 155 and high to bot 99 +bot 77 gives low to bot 93 and high to bot 84 +bot 9 gives low to bot 173 and high to bot 197 +bot 64 gives low to bot 123 and high to bot 48 +bot 177 gives low to bot 21 and high to bot 132 +bot 94 gives low to bot 6 and high to bot 25 +bot 126 gives low to bot 193 and high to bot 56 +bot 74 gives low to bot 187 and high to bot 125 +bot 80 gives low to bot 41 and high to bot 191 +bot 62 gives low to bot 157 and high to bot 138 +bot 66 gives low to bot 1 and high to bot 209 +bot 90 gives low to bot 104 and high to bot 34 +bot 68 gives low to bot 23 and high to bot 87 +bot 121 gives low to bot 55 and high to bot 126 +bot 122 gives low to bot 137 and high to bot 1 +bot 209 gives low to bot 168 and high to bot 26 +bot 141 gives low to bot 170 and high to bot 6 +bot 149 gives low to bot 62 and high to bot 13 +bot 120 gives low to bot 179 and high to bot 71 +bot 160 gives low to bot 194 and high to bot 151 +bot 86 gives low to bot 96 and high to bot 106 +value 13 goes to bot 9 +bot 180 gives low to bot 189 and high to bot 27 +value 67 goes to bot 88 +bot 169 gives low to bot 99 and high to bot 159 +bot 56 gives low to bot 98 and high to bot 147 +bot 197 gives low to bot 174 and high to bot 81 +bot 57 gives low to bot 113 and high to bot 179 +bot 39 gives low to bot 115 and high to bot 3 +bot 79 gives low to bot 22 and high to bot 40 +bot 161 gives low to output 14 and high to bot 185 +bot 21 gives low to bot 114 and high to bot 119 +bot 136 gives low to bot 28 and high to bot 158 +bot 105 gives low to bot 89 and high to bot 19 +bot 168 gives low to bot 126 and high to bot 26 +bot 193 gives low to bot 64 and high to bot 98 +bot 186 gives low to bot 86 and high to bot 178 +value 11 goes to bot 165 +bot 33 gives low to bot 116 and high to bot 150 +bot 32 gives low to bot 154 and high to bot 206 +bot 166 gives low to bot 33 and high to bot 139 +value 7 goes to bot 63 +bot 203 gives low to bot 172 and high to bot 64 +bot 200 gives low to bot 94 and high to bot 25 +value 43 goes to bot 76 +bot 145 gives low to bot 103 and high to bot 128 +bot 119 gives low to bot 186 and high to bot 97 +bot 12 gives low to bot 31 and high to bot 4 +bot 23 gives low to bot 198 and high to bot 171 +bot 34 gives low to bot 10 and high to bot 20 +bot 198 gives low to bot 43 and high to bot 17 +bot 50 gives low to output 1 and high to bot 127 +bot 155 gives low to bot 191 and high to bot 32 +bot 206 gives low to bot 12 and high to bot 43 +bot 96 gives low to bot 80 and high to bot 155 +bot 93 gives low to bot 44 and high to bot 70 +bot 24 gives low to bot 85 and high to bot 83 +bot 30 gives low to bot 159 and high to bot 68 +bot 55 gives low to bot 203 and high to bot 193 +bot 199 gives low to bot 68 and high to bot 135 +bot 170 gives low to bot 97 and high to bot 5 +bot 65 gives low to bot 152 and high to bot 194 +bot 43 gives low to bot 4 and high to bot 181 +bot 113 gives low to output 9 and high to bot 161 +bot 81 gives low to bot 141 and high to bot 94 +value 29 goes to bot 7 +bot 46 gives low to bot 175 and high to bot 195 +value 47 goes to bot 21 +value 23 goes to bot 42 +bot 13 gives low to bot 138 and high to bot 61 +bot 135 gives low to bot 87 and high to bot 111 +bot 194 gives low to bot 190 and high to bot 82 +value 73 goes to bot 109 +bot 154 gives low to bot 51 and high to bot 12 +bot 1 gives low to bot 18 and high to bot 209 +bot 98 gives low to bot 48 and high to bot 45 +bot 147 gives low to bot 45 and high to bot 95 +bot 47 gives low to output 19 and high to bot 152 +bot 26 gives low to bot 56 and high to bot 147 +bot 179 gives low to bot 161 and high to bot 71 +bot 148 gives low to bot 204 and high to bot 137 +bot 5 gives low to bot 67 and high to bot 85 +bot 174 gives low to bot 132 and high to bot 141 +bot 8 gives low to bot 13 and high to bot 75 +bot 82 gives low to bot 146 and high to bot 22 +bot 123 gives low to bot 40 and high to bot 107 +bot 99 gives low to bot 32 and high to bot 201 +bot 41 gives low to bot 196 and high to bot 192 +bot 139 gives low to bot 150 and high to bot 153 +bot 11 gives low to output 16 and high to bot 113 +bot 72 gives low to bot 65 and high to bot 160 +bot 195 gives low to bot 133 and high to bot 183 +bot 54 gives low to output 12 and high to output 10 +bot 158 gives low to bot 102 and high to bot 110 +bot 112 gives low to bot 19 and high to bot 118 +bot 31 gives low to bot 208 and high to bot 143 +bot 167 gives low to bot 7 and high to bot 96 +bot 63 gives low to bot 92 and high to bot 74 +bot 116 gives low to bot 20 and high to bot 131 +bot 184 gives low to bot 39 and high to bot 3 +bot 162 gives low to bot 205 and high to bot 39 +bot 108 gives low to output 11 and high to bot 175 +value 53 goes to bot 207 +bot 111 gives low to bot 202 and high to bot 184 +bot 25 gives low to bot 24 and high to bot 83 +value 71 goes to bot 77 +bot 69 gives low to bot 142 and high to bot 0 +bot 146 gives low to output 13 and high to bot 53 +bot 7 gives low to bot 76 and high to bot 80 +bot 131 gives low to bot 73 and high to bot 204 +bot 102 gives low to bot 195 and high to bot 117 +bot 76 gives low to bot 165 and high to bot 41 +bot 153 gives low to bot 148 and high to bot 122 +bot 208 gives low to bot 90 and high to bot 163 +bot 70 gives low to bot 144 and high to bot 78 +bot 125 gives low to bot 8 and high to bot 156 +bot 83 gives low to bot 199 and high to bot 135 +bot 75 gives low to bot 61 and high to bot 104 +bot 67 gives low to bot 169 and high to bot 30 +bot 14 gives low to bot 81 and high to bot 200 +bot 159 gives low to bot 201 and high to bot 23 +value 3 goes to bot 93 +bot 110 gives low to bot 117 and high to bot 89 +bot 128 gives low to bot 129 and high to bot 182 +bot 87 gives low to bot 171 and high to bot 111 +bot 45 gives low to bot 58 and high to bot 95 +bot 4 gives low to bot 143 and high to bot 166 +bot 60 gives low to bot 156 and high to bot 208 +bot 27 gives low to bot 108 and high to bot 46 +bot 42 gives low to bot 207 and high to bot 149 +bot 117 gives low to bot 183 and high to bot 72 +bot 115 gives low to bot 153 and high to bot 134 +bot 140 gives low to bot 125 and high to bot 60 +bot 173 gives low to bot 177 and high to bot 174 +bot 138 gives low to bot 180 and high to bot 52 +bot 100 gives low to bot 38 and high to bot 59 +value 41 goes to bot 173 +value 59 goes to bot 177 +bot 165 gives low to bot 63 and high to bot 196 +bot 84 gives low to bot 70 and high to bot 78 +bot 2 gives low to bot 160 and high to bot 91 +value 61 goes to bot 29 +bot 114 gives low to bot 109 and high to bot 186 +bot 205 gives low to bot 139 and high to bot 115 +bot 175 gives low to output 17 and high to bot 133 +bot 176 gives low to bot 57 and high to bot 120 +bot 107 gives low to bot 124 and high to bot 15 +bot 52 gives low to bot 27 and high to bot 28 +bot 103 gives low to bot 50 and high to bot 129 +bot 150 gives low to bot 131 and high to bot 148 +bot 16 gives low to output 20 and high to bot 189 +bot 190 gives low to output 18 and high to bot 146 +bot 157 gives low to bot 16 and high to bot 180 +bot 10 gives low to bot 158 and high to bot 130 +bot 202 gives low to bot 162 and high to bot 184 +bot 88 gives low to bot 77 and high to bot 84 +bot 188 gives low to bot 128 and high to bot 38 +bot 58 gives low to bot 15 and high to bot 101 +bot 171 gives low to bot 17 and high to bot 202 +bot 97 gives low to bot 178 and high to bot 67 +bot 163 gives low to bot 34 and high to bot 116 +bot 124 gives low to bot 0 and high to bot 145 +bot 71 gives low to bot 185 and high to bot 54 +bot 78 gives low to bot 14 and high to bot 200 +bot 101 gives low to bot 188 and high to bot 100 +bot 189 gives low to output 7 and high to bot 108 +bot 95 gives low to bot 101 and high to bot 100 +bot 0 gives low to bot 35 and high to bot 103 +bot 207 gives low to bot 37 and high to bot 62 +bot 49 gives low to bot 11 and high to bot 57 +bot 85 gives low to bot 30 and high to bot 199 +bot 89 gives low to bot 72 and high to bot 2 +bot 3 gives low to bot 134 and high to bot 66 +bot 181 gives low to bot 166 and high to bot 205 +bot 91 gives low to bot 151 and high to bot 172 +value 17 goes to bot 167 +bot 20 gives low to bot 130 and high to bot 73 +bot 196 gives low to bot 74 and high to bot 140 +bot 18 gives low to bot 121 and high to bot 168 +bot 185 gives low to output 15 and high to bot 54 +bot 178 gives low to bot 106 and high to bot 169 +bot 129 gives low to bot 127 and high to bot 49 +bot 19 gives low to bot 2 and high to bot 164 +bot 15 gives low to bot 145 and high to bot 188 +bot 144 gives low to bot 197 and high to bot 14 +bot 201 gives low to bot 206 and high to bot 198 +bot 164 gives low to bot 91 and high to bot 203 +bot 73 gives low to bot 105 and high to bot 112 +bot 191 gives low to bot 192 and high to bot 154 +bot 109 gives low to bot 167 and high to bot 86 +bot 151 gives low to bot 82 and high to bot 79 +bot 53 gives low to output 2 and high to bot 142 +bot 37 gives low to bot 29 and high to bot 157 +value 2 goes to bot 44 +bot 204 gives low to bot 112 and high to bot 36 +bot 40 gives low to bot 69 and high to bot 124 +bot 22 gives low to bot 53 and high to bot 69 +bot 104 gives low to bot 136 and high to bot 10 +value 19 goes to bot 88 +bot 127 gives low to output 5 and high to bot 11 +bot 183 gives low to bot 47 and high to bot 65 +bot 192 gives low to bot 140 and high to bot 51 +bot 38 gives low to bot 182 and high to bot 59 +bot 61 gives low to bot 52 and high to bot 136 +bot 156 gives low to bot 75 and high to bot 90 +value 37 goes to bot 37 +bot 28 gives low to bot 46 and high to bot 102 +bot 187 gives low to bot 149 and high to bot 8 +bot 132 gives low to bot 119 and high to bot 170 +bot 44 gives low to bot 9 and high to bot 144 +bot 29 gives low to output 0 and high to bot 16 +bot 6 gives low to bot 5 and high to bot 24 +bot 137 gives low to bot 36 and high to bot 18 +bot 130 gives low to bot 110 and high to bot 105 +value 5 goes to bot 92 +bot 35 gives low to output 3 and high to bot 50 +bot 152 gives low to output 8 and high to bot 190 +bot 143 gives low to bot 163 and high to bot 33 diff --git a/aoc10/src/main.rs b/aoc10/src/main.rs new file mode 100644 index 0000000..c2c18be --- /dev/null +++ b/aoc10/src/main.rs @@ -0,0 +1,155 @@ +extern crate regex; +use regex::Regex; + +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; +use std::cmp; + +#[derive(Debug, Clone)] +struct Bot { + low: Option, + high: Option, + low_dest: Option, + high_dest: Option +} + +#[derive(Debug, Clone)] +enum Dest { + Bot(usize), + Output(usize) +} + +impl Bot { + fn new() -> Bot { + Bot {low: None, high: None, low_dest: None, high_dest: None} + } + + fn add_input(&mut self, input: i32) { + if self.low.is_none() { + self.low = Some(input); + } + else { + let other = self.low.unwrap(); //already handled none case + self.low = Some(cmp::min(input,other)); + self.high = Some(cmp::max(input,other)); + } + } + fn ready(&self) -> bool { + self.low.is_some() && self.high.is_some() && + self.low_dest.is_some() && self.high_dest.is_some() + } + fn clear(&mut self) { + self.low = None; + self.high = None; + } +} + + +fn main() { + let mut bots = build_bots_graph(); + let outputs = find_outputs(&mut bots); + println!("Outputs {:?}", outputs); +} + +fn find_outputs(bots: &mut Vec) -> Vec { + let mut output = Vec::new(); + + let mut is_stable = false; + while !is_stable { + is_stable = true; + + for i in 0..bots.len() { + if bots[i].ready() { + is_stable = false; + + let low = bots[i].low.unwrap(); + let high = bots[i].high.unwrap(); + bots[i].clear(); + + match bots[i].low_dest { + Some(Dest::Bot(j)) => { + bots[j].add_input(low); + }, + Some(Dest::Output(j)) => { + check_add_output(&mut output, j); + output[j] = low; + }, + _ => {} + }; + match bots[i].high_dest { + Some(Dest::Bot(j)) => { + bots[j].add_input(high); + }, + Some(Dest::Output(j)) => { + check_add_output(&mut output, j); + output[j] = high; + }, + _ => {} + }; + } + } + } + + output +} + +fn build_bots_graph() -> Vec { + let lines = read_file(); + let mut bots = Vec::new(); + + let value_regex = Regex::new(r"^value (\d+) goes to bot (\d+)$").unwrap(); + let give_regex = Regex::new(r"^bot (\d+) gives low to (output|bot) (\d+) and high to (output|bot) (\d+)$").unwrap(); + + for line in lines { + if value_regex.is_match(line.as_ref()) { + let cap = value_regex.captures(line.as_ref()).unwrap(); + let value = cap.at(1).unwrap().parse().unwrap(); + let bot_index = cap.at(2).unwrap().parse().unwrap(); + check_add_bot(&mut bots, bot_index); + bots[bot_index].add_input(value); + } + else if give_regex.is_match(line.as_ref()) { + let cap = give_regex.captures(line.as_ref()).unwrap(); + let give_bot_index = cap.at(1).unwrap().parse().unwrap(); + let low_is_to_output = cap.at(2).unwrap() == "output"; + let low_dest = cap.at(3).unwrap().parse().unwrap(); + let high_is_to_output = cap.at(4).unwrap() == "output"; + let high_dest = cap.at(5).unwrap().parse().unwrap(); + + check_add_bot(&mut bots, give_bot_index); + bots[give_bot_index].low_dest = if low_is_to_output { + Some(Dest::Output(low_dest)) + } else { + Some(Dest::Bot(low_dest)) + }; + bots[give_bot_index].high_dest = if high_is_to_output { + Some(Dest::Output(high_dest)) + } else { + Some(Dest::Bot(high_dest)) + }; + } + } + + bots +} + +fn check_add_bot(bots: &mut Vec, index: usize) { + while index >= bots.len() { + bots.push(Bot::new()); + } +} + +fn check_add_output(outputs: &mut Vec, index: usize) { + while index >= outputs.len() { + outputs.push(0); + } +} + +fn read_file() -> Vec { + let file = BufReader::new(File::open("input.txt").unwrap()); + file.lines() + .map(|line| line.unwrap().trim().to_string()) + .filter(|line| line.len() > 0) + .collect() +} -- cgit v1.2.3