Solve Part 1

This commit is contained in:
apio 2022-12-05 12:29:23 +01:00
parent bd71dfb6bb
commit a83326d9c4
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 1038 additions and 2 deletions

1000
src/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,39 @@
fn main() {
println!("Hello, world!");
// https://adventofcode.com/2022/day/4
use std::ops::Range;
const DATA: &'static str = include_str!("input.txt");
fn parse_range(string: &str) -> Range<i32>
{
let (left, right) = string.split_once("-").unwrap();
left.parse().unwrap() .. right.parse().unwrap()
}
fn part1()
{
let mut pairs_fully_contained = 0;
for slice in DATA.split("\n")
{
if slice.is_empty() { continue; }
let (left, right) = slice.split_once(",").expect("Input data is not correctly structured");
let left_range = parse_range(left);
let right_range = parse_range(right);
if left_range.end >= right_range.end && left_range.start <= right_range.start
{
pairs_fully_contained += 1;
} else if right_range.end >= left_range.end && right_range.start <= left_range.start
{
pairs_fully_contained += 1;
}
}
println!("There are {} pairs in which one range is fully contained in the other one", pairs_fully_contained);
}
fn main() {
part1()
}