Skip to content

Commit

Permalink
Day 15 - Part 2, brute force
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Dec 15, 2020
1 parent 29fb56c commit 4174e43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
10 changes: 10 additions & 0 deletions day15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ https://adventofcode.com/2020/day/15
## Part 1

In order to be memory efficient, we should use a HashMap of which number was last spoken at which turn. We load it up with the input numbers and begin looping turns starting from input length.

## Part 2

Let's see if the brute-force solution from Part 1 works in reasonable time for this much larger N... Well, if you consider 2 seconds when compiled with `--release` flag reasonable, then I guess it worked. :)

Still, could we find a pattern that helps us figure out a much more efficient algorithm?

For example, the input `3,2,1` is interesting:
- the 2020th number spoken is 438
- the 30000000th number spoken is 18
33 changes: 21 additions & 12 deletions day15/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::HashMap;

fn solve(lines: Vec<String>) -> u32 {
const PART1: u32 = 2020;
const PART2: u32 = 30000000;

fn solve(lines: &[String], target_turn: u32) -> u32 {
let starting_numbers: Vec<u32> = lines[0].split(',').map(|s| s.parse().unwrap()).collect();

// HashMap<K, V>, where number K was last spoken at turn V
Expand All @@ -27,7 +30,7 @@ fn solve(lines: Vec<String>) -> u32 {
number_just_spoken = next_number;
turn += 1;

if turn == 2020 {
if turn == target_turn {
break;
}
}
Expand All @@ -37,25 +40,31 @@ fn solve(lines: Vec<String>) -> u32 {

fn main() {
let lines = utils::read_lines();
println!("Part 1: {}", solve(lines));
println!("Part 1: {}", solve(&lines, PART1));
println!("Part 2: {}", solve(&lines, PART2));
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn solve_works_with_initial_example() {
assert_eq!(solve(vec!["0,3,6".to_string()]), 436);
fn part1_works_with_initial_example() {
assert_eq!(solve(&vec!["0,3,6".to_string()], PART1), 436);
}

#[test]
fn part1_works_with_other_examples() {
assert_eq!(solve(&vec!["1,3,2".to_string()], PART1), 1);
assert_eq!(solve(&vec!["2,1,3".to_string()], PART1), 10);
assert_eq!(solve(&vec!["1,2,3".to_string()], PART1), 27);
assert_eq!(solve(&vec!["2,3,1".to_string()], PART1), 78);
assert_eq!(solve(&vec!["3,2,1".to_string()], PART1), 438);
assert_eq!(solve(&vec!["3,1,2".to_string()], PART1), 1836);
}

#[test]
fn solve_works_with_other_examples() {
assert_eq!(solve(vec!["1,3,2".to_string()]), 1);
assert_eq!(solve(vec!["2,1,3".to_string()]), 10);
assert_eq!(solve(vec!["1,2,3".to_string()]), 27);
assert_eq!(solve(vec!["2,3,1".to_string()]), 78);
assert_eq!(solve(vec!["3,2,1".to_string()]), 438);
assert_eq!(solve(vec!["3,1,2".to_string()]), 1836);
fn part2_works_with_initial_example() {
assert_eq!(solve(&vec!["0,3,6".to_string()], PART2), 175594);
}
}

0 comments on commit 4174e43

Please sign in to comment.