Skip to content

Commit 2f499ea

Browse files
committed
6.1 and 6.2 correct.
1 parent 1c1da33 commit 2f499ea

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

aoc05/src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::cmp::Ordering;
2-
31
use util::input_lines;
42

53
#[derive(Debug, Clone)]

aoc06/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
util = { path = "../util" }

aoc06/src/main.rs

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,73 @@
1+
use util::input_lines;
2+
3+
#[derive(Debug)]
4+
struct Race {
5+
time: u64,
6+
distance: u64,
7+
}
8+
9+
impl Race {
10+
fn ways_to_beat_record(&self) -> u64 {
11+
let mut ways = 0;
12+
for press in 0..=self.time {
13+
if (self.time - press) * press > self.distance {
14+
ways += 1;
15+
}
16+
}
17+
ways
18+
}
19+
}
20+
121
fn main() {
2-
println!("Hello, world!");
22+
let lines = input_lines();
23+
let races = lines[0]
24+
.split_once(":")
25+
.unwrap()
26+
.1
27+
.trim()
28+
.split_whitespace()
29+
.map(|x| x.parse().unwrap())
30+
.zip(
31+
lines[1]
32+
.split_once(":")
33+
.unwrap()
34+
.1
35+
.trim()
36+
.split_whitespace()
37+
.map(|x| x.parse().unwrap()),
38+
)
39+
.map(|(time, distance)| Race { time, distance })
40+
.collect::<Vec<_>>();
41+
// println!("{:?}", races);
42+
let mut product_of_ways = 1;
43+
for race in &races {
44+
product_of_ways *= race.ways_to_beat_record();
45+
// println!("{:?} {}", race, race.ways_to_beat_record());
46+
}
47+
println!("Part 1: {}", product_of_ways);
48+
49+
let race = Race {
50+
time: lines[0]
51+
.split_once(":")
52+
.unwrap()
53+
.1
54+
.trim()
55+
.split_whitespace()
56+
.collect::<Vec<_>>()
57+
.join("")
58+
.parse()
59+
.unwrap(),
60+
distance: lines[1]
61+
.split_once(":")
62+
.unwrap()
63+
.1
64+
.trim()
65+
.split_whitespace()
66+
.collect::<Vec<_>>()
67+
.join("")
68+
.parse()
69+
.unwrap(),
70+
};
71+
// println!("{:?}", race);
72+
println!("Part 2: {}", race.ways_to_beat_record());
373
}

0 commit comments

Comments
 (0)