Solve Part 1
This commit is contained in:
parent
bd71dfb6bb
commit
a83326d9c4
1000
src/input.txt
Normal file
1000
src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
40
src/main.rs
40
src/main.rs
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user