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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! # Day 7: Recursive Circus
//!
//! Wandering further through the circuits of the computer, you come upon a tower of programs that
//! have gotten themselves into a bit of trouble. A recursive algorithm has gotten out of hand, and
//! now they're balanced precariously in a large tower.
//!
//! One program at the bottom supports the entire tower. It's holding a large disc, and on the disc
//! are balanced several more sub-towers. At the bottom of these sub-towers, standing on the bottom
//! disc, are other programs, each holding **their** own disc, and so on. At the very tops of these
//! sub-sub-sub-...-towers, many programs stand simply keeping the disc below them balanced but with
//! no disc of their own.
//!
//! You offer to help, but first you need to understand the structure of these towers. You ask each
//! program to yell out their **name**, their **weight**, and (if they're holding a disc) the
//! **names of the programs immediately above them** balancing on that disc. You write this
//! information down (your puzzle input). Unfortunately, in their panic, they don't do this in an
//! orderly fashion; by the time you're done, you're not sure which program gave which information.
//!
//! For example, if your list is the following:
//!
//! ```txt
//! pbga (66)
//! xhth (57)
//! ebii (61)
//! havc (66)
//! ktlj (57)
//! fwft (72) -> ktlj, cntj, xhth
//! qoyq (66)
//! padx (45) -> pbga, havc, qoyq
//! tknk (41) -> ugml, padx, fwft
//! jptl (61)
//! ugml (68) -> gyxo, ebii, jptl
//! gyxo (61)
//! cntj (57)
//! ```
//!
//! ...then you would be able to recreate the structure of the towers that looks like this:
//!
//! ```txt
//!                 gyxo
//!               /
//!          ugml - ebii
//!        /      \
//!       |         jptl
//!       |
//!       |         pbga
//!      /        /
//! tknk --- padx - havc
//!      \        \
//!       |         qoyq
//!       |
//!       |         ktlj
//!        \      /
//!          fwft - cntj
//!               \
//!                 xhth
//! ```
//!
//! In this example, `tknk` is at the bottom of the tower (the **bottom program**), and is holding
//! up `ugml`, `padx`, and `fwft`. Those programs are, in turn, holding up other programs; in this
//! example, none of those programs are holding up any other programs, and are all the tops of their
//! own towers. (The actual tower balancing in front of you is much larger.)
//!
//! Before you're ready to help them, you need to make sure your information is correct. **What is
//! the name of the bottom program?**
//!
//! ## Part Two
//! The programs explain the situation: they can't get down. Rather, they **could** get down, if
//! they weren't expending all of their energy trying to keep the tower balanced. Apparently, one
//! program has the **wrong weight**, and until it's fixed, they're stuck here.
//!
//! For any program holding a disc, each program standing on that disc forms a sub-tower. Each of
//! those sub-towers are supposed to be the same weight, or the disc itself isn't balanced. The
//! weight of a tower is the sum of the weights of the programs in that tower.
//!
//! In the example above, this means that for `ugml`'s disc to be balanced, `gyxo`, `ebii`, and
//! `jptl` must all have the same weight, and they do: `61`.
//!
//! However, for `tknk` to be balanced, each of the programs standing on its disc **and all programs
//! above it** must each match. This means that the following sums must all be the same:
//!
//! - `ugml` + (`gyxo` + `ebii` + `jptl`) = 68 + (61 + 61 + 61) = 251
//! - `padx` + (`pbga` + `havc` + `qoyq`) = 45 + (66 + 66 + 66) = 243
//! - `fwft` + (`ktlj` + `cntj` + `xhth`) = 72 + (57 + 57 + 57) = 243
//!
//! As you can see, `tknk`'s disc is unbalanced: `ugml`'s stack is heavier than the other two. Even
//! though the nodes above `ugml` are balanced, `ugml` itself is too heavy: it needs to be `8` units
//! lighter for its stack to weigh `243` and keep the towers balanced. If this change were made, its
//! weight would be `60`.
//!
//! Given that exactly one program is the wrong weight, **what would its weight need to be** to
//! balance the entire tower?

#![allow(clippy::manual_split_once)]

use std::collections::HashMap;

use anyhow::Result;

pub const INPUT: &str = include_str!("d07.txt");

pub fn solve_part_one(input: &str) -> Result<&str> {
    let input = parse_input_one(input)?;
    let mut current = input.iter().next().unwrap();

    while let Some(kv) = input.get_key_value(current.1) {
        current = kv;
    }

    Ok(current.1)
}

pub fn solve_part_two(input: &str) -> Result<i64> {
    let input = parse_input_two(input)?;

    // for (k, v) in &input {
    //     let children = v.1.iter().map(|k| &input[k]).collect::<Vec<_>>();
    //     if children.is_empty() {
    //         continue;
    //     }

    //     if children.iter().any(|c| c.0 != children[0].0) {
    //         // children.iter()
    //     }
    // }

    Ok(0)
}

fn parse_input_one(input: &str) -> Result<HashMap<&str, &str>> {
    Ok(input
        .lines()
        .flat_map(|l| {
            let node = l.split(" (").next().unwrap();
            let children = l.rsplit(" -> ").next().unwrap().split(", ");

            children.map(move |c| (c, node))
        })
        .collect())
}

fn parse_input_two(input: &str) -> Result<HashMap<&str, (u32, Vec<&str>)>> {
    input
        .lines()
        .map(|l| {
            let (node, weight) = l.split_once(" (").unwrap();
            let weight = weight[..weight.find(')').unwrap()].parse().unwrap();

            let children = l.rsplit(" -> ").next().unwrap().split(", ").collect();

            Ok((node, (weight, children)))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use indoc::indoc;

    use super::*;

    #[test]
    fn part_one() {
        let input = indoc! {"
            pbga (66)
            xhth (57)
            ebii (61)
            havc (66)
            ktlj (57)
            fwft (72) -> ktlj, cntj, xhth
            qoyq (66)
            padx (45) -> pbga, havc, qoyq
            tknk (41) -> ugml, padx, fwft
            jptl (61)
            ugml (68) -> gyxo, ebii, jptl
            gyxo (61)
            cntj (57)
        "};

        assert_eq!("tknk", solve_part_one(input).unwrap());
    }

    #[test]
    fn part_two() {}
}