summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-12-02 07:26:08 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-12-02 07:26:08 +0200
commit33db46ef6dcdddeb8e9c64ca6f18a1cedfe9de36 (patch)
treee8c83fd3e00ccdf30e8c01efe6abb5032c0b03b0
parent5e5abff58c998448908acf71937915d72314c9ea (diff)
Day 2
-rw-r--r--inputs/day_2.txt16
-rw-r--r--src/bin/day_2.rs26
-rw-r--r--src/lib.rs6
3 files changed, 48 insertions, 0 deletions
diff --git a/inputs/day_2.txt b/inputs/day_2.txt
new file mode 100644
index 0000000..f2f1f5e
--- /dev/null
+++ b/inputs/day_2.txt
@@ -0,0 +1,16 @@
+493 458 321 120 49 432 433 92 54 452 41 461 388 409 263 58
+961 98 518 188 958 114 1044 881 948 590 972 398 115 116 451 492
+76 783 709 489 617 72 824 452 748 737 691 90 94 77 84 756
+204 217 90 335 220 127 302 205 242 202 259 110 118 111 200 112
+249 679 4015 106 3358 1642 228 4559 307 193 4407 3984 3546 2635 3858 924
+1151 1060 2002 168 3635 3515 3158 141 4009 3725 996 142 3672 153 134 1438
+95 600 1171 1896 174 1852 1616 928 79 1308 2016 88 80 1559 1183 107
+187 567 432 553 69 38 131 166 93 132 498 153 441 451 172 575
+216 599 480 208 224 240 349 593 516 450 385 188 482 461 635 220
+788 1263 1119 1391 1464 179 1200 621 1304 55 700 1275 226 57 43 51
+1571 58 1331 1253 60 1496 1261 1298 1500 1303 201 73 1023 582 69 339
+80 438 467 512 381 74 259 73 88 448 386 509 346 61 447 435
+215 679 117 645 137 426 195 619 268 223 792 200 720 260 303 603
+631 481 185 135 665 641 492 408 164 132 478 188 444 378 633 516
+1165 1119 194 280 223 1181 267 898 1108 124 618 1135 817 997 129 227
+404 1757 358 2293 2626 87 613 95 1658 147 75 930 2394 2349 86 385
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs
new file mode 100644
index 0000000..307029a
--- /dev/null
+++ b/src/bin/day_2.rs
@@ -0,0 +1,26 @@
+extern crate advent_of_code_2017;
+use advent_of_code_2017::*;
+
+fn main() {
+ let args = AdventArgs::init();
+ let sum = args.input.iter().map(|line| {
+ let splitline = parse_space_separated_ints(line).unwrap();
+
+ if args.part == 1 {
+ let max = splitline.iter().max().unwrap();
+ let min = splitline.iter().min().unwrap();
+ max-min
+ } else {
+ for i in 0..splitline.len() {
+ for j in 0..splitline.len() {
+ if i != j && splitline[i] % splitline[j] == 0 {
+ return splitline[i] / splitline[j];
+ }
+ }
+ }
+ panic!("Didn't find a dividing one! {:?}", splitline)
+ }
+ }).sum::<i32>();
+
+ println!("Checksum is {}", sum);
+}
diff --git a/src/lib.rs b/src/lib.rs
index c5a0f16..5d3721f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -59,3 +59,9 @@ impl AdventArgs {
.collect()
}
}
+
+pub fn parse_space_separated_ints(line: &String) -> Result<Vec<i32>, std::num::ParseIntError> {
+ line.split_whitespace()
+ .map(|x| x.parse::<i32>())
+ .collect()
+}