-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from paulcacheux/day2-2024
day 2 part 1 & 2
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
7 6 4 2 1 | ||
1 2 7 8 9 | ||
9 7 6 2 1 | ||
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,10 @@ | |
"day": 1, | ||
"part1": "1660292", | ||
"part2": "22776016" | ||
}, | ||
{ | ||
"day": 2, | ||
"part1": "502", | ||
"part2": "544" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use itertools::Itertools; | ||
|
||
use crate::aoc2024::Aoc2024; | ||
use crate::traits::days::Day2; | ||
use crate::traits::ParseInput; | ||
use crate::traits::Solution; | ||
|
||
impl ParseInput<Day2> for Aoc2024 { | ||
type Parsed = Vec<Vec<u32>>; | ||
|
||
fn parse_input(input: &str) -> Self::Parsed { | ||
let mut grid = Vec::new(); | ||
for line in input.lines() { | ||
let mut row = Vec::new(); | ||
for num in line.split_whitespace() { | ||
row.push(num.parse().unwrap()); | ||
} | ||
grid.push(row); | ||
} | ||
grid | ||
} | ||
} | ||
|
||
impl Solution<Day2> for Aoc2024 { | ||
type Part1Output = u32; | ||
type Part2Output = u32; | ||
|
||
fn part1(input: &Vec<Vec<u32>>) -> u32 { | ||
let mut safe = 0; | ||
for line in input { | ||
if safe_line(line) { | ||
safe += 1; | ||
} | ||
} | ||
safe | ||
} | ||
|
||
fn part2(input: &Vec<Vec<u32>>) -> u32 { | ||
let mut safe = 0; | ||
for line in input { | ||
if safe_line(line) { | ||
safe += 1; | ||
continue; | ||
} | ||
|
||
for i in 0..line.len() { | ||
let left = &line[..i]; | ||
let right = &line[i + 1..]; | ||
if safe_line2(left, right) { | ||
safe += 1; | ||
break; | ||
} | ||
} | ||
} | ||
safe | ||
} | ||
} | ||
|
||
fn safe_line(line: &[u32]) -> bool { | ||
safe_line2(line, &[]) | ||
} | ||
|
||
fn safe_line2(left: &[u32], right: &[u32]) -> bool { | ||
let mut ordering = None; | ||
|
||
for (&l, &r) in left.iter().chain(right).tuple_windows() { | ||
let diff = l.abs_diff(r); | ||
if !(1..=3).contains(&diff) { | ||
return false; | ||
} | ||
|
||
let current_ordering = l.cmp(&r); | ||
if let Some(ordering) = ordering { | ||
if ordering != current_ordering { | ||
return false; | ||
} | ||
} | ||
ordering = Some(current_ordering); | ||
} | ||
|
||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters