Skip to content

Commit

Permalink
refactor(2023): simplify parsing day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 19, 2023
1 parent a3e8407 commit 73f1c74
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions rust/2023/src/day_19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,24 @@ pub fn input_generator(input: &str) -> Input {
let workflows = workflows
.lines()
.map(|line| {
let mut parts = line.split('{');
let name = parts.next().unwrap();
let rules = parts
.next()
.unwrap()
.split(',')
.map(|rule| {
let parts = rule.split_once(':');

if let Some((condition, result)) = parts {
if condition.contains('<') {
let (key, value) = condition.split_once('<').unwrap();
let key = key.chars().next().unwrap();
let value = value.parse::<u64>().unwrap();
let parts = line.split(['{', ',', ':', '}']).collect::<Vec<_>>();
let name = parts[0];
let rules = parts[1..]
.chunks(2)
.map(|condition| match condition {
[target, ""] => Condition::Target(target.to_string()),
[condition, target] => {
let (key, value) = condition.split_once(['<', '>']).unwrap();
let key = key.chars().next().unwrap();
let value = value.parse::<u64>().unwrap();

Condition::LessThan(key, value, result.to_string())
if condition.contains('<') {
Condition::LessThan(key, value, target.to_string())
} else {
let (key, value) = condition.split_once('>').unwrap();
let key = key.chars().next().unwrap();
let value = value.parse::<u64>().unwrap();

Condition::GreaterThan(key, value, result.to_string())
Condition::GreaterThan(key, value, target.to_string())
}
} else {
let rule = rule.replace('}', "");

Condition::Target(rule)
}
_ => unreachable!(),
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -223,13 +213,11 @@ fn count_accepted(
*
*
*/
/*
#[doc = r#"```
use advent_of_code_2023::day_19::*;
let data = include_str!("../input/2023/day19.txt");
assert_eq!(solve_part_02(&input_generator(data)), 0);
assert_eq!(solve_part_02(&input_generator(data)), 121464316215623);
```"#]
*/
#[aoc(day19, part2)]
pub fn solve_part_02(input: &Input) -> u64 {
let mut ranges: HashMap<char, (u64, u64)> = HashMap::new();
Expand Down

0 comments on commit 73f1c74

Please sign in to comment.