Skip to content

Commit

Permalink
day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Dec 2, 2024
1 parent 6d92f1b commit e885a3c
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ all = "run --quiet --release -- all"
time = "run --quiet --release -- time"

[env]
AOC_YEAR = "2023"
AOC_YEAR = "2024"
6 changes: 0 additions & 6 deletions .devcontainer/devcontainer.json

This file was deleted.

17 changes: 0 additions & 17 deletions .editorconfig

This file was deleted.

39 changes: 20 additions & 19 deletions .github/workflows/readme-stars.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
name: Update readme ⭐️ progress

on:
# !Please set a different minute than 51 if you enable this!
# schedule:
# - cron: "51 */6 * * *" # Every 6 hours
workflow_dispatch:
# !Please set a different minute than 51 if you enable this!
# schedule:
# - cron: "51 */6 * * *" # Every 6 hours
push:
workflow_dispatch:

jobs:
update-readme:
runs-on: ubuntu-latest
if: ${{ vars.AOC_ENABLED == 'true' }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: k2bd/advent-readme-stars@v1
with:
userId: ${{ secrets.AOC_USER_ID }}
sessionCookie: ${{ secrets.AOC_SESSION }}
year: ${{ secrets.AOC_YEAR }}
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "update readme progress"
update-readme:
runs-on: ubuntu-latest
if: ${{ vars.AOC_ENABLED == 'true' }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: k2bd/advent-readme-stars@v1
with:
userId: ${{ secrets.AOC_USER_ID }}
sessionCookie: ${{ secrets.AOC_SESSION }}
year: ${{ secrets.AOC_YEAR }}
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "update readme progress"
7 changes: 0 additions & 7 deletions .vscode/extensions.json

This file was deleted.

58 changes: 0 additions & 58 deletions .vscode/launch.json

This file was deleted.

6 changes: 6 additions & 0 deletions data/examples/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
Empty file added data/examples/02.txt
Empty file.
71 changes: 71 additions & 0 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::collections::HashMap;

advent_of_code::solution!(1);

pub fn part_one(input: &str) -> Option<u32> {
let mut left = vec![];
let mut right = vec![];
for line in input.lines() {
let (left_int, right_int): (u32, u32) = line
.split_once(" ")
.map(|(a, b)| (a.parse().unwrap(), b.parse().unwrap()))
.unwrap();
left.push(left_int);
right.push(right_int);
}
left.sort_unstable();
right.sort_unstable();

let mut diff = 0;
for idx in 0..left.len() {
let l_el = left.get(idx).unwrap();
let r_el = right.get(idx).unwrap();

if l_el > r_el {
diff += l_el - r_el;
} else {
diff += r_el - l_el;
}
}
Some(diff)
}

pub fn part_two(input: &str) -> Option<u32> {
let mut left = vec![];
let mut right = HashMap::new();
for line in input.lines() {
let (left_int, right_int): (u32, u32) = line
.split_once(" ")
.map(|(a, b)| (a.parse().unwrap(), b.parse().unwrap()))
.unwrap();
left.push(left_int);
*right.entry(right_int).or_insert(0) += 1;
}
left.sort_unstable();

let mut diff = 0;
for idx in 0..left.len() {
let l_el = left.get(idx).unwrap();
let r_el = right.get(l_el).unwrap_or(&0);

diff += l_el * r_el;
}
Some(diff)
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(11));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(31));
}
}
26 changes: 26 additions & 0 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
advent_of_code::solution!(2);

pub fn part_one(input: &str) -> Option<u32> {
None
}

pub fn part_two(input: &str) -> Option<u32> {
None
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}

0 comments on commit e885a3c

Please sign in to comment.