85 lines
2 KiB
Odin
85 lines
2 KiB
Odin
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"
|
|
|
|
Row :: struct { min: i64, max: i64 }
|
|
Column :: struct { min: i64, max: i64 }
|
|
|
|
part_1 :: proc(lines: []string) {
|
|
result: i64 = 0
|
|
|
|
for line in lines {
|
|
row := Row{ min = 0, max = 127 }
|
|
col := Column{ min = 0, max = 7 }
|
|
for letter in line {
|
|
switch letter {
|
|
case 'B':
|
|
row.min += (row.max - row.min + 1) / 2
|
|
case 'F':
|
|
row.max -= (row.max - row.min + 1) / 2
|
|
case 'R':
|
|
col.min += (col.max - col.min + 1) / 2
|
|
case 'L':
|
|
col.max -= (col.max - col.min + 1) / 2
|
|
}
|
|
}
|
|
|
|
id := row.max * 8 + col.max
|
|
result = max(result, id)
|
|
}
|
|
|
|
fmt.printfln("Part 1: %d", result)
|
|
}
|
|
|
|
part_2 :: proc(lines: []string) {
|
|
result: i64 = 0
|
|
seats := [dynamic]i64{}
|
|
|
|
for line in lines {
|
|
row := Row{ min = 0, max = 127 }
|
|
col := Column{ min = 0, max = 7 }
|
|
for letter in line {
|
|
switch letter {
|
|
case 'B':
|
|
row.min += (row.max - row.min + 1) / 2
|
|
case 'F':
|
|
row.max -= (row.max - row.min + 1) / 2
|
|
case 'R':
|
|
col.min += (col.max - col.min + 1) / 2
|
|
case 'L':
|
|
col.max -= (col.max - col.min + 1) / 2
|
|
}
|
|
}
|
|
append(&seats, row.max * 8 + col.max)
|
|
}
|
|
|
|
slice.sort(seats[:])
|
|
for seat, idx in seats[1:len(seats)-1] {
|
|
if seat - 1 != seats[idx] {
|
|
result = seat - 1
|
|
break
|
|
}
|
|
}
|
|
|
|
fmt.printfln("Part 2: %d", result)
|
|
}
|
|
|
|
main :: proc() {
|
|
context.allocator = context.temp_allocator
|
|
defer free_all(context.temp_allocator)
|
|
|
|
INPUT :: #load("input.txt", string)
|
|
lines := strings.split(INPUT, "\n")
|
|
lines = lines[:len(lines)-1]
|
|
|
|
part_1(lines)
|
|
part_2(lines)
|
|
}
|