Initial commit
This commit is contained in:
BIN
odin/day01.bin
Executable file
BIN
odin/day01.bin
Executable file
Binary file not shown.
6
odin/day01/calories-basic.input
Normal file
6
odin/day01/calories-basic.input
Normal file
@@ -0,0 +1,6 @@
|
||||
1
|
||||
10
|
||||
1
|
||||
|
||||
|
||||
200
|
||||
145
odin/day01/day01.odin
Normal file
145
odin/day01/day01.odin
Normal 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
BIN
odin/day02/day02
Executable file
Binary file not shown.
95
odin/day02/day02.odin
Normal file
95
odin/day02/day02.odin
Normal 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)
|
||||
}
|
||||
3
odin/day02/strategy.input
Normal file
3
odin/day02/strategy.input
Normal file
@@ -0,0 +1,3 @@
|
||||
A Y
|
||||
B X
|
||||
C Z
|
||||
40
prompt/DAY 01.md
Normal file
40
prompt/DAY 01.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Day 1: Calorie Counting
|
||||
|
||||
Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.
|
||||
|
||||
To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.
|
||||
|
||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
||||
|
||||
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
|
||||
|
||||
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.
|
||||
|
||||
For example, suppose the Elves finish writing their items' Calories and end up with the following list:
|
||||
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
This list represents the Calories of the food carried by five Elves:
|
||||
|
||||
The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
|
||||
The second Elf is carrying one food item with 4000 Calories.
|
||||
The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
|
||||
The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
|
||||
The fifth Elf is carrying one food item with 10000 Calories.
|
||||
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).
|
||||
|
||||
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
|
||||
|
||||
To play, please identify yourself via one of these services:
|
||||
Reference in New Issue
Block a user