Created
December 22, 2015 16:18
-
-
Save salty-horse/c9154539cb0aa78209eb to your computer and use it in GitHub Desktop.
Advent of Code day 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate serde; // 0.6.6 | |
extern crate serde_json; // 0.6.0 | |
use std::fs::File; | |
use std::io::BufReader; | |
use serde_json::Value as Value; | |
fn sum_numbers(json: &Value) -> i64 { | |
match *json { | |
Value::I64(n) => n as i64, | |
Value::U64(n) => n as i64, | |
Value::Array(ref vec) => vec.iter().fold(0, |acc, ref item| acc + sum_numbers(&item)), | |
Value::Object(ref map) => { | |
let has_red = map.values().any({|x| match *x { | |
Value::String(ref s) if s == "red" => true, | |
_ => false, | |
}}); | |
if has_red { | |
return 0; | |
} | |
map.values().fold(0, |acc, ref item| acc + sum_numbers(&item)) | |
} | |
serde_json::Value::F64(n) => panic!("Unexpected float {}", n), | |
_ => 0, | |
} | |
} | |
fn main() { | |
let input_fname = "day12_input.txt"; | |
let f = File::open(input_fname).unwrap(); | |
let json: Value = serde_json::from_reader(BufReader::new(f)).unwrap(); | |
println!("Result: {}", sum_numbers(&json)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment