Skip to content

Commit

Permalink
add day 2 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Oct 30, 2024
1 parent fdc5abb commit 3ad7094
Show file tree
Hide file tree
Showing 5 changed files with 1,101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2021/02_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

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

@(test)
d02p1 :: proc(t: ^testing.T) {
input: []string = {"forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"}
got := parse_directions(input)
want := 150
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))

free_all()
}

@(test)
d02p2 :: proc(t: ^testing.T) {
input: []string = {"forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"}
got := parse_submarine_instructions(input)
want := 900
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))

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

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

Coordinate :: struct {
x, y: int,
}

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

depth_increase_count := parse_directions(lines)
fmt.printfln("Product of final position: %v", depth_increase_count)
}

parse_directions :: proc(input: []string) -> int {
pos := Coordinate{0, 0}
for instruc in input {
direction := strings.split(instruc, " ", context.temp_allocator)[0]
distance := strconv.atoi(strings.split(instruc, " ", context.temp_allocator)[1])

switch direction {
case "forward":
pos.x += distance
case "up":
pos.y -= distance
case "down":
pos.y += distance
}
}

return pos.x * pos.y
}
36 changes: 36 additions & 0 deletions 2021/02p2.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

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

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

depth_increase_count := parse_submarine_instructions(lines)
fmt.printfln("Product of final position: %v", depth_increase_count)
}

parse_submarine_instructions :: proc(input: []string) -> int {
pos := Coordinate{0, 0}
aim := 0
for instruc in input {
direction := strings.split(instruc, " ", context.temp_allocator)[0]
weight := strconv.atoi(strings.split(instruc, " ", context.temp_allocator)[1])

switch direction {
case "forward":
pos.x += weight
pos.y += aim * weight
case "up":
aim -= weight
case "down":
aim += weight
}
}

return pos.x * pos.y
}
Loading

0 comments on commit 3ad7094

Please sign in to comment.