121 lines
2.7 KiB
Plaintext
121 lines
2.7 KiB
Plaintext
sample :: #string END
|
|
3-5
|
|
10-14
|
|
16-20
|
|
12-18
|
|
|
|
1
|
|
5
|
|
8
|
|
11
|
|
17
|
|
32
|
|
END
|
|
|
|
Parsing_State :: enum {
|
|
Fresh_Ids;
|
|
Ingredient_Ids;
|
|
}
|
|
|
|
Range :: struct {
|
|
min: u64;
|
|
max: u64;
|
|
}
|
|
|
|
range_contains :: (range: Range, id: u64) -> bool {
|
|
return range.min <= id && id <= range.max;
|
|
}
|
|
|
|
main :: () {
|
|
content := read_entire_file("day5.input");
|
|
solve_first(content);
|
|
solve_second(content);
|
|
}
|
|
|
|
solve_first :: (input: string) {
|
|
fresh_ids: [..]Range;
|
|
ingredients: [..]u64;
|
|
parsing_state: Parsing_State;
|
|
for line: split(input, #char "\n") {
|
|
if line.count == 0 {
|
|
parsing_state = .Ingredient_Ids;
|
|
continue;
|
|
}
|
|
if parsing_state == .Fresh_Ids {
|
|
_, left, right := split_from_left(line, #char "-");
|
|
min := parse_int(*left, u64);
|
|
max := parse_int(*right, u64);
|
|
array_add(*fresh_ids, .{min=min, max=max});
|
|
} else if parsing_state == .Ingredient_Ids {
|
|
id := parse_int(*line, u64);
|
|
array_add(*ingredients, id);
|
|
}
|
|
}
|
|
|
|
total_fresh := 0;
|
|
i: int;
|
|
while ingredient_loop := i < ingredients.count {
|
|
defer i += 1;
|
|
ingredient := ingredients[i];
|
|
for range: fresh_ids {
|
|
if range_contains(range, ingredient) {
|
|
total_fresh += 1;
|
|
continue ingredient_loop;
|
|
}
|
|
}
|
|
}
|
|
|
|
print("%\n", total_fresh);
|
|
}
|
|
|
|
solve_second :: (input: string) {
|
|
fresh_ids: [..]Range;
|
|
for line: split(input, #char "\n") {
|
|
if line.count == 0 {
|
|
break;
|
|
}
|
|
_, left, right := split_from_left(line, #char "-");
|
|
min := parse_int(*left, u64);
|
|
max := parse_int(*right, u64);
|
|
array_add(*fresh_ids, .{min=min, max=max});
|
|
}
|
|
sorted_fresh_ids := quick_sort(fresh_ids, (a: Range, b: Range) -> s32 {
|
|
if a.min < b.min {
|
|
return -1;
|
|
} else if b.min < a.min {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
});
|
|
|
|
smooshed_ids: [..]Range;
|
|
current_i := 0;
|
|
for range: sorted_fresh_ids {
|
|
if smooshed_ids.count == 0 {
|
|
array_add(*smooshed_ids, range);
|
|
continue;
|
|
}
|
|
|
|
smooshed_range := *smooshed_ids[current_i];
|
|
if range.min <= smooshed_range.max + 1 && smooshed_range.max < range.max {
|
|
smooshed_range.max = range.max;
|
|
} else if smooshed_range.max + 1 < range.min {
|
|
array_add(*smooshed_ids, range);
|
|
current_i += 1;
|
|
}
|
|
}
|
|
|
|
total : u64 = 0;
|
|
for smooshed_ids {
|
|
total += (it.max - it.min) + 1;
|
|
}
|
|
print("%\n", total);
|
|
}
|
|
|
|
#import "Basic";
|
|
#import "Hash_Table";
|
|
#import "File";
|
|
#import "Sort";
|
|
#import "String";
|