Skip to content

Commit

Permalink
day 3 solutions & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Oct 30, 2024
1 parent b2e5d3a commit 5d167ff
Show file tree
Hide file tree
Showing 7 changed files with 1,189 additions and 19 deletions.
31 changes: 16 additions & 15 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ name: CI
on: push

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: laytan/setup-odin@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Report
run: odin report ./2021
- name: Test
run: odin test ./2021
- name: Check
run: odin check ./2021 -vet --strict-style

statistics:
needs: test
name: Benchmark and Update Readme
runs-on: ubuntu-latest
steps:
Expand All @@ -20,18 +36,3 @@ jobs:
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update readme statistics

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: laytan/setup-odin@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Report
run: odin report ./2021
- name: Test
run: odin test ./2021
- name: Check
run: odin check ./2021 -vet --strict-style
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ go.work
**/aoc202*
2021/2021
*.exe
*.bin
55 changes: 55 additions & 0 deletions 2021/03_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import "core:fmt"
import "core:mem"
import "core:testing"

@(test)
d03p1 :: proc(t: ^testing.T) {
input: []string = {
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010",
}
got := calculate_power_consumption(input)
want := 198
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))

free_all()
}

@(test)
d03p2 :: proc(t: ^testing.T) {
input: []string = {
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010",
}
got := calculate_generator_scrubber_rating(input, false)
want := 23
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))

got = calculate_generator_scrubber_rating(input, true)
want = 10
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))

free_all()
}
42 changes: 42 additions & 0 deletions 2021/03p1.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import "core:fmt"
import "core:strconv"
import "core:strings"
import "utils"

D03P1 :: proc() {
lines, backing := utils.read_lines("./inputs/03.txt")
defer delete(lines)
defer delete(backing)

power_consumption := calculate_power_consumption(lines)
fmt.printfln("Power consumption: %v", power_consumption)
}

calculate_power_consumption :: proc(input: []string) -> int {
gamma_rate_str := "0b" // binary uint prefix - used to imply base 2 when parsing

for i := 0; i < len(input[0]); i += 1 {
one_count := 0
zero_count := 0
for line in input {
if line[i] == '1' {
one_count += 1
} else {
zero_count += 1
}
}

if one_count > zero_count {
gamma_rate_str = strings.concatenate({gamma_rate_str, "1"}, context.temp_allocator)
} else {
gamma_rate_str = strings.concatenate({gamma_rate_str, "0"}, context.temp_allocator)
}
}

gamma_rate, _ := strconv.parse_uint(gamma_rate_str)
epsilon_rate := ~gamma_rate & ((0b1 << len(input[0])) - 1) // bitwise flip with mask https://stackoverflow.com/questions/6351374/bitwise-operator-for-simply-flipping-all-bits-in-an-integer

return int(gamma_rate * epsilon_rate)
}
69 changes: 69 additions & 0 deletions 2021/03p2.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import "core:fmt"
import "core:strconv"
import "core:strings"
import "utils"

D03P2 :: proc() {
lines, backing := utils.read_lines("./inputs/03.txt")
defer delete(lines)
defer delete(backing)

life_support_rating :=
calculate_generator_scrubber_rating(lines, false) *
calculate_generator_scrubber_rating(lines, true)

fmt.printfln("Life support rating: %v", life_support_rating)
}

calculate_generator_scrubber_rating :: proc(input: []string, co2: bool) -> int {
dyn_input: [dynamic]string
defer delete(dyn_input)

append(&dyn_input, ..input[:])

for i := 0; i < len(input[0]); i += 1 {
if len(dyn_input) == 1 {
break
}

zero_indexes := [dynamic]int{}
one_indexes := [dynamic]int{}

defer {
delete(zero_indexes)
delete(one_indexes)
}

for line, j in &dyn_input {
if line[i] == '0' {
append(&zero_indexes, j)
} else {
append(&one_indexes, j)
}
}

removed := 0
remove_at := zero_indexes
if len(zero_indexes) > len(one_indexes) {
remove_at = one_indexes
}

if co2 {
if len(zero_indexes) > len(one_indexes) {
remove_at = zero_indexes
} else {
remove_at = one_indexes
}
}

for index in remove_at {
ordered_remove(&dyn_input, index - removed)
removed += 1
}
}

remaining, _ := strconv.parse_uint(dyn_input[0], 2)
return int(remaining)
}
Loading

0 comments on commit 5d167ff

Please sign in to comment.