Day 4
parent
72d0c489f6
commit
1b08937fec
|
@ -0,0 +1,6 @@
|
||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
File diff suppressed because it is too large
Load Diff
59
src/main.rs
59
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::{cmp::Ordering, ops::RangeInclusive as Range};
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use chrono::{Datelike, Local};
|
use chrono::{Datelike, Local};
|
||||||
|
@ -16,6 +16,7 @@ fn main() -> Result<(), Error> {
|
||||||
let small = if small { ".small" } else { "" };
|
let small = if small { ".small" } else { "" };
|
||||||
let path = format!("inputs/{day}{small}.txt");
|
let path = format!("inputs/{day}{small}.txt");
|
||||||
let input = fs::read_to_string(path)?;
|
let input = fs::read_to_string(path)?;
|
||||||
|
println!("running day {day} part {part}");
|
||||||
let result = SOLNS[day as usize - 1][part as usize - 1](input);
|
let result = SOLNS[day as usize - 1][part as usize - 1](input);
|
||||||
println!("{result}");
|
println!("{result}");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -23,7 +24,7 @@ fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
type Part = fn(String) -> u64;
|
type Part = fn(String) -> u64;
|
||||||
type Day = [Part; 2];
|
type Day = [Part; 2];
|
||||||
const DAYS: usize = 3;
|
const DAYS: usize = 4;
|
||||||
const SOLNS: [Day; DAYS] = [
|
const SOLNS: [Day; DAYS] = [
|
||||||
[
|
[
|
||||||
// day 1 part 1
|
// day 1 part 1
|
||||||
|
@ -114,8 +115,62 @@ const SOLNS: [Day; DAYS] = [
|
||||||
.sum()
|
.sum()
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
// day 4 part 1
|
||||||
|
|input| {
|
||||||
|
make_ranges(input)
|
||||||
|
.iter()
|
||||||
|
.map(|(first, second)| {
|
||||||
|
range_contains(first, second) || range_contains(second, first)
|
||||||
|
})
|
||||||
|
.map(|b| if b { 1 } else { 0 })
|
||||||
|
.sum()
|
||||||
|
},
|
||||||
|
// day 4 part 2
|
||||||
|
|input| {
|
||||||
|
make_ranges(input)
|
||||||
|
.iter()
|
||||||
|
.map(|(first, second)| {
|
||||||
|
range_overlaps(first, second) || range_overlaps(second, first)
|
||||||
|
})
|
||||||
|
.map(|b| if b { 1 } else { 0 })
|
||||||
|
.sum()
|
||||||
|
},
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
fn make_ranges(input: String) -> Vec<(Range<u64>, Range<u64>)> {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
line.split(',')
|
||||||
|
.map(|range| {
|
||||||
|
range
|
||||||
|
.split('-')
|
||||||
|
.map(|num| num.parse::<u64>().expect("bad num :/"))
|
||||||
|
})
|
||||||
|
.map(|mut nums| {
|
||||||
|
let lo = nums.next().expect("nolo :/");
|
||||||
|
let hi = nums.next().expect("nohi :/");
|
||||||
|
lo..=hi
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.map(|mut ranges| {
|
||||||
|
let f = ranges.next().unwrap();
|
||||||
|
let s = ranges.next().unwrap();
|
||||||
|
(f, s)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn range_contains(r1: &Range<u64>, r2: &Range<u64>) -> bool {
|
||||||
|
r1.start() <= r2.start() && r1.end() >= r2.end()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn range_overlaps(r1: &Range<u64>, r2: &Range<u64>) -> bool {
|
||||||
|
r1.contains(r2.start())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
enum RPS {
|
enum RPS {
|
||||||
Rock,
|
Rock,
|
||||||
|
|
Loading…
Reference in New Issue