logo

adventofcode

Code used to solve https://adventofcode.com/, one branch per year git clone https://hacktivis.me/git/adventofcode.git
commit: 1ce0691d9be2aa63f7f00e6d208a2c8f89a84df8
parent e593dc4652ff9b3af2fe4515676d7850ea9f39cd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  2 Dec 2023 14:17:56 +0100

day1_part2.ha: New, for curiosity

Diffstat:

Aday1_part2.ha97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+), 0 deletions(-)

diff --git a/day1_part2.ha b/day1_part2.ha @@ -0,0 +1,97 @@ +// SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+aoc2023@hacktivis.me> +// SPDX-License-Identifier: MIT + +// This is broken code, the hare regex library is currently (2023-12-02) incomplete +// I solved day1_part2 with day1_part2.pl instead + +use bufio; +use strings; +use fmt; +use os; +use io; +use regex; +use strconv; + +fn stoi(line: str) int = { + const line = strings::multireplace(line, + ("one", "1"), + ("two", "2"), + ("three", "3"), + ("four", "4"), + ("five", "5"), + ("six", "6"), + ("seven", "7"), + ("eight", "8"), + ("nine", "9"), + ); + return strconv::stoi(line)!; +}; + +export fn main() void = { + let total = 0u32; + + const fre = regex::compile(`^([0-9]|one|two|three|four|five|six|seven|six|seven|eight|nine)`)!; + const lre = regex::compile( `([0-9]|one|two|three|four|five|six|seven|six|seven|eight|nine)$`)!; + defer regex::finish(&fre); + defer regex::finish(&lre); + + fmt::printfln("Regexes compiled")!; + + for(true) { + const line = match(bufio::read_line(os::stdin)) { + case let l: []u8 => yield l; + case io::EOF => break; + case => abort(); + }; + + let line = strings::fromutf8(line)!; + + fmt::printf("{} ", line)!; + + // Try1: Invalid, need to only replace first spelled number and last one, not all + //const line = strings::multireplace(line, + // ("one", "1"), + // ("two", "2"), + // ("three", "3"), + // ("four", "4"), + // ("five", "5"), + // ("six", "6"), + // ("seven", "7"), + // ("eight", "8"), + // ("nine", "9"), + //); + + // Try2: Definitely invalid "eightwothree" needs to be 8wo3 + //line = strings::replace(line, "one", "1"); + //line = strings::replace(line, "two", "2"); + //line = strings::replace(line, "three", "3"); + //line = strings::replace(line, "four", "4"); + //line = strings::replace(line, "five", "5"); + //line = strings::replace(line, "six", "6"); + //line = strings::replace(line, "seven", "7"); + //line = strings::replace(line, "eight", "8"); + //line = strings::replace(line, "nine", "9"); + + //fmt::printf("→ {} ", line)!; + + let fre_r = regex::find(&fre, line); + let lre_r = regex::find(&lre, line); + defer regex::result_free(fre_r); + defer regex::result_free(lre_r); + + fmt::printfln("Results!")!; + + assert(len(fre_r) > 0); + assert(len(lre_r) > 0); + + const first = stoi(fre_r[0].content); + const last = stoi(lre_r[0].content); + + const val = (first*10)+last; + + fmt::printfln("= {}", val)!; + total += val: u32; + }; + + fmt::printfln("Total: {}", total)!; +};