Day 3
This commit is contained in:
parent
eaedf6b76b
commit
39c86a9c8a
1 changed files with 74 additions and 0 deletions
74
d03/main.odin
Normal file
74
d03/main.odin
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package main
|
||||
|
||||
import "core:math"
|
||||
import "core:slice"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
import "core:bytes"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
import "../util"
|
||||
|
||||
part_1 :: proc(lines: []string) {
|
||||
result := 0
|
||||
|
||||
right := 0
|
||||
for line in lines {
|
||||
if line[right] == '#' {
|
||||
result += 1
|
||||
}
|
||||
|
||||
right = (right + 3) % len(line)
|
||||
}
|
||||
|
||||
fmt.printfln("Part 1: %d", result)
|
||||
}
|
||||
|
||||
Slope :: struct {
|
||||
right: i64,
|
||||
down: i64,
|
||||
}
|
||||
|
||||
part_2 :: proc(lines: []string) {
|
||||
result: i64 = 1
|
||||
|
||||
slopes := [?]Slope{
|
||||
{ right = 1, down = 1 },
|
||||
{ right = 3, down = 1 },
|
||||
{ right = 5, down = 1 },
|
||||
{ right = 7, down = 1 },
|
||||
{ right = 1, down = 2 },
|
||||
}
|
||||
|
||||
for slope in slopes {
|
||||
right: i64 = 0
|
||||
trees_hit: i64 = 0
|
||||
|
||||
for line_idx: i64 = 0; line_idx < i64(len(lines)); line_idx += slope.down {
|
||||
line := lines[line_idx]
|
||||
if line[right] == '#' {
|
||||
trees_hit += 1
|
||||
}
|
||||
|
||||
right = (right + slope.right) % i64(len(line))
|
||||
}
|
||||
|
||||
result *= trees_hit
|
||||
}
|
||||
|
||||
fmt.printfln("Part 2: %d", result)
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
context.allocator = context.temp_allocator
|
||||
|
||||
INPUT :: #load("input.txt", string)
|
||||
lines := strings.split(INPUT, "\n")
|
||||
lines = lines[:len(lines)-1]
|
||||
|
||||
part_1(lines)
|
||||
part_2(lines)
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue