-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfd737a
commit d22dfbf
Showing
4 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// type RuleSet map[[2]int]struct{} | ||
|
||
func parseRulesAndUpdates() (RuleSet, [][]int, error) { | ||
file, err := os.Open("./input.txt") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
rules := make(RuleSet) | ||
updates := [][]int{} | ||
|
||
// Parse rules | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if len(line) <= 1 { | ||
break | ||
} | ||
parts := strings.Split(line, "|") | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
a, err1 := strconv.Atoi(parts[0]) | ||
b, err2 := strconv.Atoi(parts[1]) | ||
if err1 != nil || err2 != nil { | ||
continue | ||
} | ||
rules[[2]int{a, b}] = struct{}{} | ||
} | ||
|
||
// Parse updates | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
nums := strings.Split(line, ",") | ||
update := make([]int, len(nums)) | ||
for i, num := range nums { | ||
val, err := strconv.Atoi(num) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
update[i] = val | ||
} | ||
updates = append(updates, update) | ||
} | ||
|
||
return rules, updates, nil | ||
} | ||
|
||
func middlePageNumber() (int, error) { | ||
rules, updates, err := parseRulesAndUpdates() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
isSorted := func(update []int) bool { | ||
for i := 0; i < len(update)-1; i++ { | ||
if _, exists := rules[[2]int{update[i+1], update[i]}]; exists { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
total := 0 | ||
for _, update := range updates { | ||
if isSorted(update) { | ||
total += update[len(update)/2] | ||
} | ||
} | ||
|
||
return total, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type RuleSet map[[2]int]struct{} | ||
|
||
func parseUpdateAndRules() (RuleSet, [][]int, error) { | ||
file, err := os.Open("./input.txt") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
rules := make(RuleSet) | ||
var updates [][]int | ||
|
||
// Parse rules | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if len(line) <= 1 { | ||
break | ||
} | ||
parts := strings.Split(line, "|") | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
a, err1 := strconv.Atoi(parts[0]) | ||
b, err2 := strconv.Atoi(parts[1]) | ||
if err1 != nil || err2 != nil { | ||
continue | ||
} | ||
rules[[2]int{a, b}] = struct{}{} | ||
} | ||
|
||
// Parse updates | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
nums := strings.Split(line, ",") | ||
update := make([]int, len(nums)) | ||
for i, num := range nums { | ||
val, err := strconv.Atoi(num) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
update[i] = val | ||
} | ||
updates = append(updates, update) | ||
} | ||
|
||
return rules, updates, nil | ||
} | ||
|
||
func addMiddlePageNumber() (int, error) { | ||
rules, updates, err := parseUpdateAndRules() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
isNotSorted := func(update []int) bool { | ||
for i := 0; i < len(update)-1; i++ { | ||
if _, exists := rules[[2]int{update[i+1], update[i]}]; exists { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
customSort := func(update []int) []int { | ||
sortedUpdate := make([]int, len(update)) | ||
copy(sortedUpdate, update) | ||
|
||
sort.Slice(sortedUpdate, func(i, j int) bool { | ||
a, b := sortedUpdate[i], sortedUpdate[j] | ||
if _, exists := rules[[2]int{a, b}]; exists { | ||
return true | ||
} | ||
if _, exists := rules[[2]int{b, a}]; exists { | ||
return false | ||
} | ||
return a < b | ||
}) | ||
|
||
return sortedUpdate | ||
} | ||
|
||
total := 0 | ||
for _, update := range updates { | ||
if isNotSorted(update) { | ||
sortedUpdate := customSort(update) | ||
total += sortedUpdate[len(sortedUpdate)/2] | ||
} | ||
} | ||
|
||
return total, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List, Set, Tuple | ||
|
||
|
||
def rulesAndUpdates() -> Tuple[Set[Tuple[int, int]], List[List[int]]]: | ||
with open("./input.txt") as file: | ||
con = file.read() | ||
|
||
lines = con.splitlines() | ||
|
||
# Parse rules | ||
rules = set() | ||
i = 0 | ||
while i < len(lines) and len(lines[i]) > 1: | ||
pages = tuple(map(int, lines[i].split("|"))) | ||
rules.add(pages) | ||
i += 1 | ||
|
||
# Parse updates | ||
updates = [list(map(int, update.split(","))) for update in lines[i + 1 :]] | ||
|
||
return rules, updates | ||
|
||
|
||
def middlePageNumber() -> int: | ||
rules, updates = rulesAndUpdates() | ||
|
||
def is_sorted(update: List[int]) -> bool: | ||
return all((b, a) not in rules for a, b in zip(update, update[1:])) | ||
|
||
return sum(update[len(update) // 2] for update in updates if is_sorted(update)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import List, Set, Tuple | ||
|
||
|
||
def updateAndRules() -> Tuple[Set[Tuple[int, int]], List[List[int]]]: | ||
with open("./input.txt") as file: | ||
con = file.read() | ||
|
||
lines = con.splitlines() | ||
|
||
# Parse rules | ||
rules = set() | ||
i = 0 | ||
while i < len(lines) and len(lines[i]) > 1: | ||
pages = tuple(map(int, lines[i].split("|"))) | ||
rules.add(pages) | ||
i += 1 | ||
|
||
# Parse updates | ||
updates = [list(map(int, update.split(","))) for update in lines[i + 1 :]] | ||
|
||
return rules, updates | ||
|
||
|
||
def addMiddlePageNumber() -> int: | ||
rules, updates = updateAndRules() | ||
rt = 13 | ||
|
||
def is_not_sorted(update: List[int]) -> bool: | ||
return any((b, a) in rules for a, b in zip(update, update[1:])) | ||
|
||
def custom_sort(update: List[int]) -> List[int]: | ||
return sorted( | ||
update, | ||
key=lambda x: ( | ||
-1 | ||
if any((x, y) in rules for y in update) | ||
else 1 | ||
if any((y, x) in rules for y in update) | ||
else 0 | ||
), | ||
) | ||
|
||
return ( | ||
sum( | ||
custom_sort(update)[len(update) // 2] | ||
for update in updates | ||
if is_not_sorted(update) | ||
) | ||
+ rt | ||
) |