Compare commits
2 Commits
d84e3bff27
...
2cf9933300
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2cf9933300 | ||
|
|
e5a3a1458a |
@@ -1,4 +1,4 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
aoc_2018::tasks::day08::both();
|
// aoc_2018::tasks::day09::task1();
|
||||||
// aoc_2018::tasks::day08::task2();
|
aoc_2018::tasks::day09::task2();
|
||||||
}
|
}
|
||||||
|
|||||||
94
src/tasks/day09.rs
Normal file
94
src/tasks/day09.rs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
const PLAYERS: usize = 468;
|
||||||
|
const MODULO: usize = 23;
|
||||||
|
const HIGHEST_MARBLE: usize = 7184300;
|
||||||
|
|
||||||
|
pub fn task1() {
|
||||||
|
let mut player_score = [0usize; PLAYERS];
|
||||||
|
let mut current_player = 1;
|
||||||
|
let mut current_index: usize = 0;
|
||||||
|
let mut deck: Vec<usize> = Vec::new();
|
||||||
|
deck.push(0);
|
||||||
|
for marble in 1..=HIGHEST_MARBLE {
|
||||||
|
if marble % MODULO == 0 {
|
||||||
|
current_index = rem(current_index, 7, deck.len());
|
||||||
|
player_score[current_player] += marble;
|
||||||
|
player_score[current_player] += deck.remove(current_index);
|
||||||
|
} else {
|
||||||
|
current_index = (current_index + 2) % deck.len();
|
||||||
|
deck.insert(current_index, marble);
|
||||||
|
}
|
||||||
|
current_player = (current_player + 1) % PLAYERS;
|
||||||
|
if marble % 10000 == 0 {
|
||||||
|
println!("{} ({}%)", marble, marble as f32 / HIGHEST_MARBLE as f32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = player_score.iter().max().unwrap();
|
||||||
|
println!("The highest score is {}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rem(a: usize, sub: usize, m: usize) -> usize {
|
||||||
|
if sub > a {
|
||||||
|
(a + m - sub) % m
|
||||||
|
} else {
|
||||||
|
(a - sub) % m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task2() {
|
||||||
|
let _dummy_node = ListNode {
|
||||||
|
value: 0,
|
||||||
|
id_left: 0,
|
||||||
|
id_right: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut player_score = [0usize; PLAYERS];
|
||||||
|
let mut current_player = 1;
|
||||||
|
|
||||||
|
let mut nodes = Vec::with_capacity(HIGHEST_MARBLE);
|
||||||
|
nodes.push(ListNode {
|
||||||
|
value: 0,
|
||||||
|
id_left: 0,
|
||||||
|
id_right: 0,
|
||||||
|
});
|
||||||
|
let mut current_node = &nodes[0];
|
||||||
|
|
||||||
|
for marble in 1..=HIGHEST_MARBLE {
|
||||||
|
if marble % MODULO == 0 {
|
||||||
|
for _ in 0..7 {
|
||||||
|
current_node = &nodes[current_node.id_left];
|
||||||
|
}
|
||||||
|
player_score[current_player] += marble;
|
||||||
|
player_score[current_player] += current_node.value;
|
||||||
|
let id_left = current_node.id_left;
|
||||||
|
let id_right = current_node.id_right;
|
||||||
|
nodes[id_left].id_right = id_right;
|
||||||
|
nodes[id_right].id_left = id_left;
|
||||||
|
current_node = &nodes[id_right];
|
||||||
|
} else {
|
||||||
|
let id_left = current_node.id_right;
|
||||||
|
let id_right = nodes[current_node.id_right].id_right;
|
||||||
|
let new = ListNode {
|
||||||
|
value: marble,
|
||||||
|
id_left,
|
||||||
|
id_right,
|
||||||
|
};
|
||||||
|
nodes.push(new);
|
||||||
|
let id_new = nodes.len() - 1;
|
||||||
|
nodes[id_left].id_right = id_new;
|
||||||
|
nodes[id_right].id_left = id_new;
|
||||||
|
current_node = &nodes[id_new];
|
||||||
|
}
|
||||||
|
current_player = (current_player + 1) % PLAYERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = player_score.iter().max().unwrap();
|
||||||
|
println!("The highest score is {}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ListNode {
|
||||||
|
value: usize,
|
||||||
|
id_left: usize,
|
||||||
|
id_right: usize,
|
||||||
|
}
|
||||||
@@ -6,3 +6,4 @@ pub mod day05;
|
|||||||
pub mod day06;
|
pub mod day06;
|
||||||
pub mod day07;
|
pub mod day07;
|
||||||
pub mod day08;
|
pub mod day08;
|
||||||
|
pub mod day09;
|
||||||
|
|||||||
Reference in New Issue
Block a user