-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
76 lines (64 loc) · 1.86 KB
/
day12.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! [Day 12: JSAbacusFramework.io](https://adventofcode.com/2015/day/12)
use regex::Regex;
use serde_json::Value;
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i32, i32) {
(part1(data), part2(data))
}
fn part1(data: &str) -> i32 {
let re = Regex::new(r"(\-?\d+)").unwrap();
data.lines()
.map(|line| {
re.find_iter(line)
.map(|m| m.as_str().parse::<i32>().unwrap())
.sum::<i32>()
})
.sum::<i32>()
}
fn sum(v: &Value) -> i32 {
match v {
Value::Number(n) => n.as_i64().unwrap().try_into().unwrap(),
Value::Array(a) => a.iter().map(sum).sum(),
Value::Object(o) => {
// Ignore any object (and all of its children) which has any property with the value "red".
for v in o.values() {
if v.as_str() == Some("red") {
return 0;
}
}
o.values().map(sum).sum()
}
_ => 0,
}
}
fn part2(data: &str) -> i32 {
let json: Value = serde_json::from_str(data).expect("JSON was not well-formatted");
sum(&json)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test1() {
assert_eq!(part1("[1,2,3]"), 6);
assert_eq!(part1(r#"{"a":2,"b":4}"#), 6);
assert_eq!(part1("[[[3]]]"), 3);
assert_eq!(part1(r#"{"a":{"b":4},"c":-1}"#), 3);
assert_eq!(part1(r#"{"a":[-1,1]}"#), 0);
assert_eq!(part1(r#"[-1,{"a":1}]"#), 0);
assert_eq!(part1("[]"), 0);
assert_eq!(part1("{}"), 0);
}
#[test]
fn test2() {
assert_eq!(part2("[1,2,3]"), 6);
assert_eq!(part2(r#"[1,{"c":"red","b":2},3]"#), 4);
assert_eq!(part2(r#"{"d":"red","e":[1,2,3,4],"f":5}"#), 0);
assert_eq!(part2(r#"[1,"red",5]"#), 6);
}
}