Initial commit

This commit is contained in:
2024-11-14 19:16:08 -05:00
commit 73aefb8c9c
7 changed files with 289 additions and 0 deletions

BIN
odin/day01.bin Executable file

Binary file not shown.

View File

@@ -0,0 +1,6 @@
1
10
1
200

145
odin/day01/day01.odin Normal file
View File

@@ -0,0 +1,145 @@
package day01
import "core:fmt"
import "core:os"
import "core:math"
import "core:strconv"
scan_to_next :: proc(data: []byte, delim: byte, start_from: int) -> int {
for idx := start_from; idx < len(data); idx += 1 {
if data[idx] == '\n' {
return idx
}
}
return -1;
}
main_first_attempt :: proc () {
// Read in input file.
fh, errno := os.open("day01/calories-basic.input")
if errno != os.ERROR_NONE {
panic("Don't know what to do!")
}
defer os.close(fh)
line_start := 0
line_end := -1
repr := 0
sum := 0
max := -1
content: []byte
ok: bool
content, ok = os.read_entire_file_from_handle(fh)
if !ok { return; }
for line_start < len(content) {
if content[line_start] == '\n' {
fmt.println(line_start, " starts a blank line: skipping to ", line_start + 1)
// Case: line is blank
// End summation; we are in a new group
max = math.max(max, sum)
// OK YOU NEED TO BE VERY CAREFUL WITH THIS! FIRST ODIN FOOTGUN!
// This line used to be `sum := 0` instead of `sum = 0` and I
// think that re-defined `sum` in this local scope because the
// total sum at the end was way too high; it never got reset to 0
// here. If you think about it, it makes sense... but I would think
// the compiler might warn me that I was shadowing something from
// ouside the scope...
sum = 0
// Go to next line
line_start += 1
continue
}
line_end = scan_to_next(content, '\n', line_start + 1)
fmt.println("Next line: ", line_start, " thru ", line_end)
// Case: no newlines left
if line_end == -1 {
// Get last line
slc := string(content[line_start:len(content)])
repr, ok = strconv.parse_int(slc)
if !ok {
// Line doesn't have a number on it?
// TODO Just ignore for now
} else {
fmt.println("Result: ", repr, " Current Sum: ", sum + repr)
sum += repr
max = math.max(max, sum)
sum = 0
}
break
}
// Case: Normal line with a number on it
slc := string(content[line_start:line_end])
fmt.println("Reading line: ", slc)
repr, ok = strconv.parse_int(slc)
if !ok {
// Line doesn't have a number on it?
// TODO Just ignore for now
} else {
fmt.println("Result: ", repr, " Current Sum: ", sum + repr)
sum += repr
}
line_start = line_end + 1
}
fmt.println("Largest group is ", max);
}
split_lines :: proc(data: []byte, delimiter: byte) -> [dynamic][]byte {
result: [dynamic][]byte
prev := 0
idx: int
for idx = 0; idx < len(data); idx += 1 {
if data[idx] == delimiter {
append(&result, data[prev:idx])
prev = idx + 1
}
}
if prev < idx {
append(&result, data[prev:idx])
}
return result
}
main :: proc() {
if len(os.args) < 2 {
fmt.printf("Usage: %s FILENAME\n", os.args[0])
os.exit(-1)
}
fh, errno := os.open(os.args[1])
if errno != os.ERROR_NONE {
fmt.printf("Cannot open %s: %s\n", os.args[1], os.get_last_error_string())
os.exit(int(errno))
}
defer os.close(fh)
content, ok := os.read_entire_file_from_handle(fh)
if !ok {
fmt.printf("Cannot read %s: %s\n", os.args[1], os.get_last_error_string())
os.exit(-1)
}
lines := split_lines(content, '\n')
total := 0
max := 0
for line, idx in lines {
if len(line) == 0 {
max = math.max(max, total)
total = 0
} else {
x: int
s := string(line)
x, ok = strconv.parse_int(s)
if ok {
total += x
} else {
fmt.eprintf("Line %d: not a valid integer\n", idx)
}
}
}
max = math.max(total, max)
fmt.printf("Largest grouping: %d\n", max)
}

BIN
odin/day02/day02 Executable file

Binary file not shown.

95
odin/day02/day02.odin Normal file
View File

@@ -0,0 +1,95 @@
package day02;
import "core:os";
import "core:fmt";
Move :: enum {
ROCK = 1,
PAPER = 2,
SCISSORS = 3,
}
rune_move : map[u8]Move
init :: proc () {
rune_move := make(map[u8]Move)
rune_move['A'] = Move.ROCK
rune_move['X'] = Move.ROCK
rune_move['B'] = Move.PAPER
rune_move['Y'] = Move.PAPER
rune_move['C'] = Move.SCISSORS
rune_move['Z'] = Move.SCISSORS
}
determine_score :: proc(friendly: Move, enemy: Move) -> int {
result: int;
if enemy == friendly {
result = 3
} else if friendly == Move.ROCK && enemy == Move.PAPER {
result = 0
} else if friendly == Move.PAPER && enemy == Move.SCISSORS {
result = 0
} else if friendly == Move.SCISSORS && enemy == Move.ROCK {
result = 0
} else {
result = 6
}
return result + int(friendly)
}
main :: proc () {
init()
if len(os.args) < 2 {
fmt.printf("Usage: %s FILENAME\n", os.args[0])
os.exit(-1)
}
fh, errno := os.open(os.args[1])
if errno != os.ERROR_NONE {
fmt.printf("Cannot open %s: %s\n", os.args[1], os.get_last_error_string())
os.exit(int(errno))
}
defer os.close(fh)
buffer: []u8 = make([]u8, 1);
defer delete(buffer);
num_read, error := os.read(fh, buffer)
total_score : int = 0;
friendly : Move
enemy : Move
chr : u8
for ; error == os.ERROR_NONE && num_read > 0; num_read, error = os.read(fh, buffer) {
chr = buffer[0]
switch chr {
case '\n':
fmt.printf("Score for this line: %d\n", determine_score(friendly, enemy))
total_score += determine_score(friendly, enemy)
case 'A':
enemy = .ROCK
case 'B':
enemy = .PAPER
case 'C':
enemy = .SCISSORS
case 'X':
friendly = .ROCK
case 'Y':
friendly = .PAPER
case 'Z':
friendly = .SCISSORS
case:
// this is the default
}
}
if chr != '\n' {
total_score += determine_score(friendly, enemy)
}
/// Handle potential errors
if error != os.ERROR_NONE {
fmt.printf("Error reading file: %s", os.get_last_error_string())
os.exit(-1)
}
delete(rune_move);
fmt.printf("Your score is: %d\n", total_score)
}

View File

@@ -0,0 +1,3 @@
A Y
B X
C Z