Skip to content

Commit f5a6206

Browse files
2015, day 12
1 parent 0268f0b commit f5a6206

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ regex = "0.2"
88
rust-crypto = "^0.2"
99
fancy-regex = "0.1.0"
1010
lazy_static = "0.1.16"
11+
serde_json = "1.0"

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod year_2017;
88
extern crate crypto;
99
extern crate fancy_regex;
1010
extern crate regex;
11+
extern crate serde_json;
1112

1213
#[macro_use]
1314
extern crate lazy_static;
@@ -41,6 +42,7 @@ fn main() {
4142
(2015, 9) => year_2015::day09::run(inputs::read(year, day)),
4243
(2015, 10) => year_2015::day10::run("3113322113"),
4344
(2015, 11) => year_2015::day11::run("vzbxkghb"),
45+
(2015, 12) => year_2015::day12::run(&inputs::read_first_line(year, day)),
4446
(2015, _) => println!("work in progress..."),
4547

4648
// 2016

src/year_2015/day12.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use serde_json;
2+
use serde_json::Value;
3+
4+
pub fn run(input: &[u8]) {
5+
let part1 = solve_part1(input);
6+
println!("part 1: {}", part1);
7+
assert_eq!(part1, 119433);
8+
9+
let part2 = solve_part2(input);
10+
println!("part 2: {}", part2);
11+
assert_eq!(part2, 68466);
12+
}
13+
14+
fn solve_part1(buf: &[u8]) -> i64 {
15+
let v: Value = serde_json::from_slice(buf).unwrap();
16+
17+
return sum(v, true);
18+
}
19+
20+
fn solve_part2(buf: &[u8]) -> i64 {
21+
let v: Value = serde_json::from_slice(buf).unwrap();
22+
23+
return sum(v, false);
24+
}
25+
26+
fn sum(v: Value, include_red: bool) -> i64 {
27+
return match v {
28+
Value::Null => 0,
29+
Value::Bool(_) => 0,
30+
Value::Number(n) => n.as_i64().unwrap(),
31+
Value::String(_) => 0,
32+
Value::Array(v) => v.into_iter().map(|e| sum(e, include_red)).sum(),
33+
Value::Object(ref v) => {
34+
let mut max = 0;
35+
for v in v.values() {
36+
if (v == "red") && !include_red {
37+
return 0;
38+
}
39+
max += sum(v.clone(), include_red);
40+
}
41+
return max;
42+
}
43+
};
44+
}

src/year_2015/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pub mod day08;
99
pub mod day09;
1010
pub mod day10;
1111
pub mod day11;
12+
pub mod day12;

0 commit comments

Comments
 (0)