Last active
December 1, 2023 18:42
-
-
Save CJSmith-0141/a84b3d213bdd8ed7c256561132d19b8d to your computer and use it in GitHub Desktop.
AoC 2023 Day 1
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
package net.tazato | |
import cats.effect.* | |
object Day1 extends IOApp.Simple { | |
private def eat(s: String): String = { | |
s.replaceAll("one", "o1e") | |
.replaceAll("two", "t2o") | |
.replaceAll("three", "t3e") | |
.replaceAll("four", "f4r") | |
.replaceAll("five", "f5e") | |
.replaceAll("six", "s6x") | |
.replaceAll("seven", "s7n") | |
.replaceAll("eight", "e8t") | |
.replaceAll("nine", "n9e") | |
} | |
private def answer(s: List[String]): Int = | |
s.map(s => eat(s).filter(_.isDigit)) | |
.map(s => s.take(1) ++ s.reverse.take(1)) | |
.flatMap(_.toIntOption) | |
.sum | |
private val input = | |
"""two1nine | |
|eightwothree | |
|abcone2threexyz | |
|xtwone3four | |
|4nineeightseven2 | |
|zoneight234 | |
|7pqrstsixteen""" | |
.stripMargin | |
.split('\n') | |
.toList | |
private val input2F = scala.io.Source.fromResource("day1.txt") | |
private val input2 = try input2F.getLines().toList finally input2F.close() | |
val run: IO[Unit] = for { | |
_ <- IO.println(answer(input)) | |
_ <- IO.println(answer(input2)) | |
} yield () | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment