Learning Rust using Advent of Code 2022 - Day 1
Table of contents
As ThePrimeagen has mentioned many times in his videos, Advent of Code is a great way to learn a new language. The questions touch on the core concepts (file handling, loops etc.). Solving these challenges allows you to get familiar with these concepts.
Question
I have decided to go with 2022 AOC for this activity so that I can find resources online, If I need any help. Here is the original question I used to solve the issue: https://adventofcode.com/2022/day/1
Summary of the question:
We have a list of calories that we need to process and find elf with highest total calories. The list separates elves by new line. While, the calories are separated by a line break. This list separates the elf by an empty line. The second challenge is to get the total calories of top 3 elf with highest calories.
Breakdown of the question
After reading the question, I broke it down to smaller chunks. Here is the list of steps required to write the solution. The list also includes the different programming concepts it touches:
Step | Programming concept |
Read a txt file containing list of calories | Read from a file |
Separate the txt output into a chunks for each elf | Manipulate string and split into an array |
Loop through an output for each elf | Loops! |
Read the calories for each elf, and gather their total calories | Split string into an array, Parse string into integers |
Find the highest total from all the elves | Basic Math |
Find the total of top 3 elves with highest calories | Split arrays, Sort Array |
Solution
Here is the solution I wrote after doing bunch of google searches to learn these concepts:
use std::fs;
static FILEPATH: &str = "./src/input.txt";
fn main() {
let contents = fs::read_to_string(FILEPATH).expect("Something went wrong reading the file");
let elfs = contents.split("\n\n");
let mut total_calories_vector: Vec<i32> = vec![];
for elf in elfs {
let elf_calories = elf.split("\n");
let mut total_calories = 0;
for calories in elf_calories {
let parsed_calories = calories.parse::<i32>().expect("Invalid Calories");
total_calories = total_calories + parsed_calories;
}
total_calories_vector.push(total_calories)
}
total_calories_vector.sort();
total_calories_vector.reverse();
let highest_total = total_calories_vector.first().expect("Something went wrong for sorted total calories");
let top_three: i32 = total_calories_vector.iter().take(3).sum();
println!("Highest: {}", highest_total);
println!("Sum of Top 3: {}", top_three);
}
You can find the full code at: https://github.com/harshPPatel/advent-of-code-2022/tree/main/aoc_01_calories_counting
Outro
I can see why ThePrimegen is right. Solving the first concept helped me to learn about the basic concepts in Rust. Of course, This is not the finest rust code, and I had to do lot of googling, but I was able to learn rust using this challenge.
If you are doing the same challenge, make sure to never search for exact solution! Search for the core concepts! Here is an example of some of my google searches:
How to split string in rust?
How to parse string values in rust?
How to sort and array?
How to get a chunk of an array?
The results from these google searches lead me directly to the documentation for relevant concept/methods!
If you liked this blog, please give a follow! I am planning to write blog for each problem I solve in AOC using Rust!
Subscribe to my newsletter
Read articles from Harsh Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by