|
1 | | -use std::ops::Range; |
2 | 1 | use std::str::FromStr; |
3 | 2 | use std::sync::OnceLock; |
4 | 3 | use itertools::Itertools; |
5 | 4 | use aoclp::anyhow::Context; |
6 | 5 | use aoclp::regex::Regex; |
| 6 | +use aoclp::solvers_impl::input::safe_get_input; |
7 | 7 |
|
8 | 8 | pub fn part_1() -> usize { |
| 9 | + let input = input(); |
| 10 | + println!("Presents: {}, regions: {}", input.0.len(), input.1.len()); |
| 11 | + |
9 | 12 | 0 |
10 | 13 | } |
11 | 14 |
|
@@ -73,18 +76,58 @@ impl FromStr for Region { |
73 | 76 | } |
74 | 77 | } |
75 | 78 |
|
| 79 | +fn input() -> (Vec<Present>, Vec<Region>) { |
| 80 | + parse_input(safe_get_input(2025, 12).lines()) |
| 81 | +} |
| 82 | + |
76 | 83 | fn parse_input<I, S>(input: I) -> (Vec<Present>, Vec<Region>) |
77 | 84 | where |
78 | 85 | I: IntoIterator<Item = S>, |
| 86 | + <I as IntoIterator>::IntoIter: Clone, |
79 | 87 | S: AsRef<str>, |
80 | 88 | { |
81 | | - let mut it = input.into_iter(); |
| 89 | + static INDEX_REGEX: OnceLock<Regex> = OnceLock::new(); |
| 90 | + let index_re = |
| 91 | + INDEX_REGEX.get_or_init(|| { |
| 92 | + Regex::new(r"^(?<idx>\d+):\s*$").unwrap() |
| 93 | + }); |
82 | 94 |
|
| 95 | + let mut it = input.into_iter().peekable(); |
83 | 96 | let mut presents = Vec::new(); |
84 | | - for i in 0.. { |
85 | | - let index = it.next(); |
86 | | - if |
| 97 | + let mut i = 0; |
| 98 | + loop { |
| 99 | + // if let Some(line) = it.peek() && line.as_ref().trim_ascii().is_empty() { |
| 100 | + // |
| 101 | + // } |
| 102 | + |
| 103 | + let index_s = it.next().expect("end of data before regions!"); |
| 104 | + let index_s = index_s.as_ref(); |
| 105 | + if index_s.trim_ascii().is_empty() { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + match index_re.captures(index_s) { |
| 110 | + None => break, |
| 111 | + Some(index_cap) => { |
| 112 | + let index = &index_cap["idx"]; |
| 113 | + let index: usize = index.parse().unwrap(); |
| 114 | + if index != i { |
| 115 | + panic!("expected present #{i}, found present #{index}"); |
| 116 | + } |
| 117 | + |
| 118 | + let present: Present = it.clone().take(3).into(); |
| 119 | + presents.push(present); |
| 120 | + |
| 121 | + it = it.dropping(3); |
| 122 | + i += 1; |
| 123 | + }, |
| 124 | + } |
87 | 125 | } |
88 | 126 |
|
89 | | - todo!() |
| 127 | + let regions = it |
| 128 | + .skip_while(|l| l.as_ref().trim_ascii().is_empty()) |
| 129 | + .map(|l| l.as_ref().parse().unwrap()) |
| 130 | + .collect(); |
| 131 | + |
| 132 | + (presents, regions) |
90 | 133 | } |
0 commit comments