Advent 2023 Day 6
This commit is contained in:
parent
6567050431
commit
57429c49e9
6 changed files with 79 additions and 10 deletions
|
|
@ -1,7 +0,0 @@
|
|||
Step C must be finished before step A can begin.
|
||||
Step C must be finished before step F can begin.
|
||||
Step A must be finished before step B can begin.
|
||||
Step A must be finished before step D can begin.
|
||||
Step B must be finished before step E can begin.
|
||||
Step D must be finished before step E can begin.
|
||||
Step F must be finished before step E can begin.
|
||||
|
|
@ -1 +0,0 @@
|
|||
2,3,0,3,99
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
R75,D30,R83,U83,L12,D49,R71,U7,L72
|
||||
U62,R66,U55,R34,D71,R55,D58,R83
|
||||
2
advent_of_code_2023/day6/input.txt
Normal file
2
advent_of_code_2023/day6/input.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Time: 62 64 91 90
|
||||
Distance: 553 1010 1473 1074
|
||||
42
advent_of_code_2023/day6/main1.rs
Normal file
42
advent_of_code_2023/day6/main1.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn parse_numbers_from_line(line: &str) -> Vec<i32> {
|
||||
line.split_once(' ').unwrap().1
|
||||
.split(' ').filter(|line| {
|
||||
!line.is_empty()
|
||||
}).map(|line| {
|
||||
line.parse::<i32>().unwrap()
|
||||
}).collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn count_winning_strategies(time: i32, distance: i32) -> i32 {
|
||||
let mut winners = 0;
|
||||
|
||||
for hold in 0..time {
|
||||
if (time - hold) * hold > distance {
|
||||
winners += 1
|
||||
}
|
||||
}
|
||||
|
||||
winners
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let lines = read_to_string("input.txt").unwrap().lines().map(|line| {
|
||||
String::from(line)
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
let times = parse_numbers_from_line(&lines[0]);
|
||||
let distances = parse_numbers_from_line(&lines[1]);
|
||||
|
||||
println!("times: {:?}", times);
|
||||
println!("distances: {:?}", distances);
|
||||
|
||||
let mut score = 1;
|
||||
for idx in 0..times.len() {
|
||||
score *= count_winning_strategies(times[idx], distances[idx]);
|
||||
}
|
||||
|
||||
println!("Score: {}", score);
|
||||
}
|
||||
35
advent_of_code_2023/day6/main2.rs
Normal file
35
advent_of_code_2023/day6/main2.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn parse_number_from_line(line: &str) -> u64 {
|
||||
line.split_once(' ').unwrap().1
|
||||
.split(' ').filter(|line| {
|
||||
!line.is_empty()
|
||||
}).collect::<String>().parse::<u64>().unwrap()
|
||||
}
|
||||
|
||||
fn count_winning_strategies(time: u64, distance: u64) -> u64 {
|
||||
let mut winners = 0;
|
||||
|
||||
for hold in 0..time {
|
||||
if (time - hold) * hold > distance {
|
||||
winners += 1
|
||||
}
|
||||
}
|
||||
|
||||
winners
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let lines = read_to_string("input.txt").unwrap().lines().map(|line| {
|
||||
String::from(line)
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
let time = parse_number_from_line(&lines[0]);
|
||||
let distance = parse_number_from_line(&lines[1]);
|
||||
|
||||
println!("time: {:?}", time);
|
||||
println!("distance: {:?}", distance);
|
||||
|
||||
println!("Winning strategies: {}", count_winning_strategies(time, distance));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue